Jump to content

Add pages to page field AsmSelect on page save hook


RyanJ
 Share

Recommended Posts

Hello,

I have a page field type (Multiple pages(PageArray)) with the input field type as AsmSelect. 

The admin users can select one or more pages from a list in the admin area. Some of those pages have children and if a checkbox is selected and the parent page has been added to the list, then I want to add the child pages as well on page save.  So in admin.php I am trying to set those id's, but I can't get them to save. Hopefully the code clears up what I am trying to do. I appreciate any assistance. 

$pages->addHookAfter('save', function($event) {
	$pages = $event->object; 
  	$page = $event->arguments(0); 
  	//check if on template
  	if($page->template == 'item') {
  		//is the checkbox checked to add children
 		if ($page->add_children == 1) {
 			//get the values in the page field type field
 			foreach ($page->page_assignment as $child) {
 				//if selected pages have childen
 				if ($child->numChildren > 0) {
 					//loop through those children
 					foreach ($child->children as $grandchild) {
 					//set values.
 					//$page->set('page_assignment', array($grandchild->id))
 					$page->page_assignment = array($grandchild->id);
 					//we got the id of the page we need to s
 					$pages->error($grandchild->id);
 					}
 				}
 			}
 		
 		}
  	}

 

Link to comment
Share on other sites

$pages->addHookAfter('saveReady', function($event) {
	$pages = $event->object; 
  	$page = $event->arguments(0); 
  	//check if on template
  	if($page->template == 'item') {
  		//is the checkbox checked to add children
 		if ($page->add_children == 1) {
 			//get the values in the page field type field
 			foreach ($page->page_assignment as $child) {
 				//if selected pages have childen
 				if ($child->numChildren > 0) {
 					//loop through those children
 					foreach ($child->children as $grandchild) {
 					//set values.
                    $page->page_assignment->add($grandchild);
 					}
 				}
 			}
 		
 		}
  	}

I figured it out. Two things to change if anyone is looking to do the same. 

1. Use the right hook. saveReady instead of save.

2. Add the page object. $page->page_assignment->add($grandchild);

  • Like 5
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

×
×
  • Create New...