Jonathan Lahijani Posted August 12, 2017 Share Posted August 12, 2017 I have a Page Reference field (ASM Select) and I am utilizing the "Custom Format" for the "Label field". However even the custom format itself is a bit limiting for a particular use case I have. Is it possible to hook into it and modify the output cleanly with PHP? I can't seem to find a proper hook. Somewhat related... it's possible to do this for the Tree page labels via ProcessPageListRender::getPageLabel. 1 Link to comment Share on other sites More sharing options...
abdus Posted August 12, 2017 Share Posted August 12, 2017 Labels are generated inside InputfieldPage::getPageLabel method (\wire\modules\Inputfield\InputfieldPage\InputfieldPage.module), but unfortunately it's not hookable. You can prefix the method with 3 underscores ___ to enable hooking (it works that way) but when you update the core it will be overwritten with unhookable version. Feel free to make a feature request on Github. // /site/ready.php wire()->addHookAfter('InputfieldPage::getPageLabel', function (HookEvent $e) { $field = $e->object; if($field->name !== 'myPageField') return; $page = $e->arguments(0); $e->return = "$page->title @ {$page->parent->title}"; }); 3 Link to comment Share on other sites More sharing options...
Robin S Posted August 12, 2017 Share Posted August 12, 2017 I haven't tested it much but it looks like you can hook Page::getMarkup() $wire->addHookAfter('Page::getMarkup', function(HookEvent $event) { $page = $event->object; if($page->template->name !== 'TEMPLATE(S)_USED_IN_YOUR_PAGE_FIELD') return; $out = $page->title; // add more stuff to $out using $page $event->return = $out; }); 4 Link to comment Share on other sites More sharing options...
Macrura Posted August 12, 2017 Share Posted August 12, 2017 Also consider InputfieldSelectizeMultiple, it can support any complex markup for the select items, and any field, subfield, sub-subfield etc.. 2 Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted November 10, 2017 Author Share Posted November 10, 2017 On 8/11/2017 at 5:57 PM, Robin S said: I haven't tested it much but it looks like you can hook Page::getMarkup() $wire->addHookAfter('Page::getMarkup', function(HookEvent $event) { $page = $event->object; if($page->template->name !== 'TEMPLATE(S)_USED_IN_YOUR_PAGE_FIELD') return; $out = $page->title; // add more stuff to $out using $page $event->return = $out; }); Thanks Robin, this worked well and avoids the approach of having to hack PW and make InputfieldPage::getPageLabel hookable. Note: In order for it to work, you must edit the field's "Label field" under the "Input" tab. It must be changed from "title (default)" to "Custom format (multiple fields) ..." and a value must be put in the "Custom page label format" field, even if it's just a temp value (I put "x"). 3 Link to comment Share on other sites More sharing options...
bernhard Posted July 16, 2020 Share Posted July 16, 2020 My approach from today: $wire->addHookProperty("Page::myFooLabel", function($event) { $page = $event->object; if($page->template != 'foo-page') return; $event->return = $page->title . " (foo label)"; } Then just set the label field to "myFooLabel" and enjoy. This also has the benefit of having the dynamic label always available easily via $page->myFooLabel 3 2 Link to comment Share on other sites More sharing options...
Pete Posted January 26, 2022 Share Posted January 26, 2022 On 7/16/2020 at 6:02 PM, bernhard said: My approach from today: $wire->addHookProperty("Page::myFooLabel", function($event) { $page = $event->object; if($page->template != 'foo-page') return; $event->return = $page->title . " (foo label)"; } Then just set the label field to "myFooLabel" and enjoy. This also has the benefit of having the dynamic label always available easily via $page->get('myFooLabel'); This is a great approach, thanks @bernhard One place it doesn't work, and I don't know why, is using it as a labelField in a Profields Table using PageAutocomplete. When you select something and save it works fine, but the list of suggestions returned is blank. What I mean is this - there's a result for those 4 letters, but using this hook it doesn't display. As you can see from the Everest row though, I've used it to stitch the page title and a price field together so when you select the blank item from the list it's selected, but the label doesn't appear until the page is saved. If anyone smarter than me can figure out how to get it to work with PageAutocomplete in a Profields Table you would be my hero ? Link to comment Share on other sites More sharing options...
Pete Posted January 27, 2022 Share Posted January 27, 2022 Okay so I resolved this by chance - it won't work in PageAutocomplete UNLESS you give it a "normal" field as an alternative, so here is what you need in the config for that field in the table: labelField=myFooLabel|title I suspect what is happening is the AJAX request doesn't know what myFooLabel is for some part of the request but this makes the items appear in the list using myFooLabel which was a nice surprise! Not really sure why it makes it work, just guessing above but it does work and that's the main thing ? 3 Link to comment Share on other sites More sharing options...
dotnetic Posted September 24, 2022 Share Posted September 24, 2022 On 8/12/2017 at 2:29 AM, abdus said: Feel free to make a feature request on Github. Did it, because I need the same Please make InputfieldPage::getPageLabel hookable · Issue #460 · processwire/processwire-requests (github.com) Link to comment Share on other sites More sharing options...
Pete Posted January 20, 2023 Share Posted January 20, 2023 Ha just had another case today where I needed this and forgot I'd already worked it out once nearly a year ago ? This happens to everyone right? ? 1 3 Link to comment Share on other sites More sharing options...
dotnetic Posted January 21, 2023 Share Posted January 21, 2023 19 hours ago, Pete said: Ha just had another case today where I needed this and forgot I'd already worked it out once nearly a year ago ? This happens to everyone right? ? Same for me. Encounter a problem. Search the forum, and find an answer that I provided myself ? 1 1 Link to comment Share on other sites More sharing options...
monollonom Posted September 7, 2023 Share Posted September 7, 2023 Adding my grain of salt to this (I just faced this situation), you can also leave the Label field to the default "title" and change it temporarily (though you need to set it back once the inputfield is rendered): $wire->addHookAfter("InputfieldPage::getSelectablePages", function(HookEvent $event) { if($event->object->hasField != "yourField") return; $pages = $event->return; foreach($pages as $page) { $page->originalTitle = $page->title; $page->title = "Custom title based on $page->someField"; } $event->addHookAfter("Inputfield::render", function(HookEvent $event) use ($pages) { foreach($pages as $page) { $page->title = $page->originalTitle; } $event->removeHook(null); }); $event->return = $pages; }); (not sure though how this would behave in your setup @Pete) 1 Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted February 9 Author Share Posted February 9 As of today on the dev branch, this is now possible with a new hookable method InputfieldPage::renderPageLabel() https://github.com/processwire/processwire-requests/issues/460 5 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now