We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • For MoreGallery, I'm trying to add an icon to the resource toolbar so users can more easily create new Gallery custom resources. I found the OnResourceToolbarLoad system event, but I can't seem to get that to work.

    Has anyone else managed to do this before?

    Here's the related code that does not seem to do much:

    switch ($modx->event->name) {
        case 'OnResourceToolbarLoad':
            $modx->log(modX::LOG_LEVEL_ERROR, 'Triggered on OnResourceToolbarLoad');
            $action = (isset ($modx->actionMap) && isset($modx->actionMap['resource/create']) ) ? $modx->actionMap['resource/create'] : 'resource/create';
            $scriptProperties['items'][] = array(
                'icon' => 'assets/components/moregallery/mgr/img/picture.png',
                'tooltip' => 'add new gallery',
                'handler' => 'new Function("this.redirect(\"index.php?a='.$action.'&class_key=mgResource\");");'
            );
            $items[] = array(
                'icon' => 'assets/components/moregallery/mgr/img/picture.png',
                'tooltip' => 'add new gallery',
                'handler' => 'new Function("this.redirect(\"index.php?a='.$action.'&class_key=mgResource\");");'
            );
            break;


    The logging statement ends up in the error log as expected, so the code is being triggered.

    The code in the core (resource/gettoolbar processor) looks like this:
    $this->modx->invokeEvent('OnResourceToolbarLoad',array(
        'items' => &$items,
    ));
    
    return $this->modx->error->success('',$items);


    I would expect from that code that changing the $items array in the plugin should change the toolbar. That didn't work, so I also added it via $scriptProperties['items'], thinking that might work instead, but no dice there either.

    Am I looking at this completely wrong, or is the pass-by-reference way of working with plugin variables completely broken?

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

    [ed. note: markh last edited this post 10 years, 6 months ago.]
      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.
    • Hey Mark,

      Never did that (wasn't even aware of the event), but i also failed at adding it via PHP.
      Another option could be via ExtJS wink

      <?php
      /**
       * @var modX $modx
       * @var array $scriptProperties
       *
       * @event OnManagerPageBeforeRender
       */
      
      $modx->controller->addHtml('<script>Ext.onReady(function() {
          var tree = Ext.getCmp("modx-resource-tree");
          var tb = tree.getTopToolbar();
          tb.add({
                  icon: "404.png",
                  tooltip: "Tip tip ?",
                  handler: function() {
                      console.log("clicked")
                  }
          });
          tb.doLayout();
      });</script>');
      
      return '';
      


      Hope that will help
      • Yeah, through ExtJS would've been my next option... just would've been nice if the plugin event, like, worked wink

        Thanks for the example code, that'll save me some time!
          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.
        • discuss.answer
          This is the code I ended up with to add the gallery icon to the toolbar:

          switch ($modx->event->name) {
              case 'OnManagerPageBeforeRender':
                  /**
                   * @var modManagerController $controller
                   */
                  if (!$modx->getOption('moregallery.add_icon_to_toolbar', null, true)) {
                      return;
                  }
                  $controller->addHtml(<<<HTML
          <script>
          MODx.on('ready', function() {
              var tree = Ext.getCmp('modx-resource-tree'),
                  tb = tree.getTopToolbar();
          
              setTimeout(function() {
                  tb.insertButton(4, {
                      icon: '/assets/components/moregallery/mgr/img/picture.png',
                      tooltip: 'Add Gallery',
                      handler: function() {
                          var action = (MODx.action && MODx.action['resource/create']) ? MODx.action['resource/create'] : 'resource/create';
                          MODx.loadPage(action, 'class_key=mgResource');
                      }
                  });
                  tb.doLayout();
              }, 150);
              });
          </script>
          HTML
          );
          }
            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.