We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30107
    • 44 Posts
    I'm developing a plugin which has to distinguish between modResource and modSymlink. For test purposes, I created a _symlink_ and ask for it's properties:

    $id = $modx->resource->get('id'); // returns ID of this symlink - ok
    $classkey = $modx->resource->get('class_key'); // returns modResource! D'oh!

    Shouldn't it report "modSymlink" instead of "modResource"? I suppose it returns the type of the document it is "symlinking" to. How can I get the correct type of the symlink itself?

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

      • 6038
      • 228 Posts
      Sounds odd. The class_key should return 'modDocument', 'modSymLink' or 'modWebLink' for each.
      What event are calling the plug-in on?

        • 30107
        • 44 Posts
        Quote from: christianhanvey at Nov 23, 2012, 09:39 PM
        Sounds odd. The class_key should return 'modDocument', 'modSymLink' or 'modWebLink' for each.
        What event are calling the plug-in on?
        The plugin is using the events "OnDocFormSave" and "OnBeforeSaveWebPageCache". But I don't think that this is the cause for the problem.

        If the symlink resource has the id 123, the following snippet returns "modSymlink" in the output (the snippet has to be called in the template of the symlink). That's the expected behaviour.

        $object = $modx->getObject('modDocument',array('id' => 123));
        $output .= print_r($object->toArray(), true);
        return $output;

        The following snippet returns "modDocument" - that's not expected (at least by me ;-)
        $id = $modx->resource->get('id'); // returns ID of this symlink - ok
        $classkey = $modx->resource->get('class_key'); // returns modResource! D'oh!

        [ed. note: titanium last edited this post 13 years, 10 months ago.]
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Perhaps it's because modResource is the base class?
            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
            • 30107
            • 44 Posts
            Quote from: sottwell at Nov 24, 2012, 02:32 AM
            Perhaps it's because modResource is the base class?
            Sorry, I had to correct my previous posting. I'm not sure what "base class" does mean. Do you mean that this is the class of the document the symlink is referencing to? That's true, I think.
            I don't know why the two snippets in my previous posting deliver different results.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              The modSymlink, modWeblink, modDocument, etc. are all extensions of the base modResource class.
              class modSymLink extends modResource implements modResourceInterface {
                  /**
                   * Overrides modResource::__construct to set the class key for this Resource type
                   * @param xPDO $xpdo A reference to the xPDO|modX instance
                   */
                  function __construct(xPDO & $xpdo) {
                      parent :: __construct($xpdo);
                      $this->set('type', 'reference');
                      $this->set('class_key', 'modSymLink');
                      $this->showInContextMenu = true;
                  }
              ...
              

              Definitely something odd going on. Looks like a bug to me. Have you tried having one of these linking to another weblink or symlink to see what happens? [ed. note: sottwell last edited this post 13 years, 10 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
                • 30107
                • 44 Posts
                Quote from: sottwell at Nov 24, 2012, 02:45 AM
                Definitely something odd going on. Looks like a bug to me. Have you tried having one of these linking to another weblink or symlink to see what happens?
                Here are my findings.

                My test snippet looks like this:
                $id = $modx->resource->get('id');
                $output = '<p>ID: ' . $id . '</p>';
                
                $classkey = $modx->resource->get('class_key');
                $output .= '<p>Class key: ' . $classkey . '</p>';
                
                $object = $modx->getObject('modSymLink',array('id' => 123));
                $output .= '<p>' . print_r($object->toArray(), true) . '</p>';
                
                $modx->log(modX::LOG_LEVEL_ERROR, $output, '', __FUNCTION__, __FILE__, __LINE__);
                return $output;

                1. If I choose "resource type" -> "symlink", pointing to another document: $classkey returns to "modDocument" -> wrong!
                2. If I choose "resource type" -> "weblink", pointing to another document: nothing is returned, neither in system log. I suppose the snippet isn't called by design.
                3. If I choose "resource type" -> "symlink", pointing to another symlink: same as 1.
                4. If I choose "resource type" -> "weblink", pointing to another symlink: same as 2.
                • discuss.answer
                  • 30107
                  • 44 Posts
                  To give a summary:

                  $classkey = $modx->resource->get('class_key');

                  -> returns modDocument: wrong!

                  $resource = $modx->getObject('modResource', array('id' => $modx->resource->get('id')));
                  $classkey = $resource->get('class_key');

                  -> returns modSymlink: true! (but weird and inelegant, at least).

                  I think I have to go with the second choice. Seems to be a bug.
                    • 22303 MODX Staff
                    • 10,725 Posts
                    A SymLink is forwarded to another Resource, so it is expected that it takes on the class_key of the SymLink target. Otherwise, the Resource being forwarded to, would also attempt forward it's request.