Skip to content

Instantly share code, notes, and snippets.

@em-piguet
Last active August 29, 2015 14:13
Show Gist options
  • Save em-piguet/804897e7ca133a312e9e to your computer and use it in GitHub Desktop.
Save em-piguet/804897e7ca133a312e9e to your computer and use it in GitHub Desktop.
Batch Find and Replace a value in a MODX TV
<?php
/*
Name:
findReplaceTV
Description :
This snippet is useful if you want to batch replace a string in multiple resources at once.
Usage :
[[!findReplaceTV ?
&parent=`91` // a container ID
&tvname=`image` // a tv name
&find=`assets/users` // look for this string
&replace=`assets/people` // replace with this one
]]
Version 1 : 2014.01.07@empiguet
*/
foreach ($modx->getIterator('modResource', array('parent' => $parent)) as $res) {
$docid = $res->get('id'); // ID loop
$page = $modx->getObject('modResource', $docid);
$tvvalue = $page->getTVValue($tvname);
$tvvalue = str_replace($find, $replace, $tvvalue);
$page->setTVValue($tvname, $tvvalue) ;
$res->save();
echo ('ID :' . $docid . ' => TV (updated) : ' . $page->getTVValue($tvname) .' <br/>' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment