Jump to content

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


joe_g
 Share

Recommended Posts

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!

 

Link to comment
Share on other sites

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