I’m building a plugin that needs to find for certain TV inside a resource to do what the snippets are build to do!.
It’s working fine, the problem is if the TV exist but it’s empty is not doing anything because obviously I’m getting the TV content not checking if it has been assigned to the resource.
//Query all template-ids with the TV (ID = 1) assigned to it.
$c = $modx->newQuery('modTemplateVarTemplate', array('tmplvarid' => 1)); //change '1' to the correct TV-ID
$c->select($modx->getSelectColumns('modTemplateVarTemplate', '', '', array('templateid')));
$templates = $c->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
//Use these template-ids in your call to getCollection
$documents = $modx->getCollection('modResource',array('template:IN' => $templates, ...));
Probably the best way is a direct query to the tables on a single sentence, yet I won’t necessarily show if a TV is used in a resource, for example, you could have a TV on a resources just using the default value, and it would show as not used, as well as a TV with assigned value, but not actually used anywhere, showing that it’s used.
At the end I’ve found 2 possible ways to this challenge, one, is to use a parser and before procesing modx tags, check if the TV is present on any of the resources elements, or the best one, which I went with, was to have a custom CMP where I can explicitly add a TV to a chunk or resource, and have the matching info on a custom table, I did this to overcome the shown of non used TVs on a resources to reduce the amount of “noise” my users were getting, like seeing 10 potential TVs, when only one was actually used at a given point
This is the right output!
I’m working local anyway: AMPPS V41 + ModX 2.8.3 + PHP 7.4
@camicase82 Thanks man for your help, I think the approach is very good, I like the idea of find TV’s in the code to hide fields in the manager, but is it not easier just create a template only with the necessary TV’s? Cheers mate!
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”.