We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41255
    • 19 Posts
    How can I use a Plugin for resources with a specific template?

    When I edit and save a resource document with template 'Company' the plugin must run. Documents with other templates may not run the plugin.

    Plugin 1
    <?php
    /* keep this from running in the Manager */
    if ($modx->context->get('key') == 'mgr') {
        return;
    }
     
    /* save the new alias based on pagename */
    $resource->set('alias', '');
    $resource->save();
    

    System Event: OnBeforeDocFormSave

    Plugin 2
    <?php
    /* keep this from running in the Manager */
    if ($modx->context->get('key') == 'mgr') {
        return;
    }
     
    /* (optional) Don't execute for existing resources --
       Remove the following line to let users modify
       existing parents in NewsPublisher
     
    if (!($mode == modSystemEvent::MODE_NEW)) return;  */
    
    /* get parent ID from TV */
    $parentId = $resource->getTVValue('index_category');
    
    /* save the parent id in the resource's parent field */
    $resource->set('parent', $parentId);
    $resource->save();
    

    System Event: OnDocFormSave

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

    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Check for the 'template' value of the resource; this is the ID of the template in question. You can even make it a plugin property.
      $myPluginTemplate = $modx->getOption('myplugin.template', $scriptProperties, '1');
      if($resource->get('template') != $myPluginTemplate) return;


      Those events also have a $mode variable available.
      if($mode == 'upd') return;


      https://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events/onbeforedocformsave
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 41255
        • 19 Posts
        Thank you for your explanation.

        This solution works for me. Perfect.
          • 3749
          • 24,544 Posts
          The $mode variable takes a slightly different form now, (not sure if there's still backward-compatibility code there). To make sure the code works in the future, the test should be:

          if ($mode == modSystemEvent::MODE_UPD) {
          }
          

          or

          if ($mode == modSystemEvent::MODE_NEW) {
          }



            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            $modx->invokeEvent('OnBeforeDocFormSave',array(
                'mode' => modSystemEvent::MODE_NEW,
                ...

            It's been that way since 2.0, and "new" and "upd" has worked since then.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Line 2615 of modx.class.php
              class modSystemEvent {
                  /**
                   * @var string For new creations of objects in model events
                   */
                  const MODE_NEW = 'new';
                  /**
                   * @var string For updating objects in model events
                   */
                  const MODE_UPD = 'upd';
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 3749
                • 24,544 Posts
                Right, but the constants are the "official" references. The values could change to integers for more speed, though it's unlikely at this point.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting