We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    I'm trying to add a button to the Elements Tree toolbar to create a new Category. Figured it should be fairly simple, just add another block to the modx.tree.element.js file immediately following the other blocks for the other buttons, following the same pattern. Wrong.

    Category management is handled quite differently than the other elements. Category creation is tied to the main Categories element in the Tree, via AJAX. The form is created in the same modx.tree.element.js file, after the toolbar buttons are defined. Ok, shouldn't be a big problem to get the new button in the toolbar to use the same AJAX-based popup form, right? Wrong.

    Question: "this" obviously doesn't work; the scope is wrong. But what is the scope supposed to be?


            },{
                icon: MODx.config.manager_url+'templates/default/images/restyle/icons/category.png'
                ,cls: 'x-btn-icon'
                ,tooltip: {text: _('new_category')}
                ,handler: this.createCategory
                ,scope: this
                ,hidden: MODx.perm.new_category ? false : true           
            }]
    


    I added new_category to the list of permission settings in the config.js.php file (around line 90) so the permission check works on the button.

      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
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      The scope is correct (this refers to the tree object), but it fails within the MODx.tree.Element.createCategory function. The problem is that it expects to be triggered by the context menu, which sets "this.cm.activeNode" with the details of the node that was right clicked.

      If we fake that before calling createCategory we're all set.

      This is what I added to the tbar config:
      ,{
                  icon: MODx.config.manager_url+'templates/default/images/restyle/icons/category.png'
                  ,cls: 'x-btn-icon'
                  ,tooltip: {text: _('new')+' '+_('category')}
                  ,handler: function() {
      				this.cm.activeNode = { attributes: {}, id: 'n_category' };
      				this.createCategory(null,{target: null})
      			}
                  ,scope: this
                  //,hidden: MODx.perm.new_category ? false : true
              }

      (which is now line 67-76)

      We're also setting the activeNode.id on n_category, which will make sure the category part of the tree is refreshed after adding the category. We also pass in some bogus stuff to createCategory to prevent breaking other stuff.
        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.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Thank you. Works great!

        Do you think it's worth posting as a feature request in Tracker?

        I figured it would end up being some way of triggering that node as soon as I realized that there isn't a clear method for managing categories as there is for the other elements.

        The next little irritation I'd like to scratch is adding another menu item to the main Site menu when installing Articles. After all, it's a custom resource type so it should be possible to add a link for it in the menu along with the other resource types. Unfortunately there isn't enough room in the Resource Tree toolbar to add another button; I'm not sure it would even be possible to add one on-the-fly like that without hacking the core .js file. [ed. note: sottwell last edited this post 14 years, 7 months ago.]
          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
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          Do you think it's worth posting as a feature request in Tracker?
          Or a pull request on github now that we've pretty much got it already, right? smiley I think it would make it more consistent.

          The next little irritation I'd like to scratch is adding another menu item to the main Site menu when installing Articles. After all, it's a custom resource type so it should be possible to add a link for it in the menu along with the other resource types.
          That should probably be a feature request for the Articles package.. it's technically already possible so that does beg the question why it wasn't implemented in the first place.

          Unfortunately there isn't enough room in the Resource Tree toolbar to add another button; I'm not sure it would even be possible to add one on-the-fly like that without hacking the core .js file.
          It's possible to refactor the toolbars to get those with AJAX as well, which could then allow a plugin to dynamically append items. But yeah there's not a lot of room for more there.. sad
            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.