Collections: renderer for tvLabel possible?

I use Collections and have a listbox TV “names” with names like this:

Name 1==01||Name 2==02|| and so on

Creating the columns I can set a column for the names but see as content only the numbers. How I can get the names visible in column (like with the output modifier names:tvLabel)?

Thanks for help.

You can create a custom snippet renderer.
Create a new snippet ‘my_renderer’, copy the code from the tvLabel output modifier and make some adjustments.

The code should look something like this. (Change the name of the listbox TV in the code)

<?php
$name_tv = 'my_listbox_tv';
$output = $modx->getOption('value', $scriptProperties, '');

$tv = $modx->getObject('modTemplateVar', array('name' => $name_tv));
if (!$tv) {
    break;
}
$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;

Then in the Collections view, update the column and add ‘my_renderer’ into the “Snippet Renderer” field.

Thanks, tried it but it failed. In the resource with the collection no sub-resources are visible anymore.

I understand that I need a custom renderer but can’t do this itself because of less knowledge.

Does this code work?

<?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;
1 Like

Wow! That’s it!
Many thanks for your quick help.

And the best: it works universal for all TVs with a listbox. Great improvement for my collections.

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”.