V
V
Vasily Vyaznikov2017-03-17 10:17:57
PHP
Vasily Vyaznikov, 2017-03-17 10:17:57

How to display an empty tv field MODX REVO?

There is a simple snippet that gets the value of the tv fields:

$where = array (
    'contentid' => $resourceId, // id ресурса
    'tmplvarid' => $variableId // id поля
);

$tv = $modx->getObject('modTemplateVarResource', $where); // получение tv.объекта
$val = $tv->get('value'); // получение значения

return $val;

But, if these fields are not filled in the resource, then an error like this appears:
Fatal error: Call to a member function get() on null in /home/.../www/core/cache/includes/elements/modsnippet/6. include.cache.php on line 19
How can I get around this? It is necessary that in some cases the fields could be empty, i.e. empty.
MODX REVO 2.5.5
PHP 5.6

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Vyaznikov, 2017-03-17
@vyaznikov1991

thanks for the idea qork
Rewrote the snippet:

$where = array (
    'contentid' => $resourceId, // id ресурса
    'tmplvarid' => $variableId // id поля
);


$tv = $modx->getObject('modTemplateVarResource', $where); // получение tv.объекта

if(!$tv) return;


$val = $tv->get('value'); // получение значения

return $val;

D
display: block, 2017-03-17
@qork

if(!$resourceId || !$variableId) return;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question