Jump to content

Link to pageReference when the field is set to: Open + Locked (not editable)


Recommended Posts

When you have a pageReference field and set the visibility to `Open + Locked (not editable)`, only the text that you set (default: title) is shown.
But is is also possible to enable a link to this page reference? It would be very helpful if a user can navigate to the pageReference.

  • Like 1
Link to comment
Share on other sites

AI help sounds good:

Certainly! You can use a hook to modify the output of a Page Reference field when it is set to "Open + Locked (not editable)" by hooking into the `renderValue` method of the `InputfieldPage` class. Here is an example of how you can achieve this in ProcessWire:

// Add this to your site/ready.php or a custom module
$wire->addHookAfter('InputfieldPage::renderValue', function(HookEvent $event) {
    $inputfield = $event->object;
    $page = $inputfield->value;

    if($page instanceof Page) {
        // Modify the output to include a link to the referenced page
        $event->return = "<a href='{$page->url}'>{$page->title}</a>";
    }
});

This hook will intercept the rendering of the value for `InputfieldPage` fields and modify it to include a link to the referenced page. Make sure to place this code in a file that is executed on every request, such as `site/ready.php` or within a custom module's `init` method.

For more information on hooks in ProcessWire, you can refer to the https://processwire.com/docs/modules/hooks/

  • Like 2
Link to comment
Share on other sites

Posted (edited)

Thanks, this is indeed what I was looking for (using `$page->editUrl` to link to the edit page).

My final code which checks if the user can edit the referenced page:

$wire->addHookAfter('InputfieldPage::renderValue', function(HookEvent $event) {
    $page = $event->object->value; 
    
    if($page instanceof Page && $page->editable) {
        $event->return = "<a href='{$page->editUrl}'>{$page->title}</a>"; 
    }
});

 

Edited by thuijzer
  • Like 1
Link to comment
Share on other sites

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...