Jump to content

Set checkbox in admin depending on query (i'm not good enough with hooks to figure it out)


Recommended Posts

Posted

Hey there,

So I'm trying to update a checkbox in the admin depending on a condition (if the current page exist in a PageArray elsewhere). I'm not sure how to navigate the hooks to achieve this.

I suppose I'm trying to do "before render, check if this page exist in the PageArray, if yes, set checkbox to checked, otherwise leave unchecked".

many thanks!

 

Posted
$wire->addHookBefore('Inputfield::render', function (HookEvent $event) {
    $inputfield = $event->object;

    // Proceed only for 'yourField'
    if ($inputfield->name !== 'yourField') {
        return;
    }

    // Get the associated page
    if (!($page = $inputfield->hasPage)) {
        return;
    }

    // Check if the page is part of the yourPageArrayField on page with id 1 
    // (or whatever the ID of the page with your pageArraField is)
    $productPages = pages()->get(1)->yourPageArrayField;
    if ($productPages->has($page)) {
        $inputfield->attr(['checked' => 'checked', 'value' => 1]);
    }
});

 

  • Like 3

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