Help with Snippets, Dashboard Widgets, and VersionX

I have a custom dashboard widget (manager side) that I created (unfortunately cannot share the exact code) which essentially uses a dashboard widget which contains a table of data populated by the user. The snippet will go through each row of the table and generate resources and other actions for each table row entry.

On the front end, i have a “hub” that lists a number of resources, and sorts them based on the dates saved by VersionX for that resource.

The issue I am having is that VersionX relies on the OnDocFormSave event to trigger a new “version-entry” to be created for a resource. My snippet/widget creates and saevs its resources using “$newResource->save();” which does not seem to be setting an initial “version-entry” in VersionX.

I can prove this by making a new resource myself and saving, which directly calls the ondocformsave event and creates a first “version-entry” for the resource i create.

But if I use my widget/snippet, there is no entry in the VersionX UI. I must open the resources created by my widget and manually click “save” to get VersionX to take a snap of the resource and log that version.

Can anyone provide help on how to work with VersionX/snippets/events so that it will create an initial “version-entry” for each of the resources that i create using my snippet?

Why not order by the resource editedon date itself?

Maybe you can just call the function newResourceVersion() yourself in your snippet, after you created the new resource. Something like this:


$path = $modx->getOption('versionx.core_path', null, MODX_CORE_PATH . 'components/versionx/');
$versionx = $modx->getService('versionx', 'VersionX', $path . 'model/');
$result = $versionx->newResourceVersion($newResource, 'new');
1 Like

@halftrainedharry Thank you for this suggestion. I have unfortunately tried this with no success previously, and also tried yours as well with no success.

This could be where my lack of knowledge shows because I am not sure how to troubleshoot the code effectively to see what is happening/breaking.

This method is definitely my preferred way of solving if possible, but i may have to take @markh 's advice and change my sorting parameter, though this is not preferred.

@halftrainedharry’s code is exactly what VersionX does itself when the OnDocFormSave event is run, so that ought to work. If it doesn’t:

  • Check the variable naming. Harry’s example assumes $newResource but as you haven’t provided your code it is impossible for us to say if that’s the right variable.
  • Check the MODX log for any errors.
  • If you get a white page, check the server (PHP) error log.

@markh $newResource is correct as that variable name came from my code.

no white pages so doesnt seem to be any php syntax errors.

i will need to check the modx logs though and will port more info here after I do.

I have a plugin setup that execute VersionX when resources are manually saved and it works fine. but when i try using that same code in this snippet, it doesnt - so modx errors are probably the best bet for causes…

i should get back to this in the later afternoon

Has perhaps nothing on the resource changed? The newResourceVersion method only creates a new version if the resource is different from the last stored version.

@markh This is a very big possibility, as it is saving before running versionx,

.. code that manipulates the RESOURCE ..


$path = $modx->getOption('versionx.core_path', null, MODX_CORE_PATH . 'components/versionx/');
$versionx = $modx->getService('versionx', 'VersionX', $path . 'model/');
$result = $versionx->newResourceVersion($newResource, 'new');

When the resource is created with this widget, there are a number of TVs set and snippets running to automate tasks that would normally be executed by a user, but aftermaking these changes it uses $newResource->save(); to save the state. SHould i be attempting to do versionx code above before the $newResource->save(); line?

I re-read this a third time and want to note that the verision history shows no entries, no history after the widget creates the resource. i can fix this by simply pressing the save button at that point and a version entry will be logged. but I am trying to add somethign to the widget so they user doesnt need to use teh save button manually to trigger versionx via the OnDocFormSave event

The resource has to be saved in the database before calling newResourceVersion() because inside the function the resource is loaded again.

I did a test with the following snippet and this works on my installation.

$newResource = $modx->newObject('modResource');
$newResource->set('parent', 1);
$newResource->set('pagetitle', 'created with snippet');
$newResource->set('template', 1);
$newResource->set('alias', 'created-with-snippet');
$newResource->set('createdon', time());
$newResource->set('published', 1);
$newResource->set('publishedon', time());
$newResource->setContent('page content');

$path = $modx->getOption('versionx.core_path', null, MODX_CORE_PATH . 'components/versionx/');
$versionx = $modx->getService('versionx', 'VersionX', $path . 'model/');
$result = $versionx->newResourceVersion($newResource, 'new');

if ($result){
    return 'versionx entry created';
} else {
    return 'creation of versionx entry failed';

Isn’t the whole point of this thread that there isn’t already a stored version of the resource in modx_versionx_resource?

I guess, but I can’t think of any other reason calling that method would cause no errors nor a new version getting logged. :stuck_out_tongue: