Jump to content

Recommended Posts

Posted (edited)

Hi all, Im trying to fill an options field in all the children of the parent page, after I save it. 

the options field is configured as multiple select checkboxes. here's the code i have so far:

$this->addHookAfter('Pages::saved', function($event) 
{ 
    $page = $event->arguments[0]; 		//set the page
	if($page->template == 'sport-verenigingen-overzicht')
	{
          //get the subcategories from the parent textarea, split on newline
     	  $subcats = preg_split('/[\n\r]+/', $page->subcats);	

          //(also tried without imploding and adding the array, also doesnt work)
     	  $subcats = implode("|",$subcats); 

          //get the children
          $children = $page->children();						

          foreach ($children as $child) 
          {
              //set the options(sport_categorie is the options field)
          $child->sport_categorie = $subcats;
          $child->save('sport_categorie');
          }

        //if i use a normal textfield instead of an optionsfield,
	//all the children have the correct data e.g: test1|test2|test3
	//how to get the values into the options field??
    }      
 });

Hope you guys can help! Keep up the good work, I'm loving what you're doing with PW!!

Edited by Vinnie
formatting
Posted (edited)

Check this thread and theses answers :

 

 

If you still can't figure how it work, just post here, we will give you a working chunk code.

Oh and welcome to the forum :)

 

 

Edited by flydev
welcome
Posted

hi Flydev! Thanks a lot for the quick reply.

I have read those topics but somehow i still cant get it to work

the line   $field = $fields->get("sport_categorie");   produces the error:  Call to a member function get() on null.

Maybe this is because im adding the hook in admin.php? On the other hand admin is just a template so it should have acces to $fields?

Thanks in advance!

Posted
8 hours ago, Vinnie said:

the line   $field = $fields->get("sport_categorie");   produces the error:  Call to a member function get() on null.

Inside a hook in admin.php, you have to use $this or wire() to return an object.

So $this->fields->get("sport_categorie"); will return your field.

 

About the option field, look at the following code :

$this->addHookAfter('Pages::saved', function($event) 
{ 
        $page = $event->arguments[0]; 		//set the page
	if($page->template == 'sport-verenigingen-overzicht')
	{
           //get the subcategories from the parent textarea, split on newline
           $subcats = preg_split('/[\n\r]+/', $page->subcats);	

           //(also tried without imploding and adding the array, also doesnt work)
     	   //$subcats = implode("|",$subcats);
	   $subcats = implode("\n",$subcats);

          //get the children
          $children = $page->children();						

          foreach ($children as $child) 
          {
            //set the options(sport_categorie is the options field)
            //$child->sport_categorie = $subcats;
            //$child->save('sport_categorie');
            $field = $this->fields->sport_categorie;
            $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true);
            $child->save($child->sport_categorie);
          }

        //if i use a normal textfield instead of an optionsfield,
	//all the children have the correct data e.g: test1|test2|test3
	//how to get the values into the options field??
    }      
 });

 

 

 

 

  • Like 3
Posted

Hi again,

Yes this fixed it!
I Didnt know I had to use $this in admin.php.

I'm just having one little issue now, when i remove a line from the parent textarea, and add something else, iget the following error:
Integrity constraint violation: 1062 Duplicate entry 'blabla-134' for key 'title'

How can i prevent this?
 

Thanks in advance!
 

Posted (edited)

removed solution because it did not work after all

Edited by Vinnie
deleted solution because it did not work after all
Posted

I have found the problem: when adding the options in my textarea i need to include the id so instead of:

cat1
cat2
cat3

I need to fill in 

1=cat1
2=cat2
3=cat3

my final code:

$this->addHookAfter('Pages::saved', function($event) 
{ 
    $page = $event->arguments[0]; 							//set the page

	//get the subcategories from the parent textarea, split on newline
    $subcats = preg_split('/[\n\r]+/', $page->subcats); 

    //implode the items with a newline
	$subcats = implode("\n",$subcats); 

    switch ($page->template) 
    {
    	case 'winkels-overzicht':
	        //set the options
	    	$field = $this->fields->winkel_categorie;
	        $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true);
    	break;

    	case 'sport-verenigingen-overzicht':
	        //set the options
	    	$field = $this->fields->sport_categorie;
	         $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true);
    	break;

    	case 'cultuur-overzicht':
	        //set the options
	    	$field = $this->fields->cultuur_categorie;
	         $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true);
    	break;

    	case 'vrijetijd-overzicht':
	        //set the options
	    	$field = $this->fields->vrije_tijd_categorie;
	         $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true);
    	break;

    	case 'horeca-overzicht':
	        //set the options
	    	$field = $this->fields->horeca_categorie;
	         $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true);
    	break;
    	
    	default:
    		break;
    }
 });

 

  • Like 1

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
×
×
  • Create New...