Update page status after page.update:after

Hi all,

I use a site.update:after hook to change the status of two pages to draft or unlisted depending on a toggle fields data. When I select “Results” the status of two pages should update to draft and when selecting “Registration” the status should update to unlisted. The hook works fine when I select “Results” and both pages change their status to draft but not the other way around. It throws an Call to a member function changeStatus() on null error. Anything I’m missing here?

version: 3.4.2

site.yml

toggle:
  label: Status
  type: radio
  columns: 2
  options:
    - Results
    - Registration

my hook

'hooks' => [ 
   'site.update:after' => function ($newSite) {
       $pageResults = page('results');
       $pageRegistration = page('registration');

       if($newSite->toggle() == "Results") {
           $pageResults ->changeStatus('unlisted');
           $pageRegistration ->changeStatus('unlisted');
       } elseif($newSite->toggle() == "Registration") {
           $pageResults ->changeStatus('draft');
           $pageRegistration ->changeStatus('draft');
      }
   }
]

This happens because the page helper doesn’t find drafts.

Use

$resultPage = site()->childrenAndDrafts()->find('results');

instead.

1 Like

I see I’ve missed that info box on the page helper reference page…

Thanks a lot @texnixe :slight_smile: