Jump to content

Hook to change page status depending on children


Juergen
 Share

Recommended Posts

Hello @ all,

I have the following scenario: A parent page status should be changed depending on the number of children. If there is at least 1 child the status should be published, if there are no children the statuts should be unpublished. Seems easy to realize at first sight, but ... the button of the parent page should also change.

If there are children (at least 1):

screenshot-www.juergen-kern.at-2017-11-08-08-39-26.png.fc79cd55ca50b6e6c63769066e3760e3.png

Only the save button should be displayed.

If there are no children:

screenshot-www.juergen-kern.at-2017-11-08-08-39-51.png.6d9a65745ce01005489a7bf68626bf9c.png

The 2 buttons should be displayed.

The children will be created or deleted via a Pagetable field and can be also created by clicking a checkbox in the parent page. I have tried to hook into "added" and "deleted" and also "trashed" to set the page->parent->status and it seems to work, but the button appearence will not change. So the status of the parent page seems to be changed but not the buttons (that is the problem) .

Here is what it looks like after deleting of the last child:

screenshot-www.juergen-kern.at-2017-11-08-08-55-04.png.9feb90fc1811f6dc6166ad460a9bf7b1.png

As you can see the status of the page was changed to "2048" which is unpublished, but the button is the same as before (no change).

If you take a look into the settings tab you will see that the checkbox for unpublish is also not checked:

screenshot-www.juergen-kern.at-2017-11-08-08-59-45.png.64a5af874781628defe783ac7d18ad49.png

The buttons change only if I hook into "before save ready" but this kind of hook doesnt take care of added or delete pages. It is also not possible to check for hasChildren because the creation of the children take place after saving of the parent page.

Does someone has struggled with the same problem and has a working solution?

Another approach would be to prevent the deletion of the last child.

Link to comment
Share on other sites

An idea would be to check the number of children before the deletion. If there is only 1 child than prevent processing of the delete function. Maybe lets say a hook that prevents the execution of the deletion????

Something like

$event->replace = true;
Link to comment
Share on other sites

Ok, I have found a solution to prevent the deletion of a page:

$wire->addHookBefore("Pages::trash", function($event) {
$page = $event->object;
$event->replace = true; // now original function won't be called
$event->return = $this->message("Trashing not allowed!");
});

So it must be adapted to only run on certain templates and if there is only 1 child.

Link to comment
Share on other sites

Here is the final code the prevents the deletion of the last child:

I restrict it that the child is type of one of the following templates:  single-date, single-event, single-business-vacation or single-special-business-hours.

//Prevent deletion of the the last child
$wire->addHookBefore("Pages::trash", function($event) {
$page = $event->arguments(0);
if(!in_array($page->template->name, array('single-date', 'single-event', 'single-business-vacation', 'single-special-business-hours'))) return;
$parent = $page->parent;
$childrennumber = count($page->parent->children);
if($childrennumber === 1) {
 $event->replace = true; // now original function won't be called
 $event->return = $this->message("Trashing not allowed! There must be at least 1 page.");
}
});

I put a complete description in the Tutorial section

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