Hello, First of all: It works fine in MODX 2.8.4 and previous versions. If I try to list the userinfo via TV Listbox:
@SELECT fullname, id FROM modx_user_attributes WHERE blocked='0' ORDER BY fullname ASC
I get this error message:
.../modx/core/src/Revolution/modTemplateVar.php : 1016) modTemplateVar::parseBinding - third parameter is invalid JSON :@SELECT fullname, id FROM modx_user_attributes WHERE blocked='0' ORDER BY fullname ASC
OK, I got it. The class modTemplateVar does not seem to work with MODX 3.0.1 and I have modified it, maybe someone can use that too, or suggestions for improvement?
<?php
# V22.11.003
#
# Save another User ID via TV [tvSetCreatedBy] by using a ListBox: @SELECT `fullname`, `id` FROM `modx_user_attributes` WHERE `blocked`=0 ORDER BY `fullname` ASC
#
# System Events: 'OnDocFormRender', 'OnDocFormSave'
#
#
# $modx->setDebug(E_ALL & ~E_NOTICE);
# $modx->setLogLevel(modX::LOG_LEVEL_DEBUG);
if ($mode == MODX\Revolution\modSystemEvent::MODE_NEW) {
return;
}
switch ($modx->event->name) {
case 'OnDocFormRender':
$resourceID = $resource->get('id');
$authorID = $resource->get('createdby');
# Works with MODX 2.8.x without 'OnDocFormSave' event, but not with 3.0.1
# $tv = $modx->getObject('MODX\Revolution\modTemplateVar',array('name'=>'tvSetCreatedBy'));
# $tv->setValue($resourceID, $authorID);
# $tv->save();
# $modx->log(modX::LOG_LEVEL_ERROR, '[setCreatedBy -> resourceID + authorID]'.' : '.$resourceID.' + '.$authorID);
# Works with MODX 3.0.1
$page = $modx->getObject('MODX\Revolution\modResource', $resourceID);
if (!$page->setTVValue('tvSetCreatedBy', $authorID)) {
$modx->log(xPDO::LOG_LEVEL_ERROR, 'There was a problem saving your tvSetCreatedBy');
}
break;
case 'OnDocFormSave':
$createdbyID = $resource->getTVValue('tvSetCreatedBy');
if (!empty($createdbyID) && $resource->get('createdby') != $createdbyID) {
$resource->set('createdby', $createdbyID);
$resource->save();
}
break;
}
I tested your original plugin code (even without the namespaces) on MODX 3.0.1 and it worked for me.
I can’t reproduce any problems with modTemplateVar.
That code you’re showing is not the modTemplateVar class code (which works fine in MODX 3), so you haven’t modified the class. It’s the code of a plugin. It’s looks like you’ve added the namespace prefix to the modSystemEvent and modTemplateVar references as halftrainedharry suggested. I don’t see anything else wrong with your plugin.