We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43302
    • 18 Posts
    Hi,

    I'm trying to figure out how to add a submenu to my CMP build script. I've come so far that I have the main menu item set up, but I'm not sure how to link the submenu item to it's parent. I've tried to add the submenu as a child of it's parent, but it doesn't seem to work. I get no errors from the build script, and outputting $menu is a bit daunting. I'm not sure where to look.

    They both use the same Action, they just have different parameters (straight jump to a subpage).


    Edit:

    I'm not sure what I was doing when I posted this, I was thinking way too much. I've since realized just how far off I was, and how easy it actually is. So to help anyone trying to figure out the same thing, I've posted the correct way in the second post below. Hope it helps someone.

    This question has been answered by fractalwolfe. See the first response.

    [ed. note: fractalwolfe last edited this post 13 years, 2 months ago.]
    • discuss.answer
      • 43302
      • 18 Posts
      Here's the correct way to add submenus, you simply set the parent field to the parents name. See code below.

      transport.menu.php
      $action = $modx->newObject('modAction');
      $action->fromArray(array(
          'id' => 1,
          'namespace' => 'harbour',
          'parent' => 0,
          'controller' => 'index',
          'haslayout' => true,
          'lang_topics' => 'harbour:default',
          'assets' => '',
      ),'',true,true);
      $menus[1] = $modx->newObject('modMenu');
      $menus[1]->fromArray(array(
          'text' => 'harbour', // This value is used in the parent field of the submenu
          'parent' => 'components',
          'description' => 'harbour.menu_dashboard_desc',
          'icon' => 'images/icons/plugin.gif',
          'menuindex' => 0,
          'params' => '',
          'handler' => '',
      ),'',true,true);
      $menus[1]->addOne($action);
      
      $action = $modx->newObject('modAction');
      $action->fromArray(array(
          'id' => 1,
          'namespace' => 'harbour',
          'parent' => 0,
          'controller' => 'index',
          'haslayout' => true,
          'lang_topics' => 'harbour:default',
          'assets' => '',
      ),'',true,true);
      $menus[2] = $modx->newObject('modMenu');
      $menus[2]->fromArray(array(
          'text' => 'harbour.menu_events',
          'parent' => 'harbour', // This matches the text value of the parent
          'description' => 'harbour.menu_events_desc',
          'icon' => 'images/icons/plugin.gif',
          'menuindex' => 0,
          'params' => '&action=mgr/events',
          'handler' => '',
      ),'',true,true);
      $menus[2]->addOne($action);
      
      return $menus;

      build.transport.php
      $menus = include $sources['data'].'transport.menu.php';
      foreach ($menus as $menu) {
          $vehicle = $builder->createVehicle($menus,array(
              xPDOTransport::PRESERVE_KEYS => true,
              xPDOTransport::UPDATE_OBJECT => true,
              xPDOTransport::UNIQUE_KEY => 'text',
              xPDOTransport::RELATED_OBJECTS => true,
              xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
                  'Action' => array(
                      xPDOTransport::PRESERVE_KEYS => false,
                      xPDOTransport::UPDATE_OBJECT => true,
                      xPDOTransport::UNIQUE_KEY => array ('namespace','controller'),
                  ),
              ),
          ));
          $builder->putVehicle($vehicle);
          unset($menu, $vehicle);
      }