3.0.3: Collections view fails with custom snippet renderer

I use a listbox tv with this pattern in a collection:
first choice==1||second choice==2||and so on==3

To show the TV labels instead of the numbers in the collections view I use a custom snippet renderer that works fine with version 3.0.1

This is the renderer code:

<?php
$name_tv = $modx->getOption('column', $scriptProperties, '');
$name_tv = preg_replace('/^tv_/', '', $name_tv);
$output = $modx->getOption('value', $scriptProperties, '');

$tv = $modx->getObject('modTemplateVar', array('name' => $name_tv));
if (!$tv) {
    return 'no TV found!';
}
$o_prop = $tv->get('output_properties');
$options = explode('||', $tv->get('elements'));
$lookup = array();
foreach ($options as $o) {
    list($name, $value) = explode('==', $o);
    $lookup[$value] = $name;
}
if (isset($o_prop['delimiter'])) {
    $delimiter = $o_prop['delimiter'];
    $values = explode($delimiter, $output);
} else {
    $delimiter = '';
    $values = array($output);
}
$return_values = array();
foreach ($values as $v) {
    $return_values[] = $lookup[$v];
}
$output = implode($delimiter, $return_values);
return $output;

The collections view shows this error (translated from German):
A server error prevented the contents of this table from loading. For more information, see your browser's console, the MODX Manager log and/or the PHP server log.

The modx error log is empty. The developer tools show this error:

Failed to load resource: the server responded with a status of 500 ()
/connectors/index.php:1

500
modx.grid.js?mv=303pl:205 
onStoreException @ modx.grid.js?mv=303pl:205

I tried to overwrite the modx.grid.js from revolution/modx.grid.js at c553c47bf25a6d21e1995648bf1b21664420ea76 · modxcms/revolution · GitHub but it doesn’t help.

Any ideas?

I don’t think it is a problem with MODX 3.0.3.
Did you change the PHP version as well?

I tried to reproduce the issue with MODX 3.0.3 and different PHP versions (7.4, 8.0, 8.1), but everything worked correctly.

Did you check the server error log? With an error 500, there is probably an error message there.

Why??

I have two installations with the same configuration in this part. PHP version is the same: 8.0.2x
One has MODX version 3.0.1 which works fine without any problems. the other has the 3.0.3 and there is the problem. The collections are identical, also the snippet for the renderer.

This is the server error message:
AH01071: Got error 'PHP message: PHP Parse error: syntax error, unexpected token "<", expecting end of file in [/var/www/vhosts/***/core/cache/includes/elements/modx/revolution/modsnippet/50.include.cache.php](https://***/smb/file-manager/code-editor?currentDir=***core%2Fcache%2Fincludes%2Felements%2Fmodx%2Frevolution%2Fmodsnippet&subscriptionId=1531&file=50.include.cache.php&redirect=%2Fsmb%2Flog-file%2Fbrowser%2Fid%2F1534) on line 2', referer: https://***/manager/?a=resource/update&id=16

Because of the error message in developer tools:

500
modx.grid.js?mv=303pl:205 
onStoreException @ modx.grid.js?mv=303pl:205

What is the snippet with the ID = 50? Your renderer snippet?

Yes, it is.

That’s crazy: the cache file is a bit different from the original snippet. The snippet code can be found above.
And this is the code in the cache file:

<?php
<?php
$name_tv = $modx->getOption('column', $scriptProperties, '');
$name_tv = preg_replace('/^tv_/', '', $name_tv);
$output = $modx->getOption('value', $scriptProperties, '');

$tv = $modx->getObject('modTemplateVar', array('name' => $name_tv));
if (!$tv) {
    return 'no TV found!';
}
$o_prop = $tv->get('output_properties');
$options = explode('||', $tv->get('elements'));
$lookup = array();
foreach ($options as $o) {
    list($name, $value) = explode('==', $o);
    $lookup[$value] = $name;
}
if (isset($o_prop['delimiter'])) {
    $delimiter = $o_prop['delimiter'];
    $values = explode($delimiter, $output);
} else {
    $delimiter = '';
    $values = array($output);
}
$return_values = array();
foreach ($values as $v) {
    $return_values[] = $lookup[$v];
}
$output = implode($delimiter, $return_values);
return $output;
return;

The first and last line are too much. But why?

The return; line at the end is normal.
But two <?php lines at the top not. Are you sure there is only 1 <?php in your snippet code?

Yes, I’m sure. Checked it multiple times.

There is another entry in the server error log – seems I should ask at first the provider support what that means. (AH01630: client denied by server configuration)

NOPE, that has nothing to do with this problem. The error message is:
AH01630: client denied by server configuration: [/var/www/vhosts/***/core/docs/changelog.txt]

Have you tried, removing the line <?php in the snippet completely and then saving the snippet.
It should add <?php automatically.

(Are there maybe some spaces at the end of the line <?php ?)

MODX makes these requests to the changelog.txt just to test if the core folder is protected.

Removed the first line and saved the snippet. Cleared the cache – checked the cache: is empty.
Called the resource again: double <?php in the cached file (and no extra spaces in the first lines).

A very crazy solution …

Instead of deleting the first line in the snippet (the <?php) I commented it out. Now the collection view works fine and the cached file looks this at first lines:

<?php
//<?php
$name_tv = $modx->getOption('column', $scriptProperties, '');
$name_tv = preg_replace('/^tv_/', '', $name_tv);
…

Other snippets outside the collection extra work without problems WITH the first line.
I think there is a conflict between MODX 3.0.3 and the Collections extra.

This is more a hack than a solution.

The line <?php should get removed when the snippet is stored to the database. In the code here:

So in the database table modx_site_snippets → column “snippet” there should be no <?php at the start of the code.

The real question is, why this doesn’t seem to work for this snippet?


No, Collections just uses the normal runSnippet() function. There is nothing special about a snippet that is used as a Collections renderer.

Hmm … I’m not a programmer. I see only one difference: the correct working 3.0.1 and the problematic 3.0.3, both with the same PHP version, same collection extra, same collection view and same renderer snippet.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.