We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32046
    • 75 Posts
    Hi,
    I have a site where the client need to be able to enter his product info under any any number of categories, each category being a container. Some products need to be listed in several categories (containers), but rather than create the same resource in each container as needed, I would like allow the client to create the resource once and then simply create weblinks (to that resource) in the other relevant containers. I would like to do this by having a TV of checkboxes where they can choose additional categories when they create a product resource.

    To achieve this I’ll write a plugin using the onDocFormSave event to examine the state of the checkboxes immediately after a resource is created or edited. Depending on the state of the checkboxes the plugin will then create or delete weblinks in various relevant containers.

    This is pretty straightforward (or so I thought), however I cant seem to find any method to create or delete weblinks on the fly. I thought there would be API functions like $modx->createWeblink, $modx->deleteResource etc.

    So the question (finally):
    - Does anyone know of any API functions that can achieve this?
    - If not, is it safe to add/remove rows of the modx database. For example, I can see when I create a Weblink in the manager it adds a row to modx_site_content table. Is it safe to add/delete rows here using $modx->db->query etc, or are there other rows that also need altering when a weblink is created/edited/deleted?


      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Why design it that way? Why not have a checkbox or radio button TV to use tags, then use Ditto to filter by tag?
        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
        • 32046
        • 75 Posts
        Thanks Susan, is there anything you don’t know about modX?
        I didn’t realise Ditto could do that, I haven’t used it much for anything other than very basic output. Definitely sounds like a possible solution.
        I guess the next question is can ditto output be piped into Wayfinder to create a menu system, or even, I suppose, can ditto be used to create a menu system, which I guess it probably can.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          The main differences between Ditto and Wayfinder is that Ditto uses TVs, while Wayfinder is more limited to the usual resource fields that one would expect to use in a menu, and Ditto is not nested; it creates a single-level list. There are also more options in Wayfinder for dealing with issues that are only relevant to nested lists, such as dealing with elements that are "parents".

            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
            • 15152
            • 360 Posts
            Susan is sharp - vey sharp. I never thought about that solution, but it makes perfect sense now.
              There are no boring projects. Only boring executions. ~ Etzkorn
              www.impress-design.com
              • 3749
              • 24,544 Posts
              Please excuse me for butting in. Susan’s idea is definitely the way to go, but I wanted to comment on your original question about how to do it. It would be possible, but fairly inconvenient in Evolution.

              In Revolution, however, it would look something like this (assuming that you have the pagetitle, and url of the target document for the weblink):

              $fields = array(
                  'pagetitle' =>$pagetitle,
                  'content'=>$url
              );
              
              $weblink = $modx->newObject('modWeblink', $fields);
              $weblink->save();
              
              /* and later: $weblink->remove(); */


                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
                • 32046
                • 75 Posts
                Bobray, revo looks awesome, and ultimately I guess thats what I’ll end up using when I find the time to look into it further.
                Susan, your idea was super, being able to use tags in ditto is pretty awesome. I got the ditto/tv/checkboxes working in just a few quick minutes but unfortunately I then realized I need the structure to be laid out in such a way so Wayfinder can construct menus/submenus and make active links, which looked to be a nightmare with the way I had structured it all for Ditto.

                The final solution (which is now 90% complete and looks like it’ll work really nicely) was to have a container for all the product resources. A second container held the product catagories on the first level (also as containers) and under each relevant container a weblink is created to point to the relevant product in the product container. These weblinks are automatically created (and deleted) using the DB API and a simple onDocFormSave plugin, which reads the product templates category checkboxes with are selected/deselected depending on which categories the products should appear in. Using weblinks means the structure is kept tidy for Wayfinder.

                Thanks for the awesome help and ideas. You guys are so knowledgeable on modX - it would be extremely difficult to grasp some of these ideas without help from the fantastic modX community. smiley