We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22448
    • 241 Posts
    This is one of those that i didn’t think would be a problem, but after trying different things i still can’t find a solution.

    Here’s the situation.

    I need to run some code to populate a drop down list in a template variable. So i wrote a snippet to execute the necessary logic. But the logic needs to know the id of the document that the values are being populated for. I tried the following from within @EVAL of a tv and from a snippet that was being called from tv @EVAl with no success:
    $myID = $modx->documentObject[’id’];
    $myID = $modx->documentIdentifier;

    Although all of these methods work fine when the snippet is called from the template.
    So i suspect that different document parsing takes place when a document is loaded in the manager.

    This seems to be pretty fundamental so it leads me to believe that i’m missing something obvious.

    Any help would be greatly appreciated.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      When you are editing a resource, the resource ID is in the $_GET[’id’] and it’s made available as the $id variable (it’s set in line 37 of the manager/actions/mutate_content.dynamic.php file).

      When editing a resource, it’s not being parsed. You are just being provided a form to set all the metadata and content values for the resource. The $modx object is loaded just to provide some useful global values and functions, but since the resource isn’t actually going through the parser the documentObject and similar objects and arrays aren’t available.
        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
        • 33968
        • 863 Posts
        You’ll need to use an @select binding, so hopefully you can get your intended output using a mysql query instead.

        More info here:
        http://modxcms.com/forums/index.php/topic,49281.msg287729.html#msg287729
          • 22448
          • 241 Posts
          @Susan
          that’s what i suspected. i didn’t even look at the url. thank you for the suggestion

          @lucas
          that’s a different take and totally makes sense. thanks for the pointer
            • 22448
            • 241 Posts
            Following Susan’s suggestion i looked into getting the document id from the url.

            $id variable is populated with the edited document id on line 37 of mutate_content.dynamic.php. However just using $id variable in my snippet did not work. I suspect it’s due to the scoping issues.
            So just copying the id setting code into the snippet and changing the variable name did the trick:

            if (isset($_REQUEST['id']))
                    $myId = (int)$_REQUEST['id'];
            else    $myId = 0;


            Thank you Susan