Jump to content

Modify "Custom Page Label Format" via hook/API


Jonathan Lahijani
 Share

Recommended Posts

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.

  • Like 1
Link to comment
Share on other sites

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}";
});

 

  • Like 3
Link to comment
Share on other sites

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;
});

 

  • Like 4
Link to comment
Share on other sites

  • 2 months later...
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").

  • Like 3
Link to comment
Share on other sites

  • 2 years later...

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');

  • Like 2
  • Thanks 2
Link to comment
Share on other sites

  • 1 year later...
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.

image.png

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

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!

image.png

Not really sure why it makes it work, just guessing above but it does work and that's the main thing ? 

  • Like 3
Link to comment
Share on other sites

  • 7 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...