Last active
August 29, 2015 14:13
-
-
Save em-piguet/804897e7ca133a312e9e to your computer and use it in GitHub Desktop.
Batch Find and Replace a value in a MODX TV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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