MODX Community

Batch Template Variable Values

Hello, I got a TV “Date” with different dates in more than 100 different pages.
I need to change all of them to one specific date, the Batch extra is not doing TV’s how can I do that?.

Thank you very much!

If you can wait, I have a blog post that should be published in a week or two at Bob’s Guides that has more detail. It’s a utility snippet that empties all TVs under a certain parent.

For now, here’s the code for the snippet, slightly modified for your use case:

$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', ''); // put your new date between the single quotes.
           $tvr->save();
       }
    }
}

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

Be sure to set the $tvId and $parents variables correctly.
The $parents variable can be set to a single parent if that works for you.

Create a snippet called FixTVs with the code above.
Put this tag as the content of a new resource and view the resource.

[[FixTVs]]

Note: TVs with no value set will not be changed.

1 Like

Bob I’m getitng this: Finished – corrected 0 TVs

I put my data into the code:


$tvId = 55; // This is the TV that will be emptied
$parents = ‘22,325,29,326,41’; // children of these parents will have the TV emptied

$count++;
$tvr->set(‘value’, ‘2020-06-30’); // put your new date between the single quotes.
$tvr->save();

The TV ID is right and all this parents: ‘22,325,29,326,41’ contain children with the TV 55…
what might be wrong?

thanks Bob.

1 Like

I can’t see anything wrong. Could the Resource’s TVs have been set to the default value for that TV?

This is from Sean Kimball

<?php

$docID = $modx->resource->get('id'); //get the page id

$tvId = 9; //the tv id I want to change

$tv = $modx->getObject('modTemplateVar',$tvId); // get the obj.

$tv->setValue($docID, $tv->getValue($docID) + 1 ); // set it's new value

$tv->save(); // save the new value

Well it’s working Bob.
there was a tiny misspelling in the code:

$parents as $parentId replace with $parents as $parent
because is being called like: ‘parent’ => $parent

see the error:

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

and here is the working code:

$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 $parent) {
    /* Get the children */
    $docs = $modx->getCollection('modResource', array('parent' => $parent));

    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', ''); // put your new date between the single quotes.
           $tvr->save();
       }
    }
}

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

Thanks Bob, really appreciate your help!

1 Like

Sorry about that. :woozy_face:

I’m glad you got it working.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.