Jump to content

Recommended Posts

Posted

Hello,

I'm trying to add or remove a value from a read only page reference field based on the fact if an image field on the same page is empty or not. The other values should not be affected.
So in my ready.php I have:

$wire->addHookBefore('Pages::saveReady', function($event){
    $page = $event->arguments(0);
    if($page->hasField("images")) {
        if($page->images->count() > 0) {
            $page->my_page_reference_field = 'Title of referenced Page'; //-> this works and adds the value to the list
            $this->message("Added value");
        } else {
            $page->my_page_reference_field->remove('Title of referenced Page'); //-> this doesn't
            $this->message("Value removed");
        }
    }
});

Adding the Value works great, but I cannot figure out how to remove a specific value.

Does anybody know how to solve this?

Posted
1 hour ago, torf said:
$page->my_page_reference_field->remove('Title of referenced Page'); //-> this doesn't

Not sure but a quick shot: use ->removeAll();

Posted
23 minutes ago, bernhard said:

Not sure but a quick shot: use ->removeAll();

Thanks - that indeed works, but removes all the values from the field. In my case I'd need to keep the other values and just delete a specific on.
But I'll keep that in mind as plan B if I do not find another solution.

Posted

First you need to make sure to get the value of the page reference field as PageArray (not a single page object or such). This might depend on your setting of the field and whether output formatting is turned on or off (in the hook it should be off, but just in case...).

As ->removeAll() works it should be a PageArray in your case. In my case the field is called "pagelink" and I force the return type to be a pagearray via []

l4V3Od0.png

Then from the pagearray you can remove your page via remove("id=...") which is all you need I think

  • Like 2
Posted

Ah. Thanks a lot - It's a bit more complicated then I thought, but this will work.

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
  • Recently Browsing   0 members

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