We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3232
    • 380 Posts
    I am creating a simple plugin to check and see if a menu title is empty on save and if it is, copy the value of pagetitle to it.

    my code is as follows:

    switch($modx->event->name) {
        
      case 'OnBeforeDocFormSave':
            $menutitle = $_POST['menutitle'];
      	$pagetitle = $_POST['pagetitle'];
      	$modx->log(modX::LOG_LEVEL_ERROR, $menutitle .':' . $pagetitle);
        	       
      	if ($menutitle == '')
    	  $_POST['menutitle'] = $pagetitle;
            break;
    }


    It appears the post data is saved before this runs so I can't modify it. How would one go about changing the value at the onbeforedocsave event?
      • 3232
      • 380 Posts
      After a lot more tinkering I was able to get it to work with the following:

      switch($modx->event->name) {
          
        case 'OnBeforeDocFormSave':
        
      	$title = $resource->get('pagetitle');
      	$mtitle = $resource->get('menutitle');
      	
        	if ($mtitle == '')
      	  $resource->set('menutitle', $title );
      						       
        	break;
      }
        • 18373 ☆ A M B ☆
        • 3,141 Posts
        Glad you got it figured out smiley

        Something else you could do (depending on your needs / where you use the menutitle) is using something like this in the template:
        [[*menutitle:default=`[[*pagetitle]]`]]


        or if using it inside tpl chunks for Wayfinder / getResources, use:
        [[+menutitle:default=`[[+pagetitle]]`]]
          Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

          Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.