MODX 3 coding question — onResourceSort not working

In a plugin, in MODX 3, the following isn’t working in a Manager plugin:

if($modx->event->name == 'OnResourceSort') {
  $resource = $modx->getObject('modResource', $modifiedNodes[0]->id);
}

Does MODX 3 not return $modifiedNodes after dragging and dropping in the tree? Or is something else amiss?

Works fine in 2.8.1.

The call looks like this:

$this->modx->invokeEvent('OnResourceSort', [
            'nodes' => &$this->nodes,
            'nodesAffected' => &$this->nodesAffected,
            'contexts' => &$this->contexts,
            'contextsAffected' => &$this->contextsAffected,
            'modifiedNodes' => &$this->nodesAffected, /* backward compat */
        ]);

It looks like modifiedNodes is deprecated, so you might have better luck with nodesAffected.

I had already tried nodesAffected, and it didn’t return anything. I’ve tried it in MODX 3, and same thing: no ID returned. Should this work?:

$nodesAffected[0]->id

I tried it with the newest Nightly Build for MODX 3 and it seems to me, that the Event OnResourceSort never gets fired at all. Nothing is written to the log, when I sort resources in the tree.

$modx->log(modX::LOG_LEVEL_ERROR,'Event: ' . $modx->event->name);

if($modx->event->name == 'OnResourceSort') {
  $resource = $modx->getObject('modResource', $modifiedNodes[0]->id);
}

Can you confirm this?

I think you’re right. I installed a plugin to write a message to the log onResourceSort and nothing happens.

I’ve submitted a bug report:

1 Like

So I finally got this figured out. It seems that $modifiednodes is an array, that contains objects, that contain arrays. So, this is what works:

$resource = $modx->getObject('modResource',$modifiedNodes[0]->_fields['id']);

The array $modifiedNodes contains an object called “0”, which contains an array called “_fields”, which contains all the resource info, including the ID (and other fields).

0 is just the index of the first item in the array. (There may be other items.)
The items in the array should already be of type modResource. There’s no need to use $modx->getObject().

$resource = $modifiedNodes[0];

but $modifiedNodes[0]->id doesn’t work in MODX 3, so something is different there. It worked in MODX 2.8. Normally, I don’t have to use “get” ($resource->get(‘id’)), I can get the ID directly ($resource->id).

So, does this mean that any script where I use “$modx->resource->id” will fail in MODX 3?

UPDATE:
I tried $modx->resource->id in a script in MODX 3, and it works. So, why doesn’t $modifiedNodes[0]->id work? I tried $modifiedNodes[0]->get(‘id’), and that DOES work in MODX 3.

That’s weird. When I test it, it works.

Maybe it depends on the system event used? Don’t know why it would. I’m using it with OnResourceSort. I did extensive testing and recording of output, and using $modifiedNodes[0]->id always gave no value.
For me, this works:

if($modx->event->name == 'OnResourceSort') {
    $resource = $modx->getObject('modResource',$modifiedNodes[0]->_fields['id']);
}

And this works:

if($modx->event->name == 'OnResourceSort') {
    $resource = $modx->getObject('modResource',$modifiedNodes[0]->get('id');
}

When I run this plugin, it logs the correct id value to the MODX error log.

if($modx->event->name == 'OnResourceSort') {
    $modx->log(modX::LOG_LEVEL_ERROR,"[OnResourceSort] id = " . $modifiedNodes[0]->id);
}

@snowcreative Are you sure that event is firing? You can check by writing to the error log in your plugin.

BTW, you probably know this, but if OnResourceSort is the only event your plugin is attached to, there’s no need to check the event name.

Yes, it’s firing because the other code works, and when I insert a line to write in the log, it does write the message.

And yes, OnResourceSort isn’t the only event the plugin uses.

That’s exactly what I put in my script to test it, and no value for ID was recorded. So, no idea why it works for you and not for me! Not a big deal, since there are alternatives, but a mystery none the less.