MODX 3.0.1: Error if list createdby via @SELECT

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

Therefore my appropriate plugin does not work:

<?php
# System Events, check the 'OnDocFormRender' event
#
# $modx->setDebug(E_ALL & ~E_NOTICE);
# $modx->setLogLevel(modX::LOG_LEVEL_DEBUG);

if ($mode == modSystemEvent::MODE_NEW) {
   return;
}

  $resourceID = $resource->get('id');
  $authorID = $resource->get('createdby');

$tv = $modx->getObject('modTemplateVar',array('name'=>'tvCreatedBy'));
$tv->setValue($resourceID, $authorID);
$tv->save();

return;

Is this a MODX bug? Does anyone know how to fix it?

mySystem:

  • MODX 3.0.1 installation on Linux Server (Ubuntu)
  • PHP: 8.1.11
  • mySQL: 8.0.30

Thanks Jo

OK, sorry, meanwhile I found this on github: github.com/modxcms/revolution/pull/16286
By this fix the error message is gone, now I have to check my plugin.

Maybe try adding the namespace to the classnames:

MODX\Revolution\modSystemEvent instead of modSystemEvent
(MODX\Revolution\modTemplateVar instead of modTemplateVar)

@halftrainedharry Thanks for the hint, now I get his error from my plugin:

(DEBUG @ .../httpdocs/modx/core/src/Revolution/modLexicon.php : 526) Language string not found: "{$username}"

Do you know what this means?

Plugin:

<?php
# V22.11.002
# System Events, check the 'OnDocFormRender' event
#
$modx->setDebug(E_ALL & ~E_NOTICE);
$modx->setLogLevel(modX::LOG_LEVEL_DEBUG);

if ($mode == MODX\Revolution\modSystemEvent::MODE_NEW) {
   return;
}

  $resourceID = $resource->get('id');
  $authorID = $resource->get('createdby');

$tv = $modx->getObject('MODX\Revolution\modTemplateVar',array('name'=>'tvCreatedBy'));


$tv->setValue($resourceID, $authorID);
$tv->save();

return;

I don’t think this error is from your plugin.

Some code tries to load the text from the lexicon with the key {$username}. But this key doesn’t exist in the lexicon.

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.

I can’t reproduce any problems with modTemplateVar.

Hi, tks for your reply. In my case the user with ID1 is always selected in the TV list. Strange, I use this since MODX 2.2.x without problems.

Why exactly do you save the createdby value in a separate TV?

Homepage for a small town of about 10 writers. Most are not so fit on the PC and then someone else writes reports on their behalf.

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.