Empty a TV in Resources under Certain Parent Folders

Changing the value of all TVs for resources under certain parent folders.


In the previous article, we looked getting the path of a Media Source. In this one, we'll look at how to empty a TV for resources under selected parent folders.


MODX logo

The Problem

MODX Forum user trofey asked for a way to "blank" particular TVs for resources under specific parent folders. This can be done with a fairly simple utility snippet. The snippet gets the modTemplateVarResource object for the TV of each resource, replaces the value with an empty string (''), and saves it.


The Solution

First, create a resource called FixTVs with this content:

<h2>Fixing TVs</h2>

[[!FixTVs]]

Next, create a new snippet called FixTVs and paste in the code below. To blank the TVs, just view the resource.


The Code

/* FixTVs Snippet */
$tvId = 12; // This is the TV that will be emptied
$parents = '12,33,56';  // children of these parents will have the TV emptied
$parents = explode(',', $parents);
$count = 0;

foreach ($parents as $parentId) {
    /* Get the children */
    $docs = $modx->getCollection('modResource',
        array('parent' => $parentId));

    foreach ($docs as $doc) {
       /* Get the modTemplateVarResources */
       $docId = $doc->get('id');
       $criteria = array(
           'contentid' => $docId,
           'tmplvarid' => $tvId,
       );

       $tvr = $modx->getObject('modTemplateVarResource',
           $criteria);

       /* Blank the value of the TV */
       if ($tvr) {
           $count++;
           $tvr->set('value', '');
           $tvr->save();
       }
    }
}

return "Finished -- corrected " . $count . ' TVs';

How it Works

The ID of the TV and the comma-separated list of the IDs of the parents are set at the top of the code. The parent IDs are in parentheses next to the names of the parent folders in the Resource tree. The ID of the TV is in parentheses next to the name of the TV in the Template Variables section of the Elements tree. We also initialize the $count variable so we can keep track of the number of fixed TVs and report it at the end of the process.

The explode() line creates a PHP array where each member is a parent ID.

We loop through the parents one at a time and get each parent's children with a call to getCollection(). The child resource objects are returned from getCollection() as an array and placed in the $docs variable.

Next, we loop through the $docs and use getObject() to get the modTemplateVarResource object, which contains the value of the TV for the current resource being processed. That's placed in the $tvr variable.

The if ($tvr) line is important. If the default value of the TV is empty and the TV is already blank, there will be no modTemplateVarResource object. If we try to modify the result of the getObject() call, the code will crash because that object doesn't exist. Using the if statement makes the code skip the next few lines (the ones that modify the value) if that happens.

Once we know the $tvr object exists, we simply change its value field to an empty string and save it.

At the end of the code, we report how many TVs have been modified.


Coming Up

In the next few articles, we'll look at some ways to find the current year in a snippet and use it to control forwarding and page content.



Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)