N
N
Natalia Kazakova2016-07-06 22:57:05
MODX
Natalia Kazakova, 2016-07-06 22:57:05

How to organize the display of a value of the type "list of resources" in the Collections snippet?

Good afternoon! The object category is set to the material type Collection. In the columns I indicate the TV fields. Those that have input type "list of resources" or checkboxes (selects) with possible values ​​of type value1==1||value2==2||.. are displayed as numbers - for a list of resources, this is the resource id or keys for a multiselect respectively.
Who worked with this snippet, tell me how to write a friendly output of meaningful values?
I myself work with this snippet for the first time, I did not find such a solution in the open spaces of the network.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Natalia Kazakova, 2016-07-07
@kazakova-net

The solution was found independently.
After digging into the documentation, I found out that Collections provides the ability to process a value with render snippets.
For my needs, I wrote two types of snippets.
If in the Collections view you need to display the value of a field with the input type "list of resources" and you need to display the pagetitle of the resources, and not their id, you can use the following code:

<?php
$value = $modx->getOption('value', $scriptProperties, '');
$res = $modx->getObject('modResource',$value);
$output = $res->get('pagetitle');
return $output;

To get a meaningful value for a field with a checkbox input type with possible values ​​value1==1||value2==2||.. the following snippet-render came in handy:
<?php
$inputValue = $modx->getOption('value', $scriptProperties, '');

$resourceQuery = new xPDOCriteria($modx, "SELECT * FROM {$modx->getTableName('modTemplateVar')} WHERE name = 'objectTip'");
$tv = $modx->getObject('modTemplateVar', $resourceQuery);
$elements = $tv->get('elements');
$options = explode('||', $elements);
$lookup = array();
  
foreach ($options as $o) {
  list($name, $value) = explode('==', $o);
  $lookup[$value] = $name;
}

$output = $lookup[$inputValue];

return $output;

Since I just started studying xPDO and my knowledge is not deep, I do not exclude that this snippet can be made universal for any TV field with a checkbox input type (similarly, select), and I will be glad if someone in the comments tells me how to do it. I wrote a separate snippet for each field and passed its name to the request. Since in this way I needed to process only 2 fields, I settled on this solution.
Perhaps someone will need it.

S
SergNewman, 2019-11-28
@SergNewman

5ddf6c74ab676041074988.png
What am I doing wrong? I still get the ID :-(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question