Overview
In my previous article, I wrote about using resolvers in a transport package to modify resources. In this one, we'll see examples of using code in a resolver to update previously installed elements. In this one, we'll use chunks as an example. In the next one, we'll look at snippets, which are a special case because of the snippet properties.
As we learned in my previous article, you can use getContent()
and setContent()
to modify the content of all
elements except template variables (more on those in a bit). The process is very similar to what we did with resources
in the previous article.
This code to modify chunks will look a lot like the code from my previous article. We'll continue with our example of
changing the old class names, userData
and resourceData
to UserData
and ResourceData
. I've included the function
to check for the old class names, the class prefix code, and the search and replace terms and arrays, so you don't have
to look back to the previous article to see them. Obviously, these only need to appear once, even though you might be
changing more than one kind of object.
Here is the stuff from the top of the file.
function checkContent(array $terms, string $content) {
foreach ($terms as $term) {
if (strpos($content, $term) !== false) {
return true;
}
}
return false;
}
$base = MODX_CORE_PATH . 'components/classextender/model/';
$prefix = $modx->getVersionData()['version'] >= 3
? 'MODX\Revolution\\'
: '';
$uSearch = 'UserData';
$uReplace = 'userData';
$rSearch = 'ResourceData';
$rReplace = 'resourceData';
$searchArray = array(
$uSearch,
$rSearch,
);
$replaceArray = array(
$uReplace,
$rReplace,
);
The Chunk-specific Code
Here's the code that modifies the chunks:
/* Chunks to Check */
$chunks = array(
'ExtUserSchema',
'MyExtUserSchema',
'ExtResourceSchema',
'MyExtResourceSchema',
);
$count = 0;
$fixedChunkCount = 0;
foreach ($chunks as $chunk) {
$chunkObj = $modx->getObject($prefix . 'modChunk', array('name' => $chunk));
if ($chunkObj) {
$count++;
$content = $chunkObj->getContent();
if (checkContent($searchArray, $content)) {
$fixedChunkCount++;
$content = str_replace($searchArray, $replaceArray, $content);
$chunkObj->setContent($content);
$chunkObj->save();
}
}
}
The code above should be fairly self-explanatory. If the chunk exists, and if it contains either old class name, we update all instances of the class name and save it.
You may have noticed that in this article, and the previous one, we haven't done anything with the $count
or
$fixedChunkCount
variables. In my previous article, I talked about testing the code with a PHP script. In that code I
echoed the values of those variables.
You may also want to use them in messages to the user during the upgrade. Some extra developers use no messages at all. This makes for a very clean-looking console when you install or upgrade an extra. Others like to report on the progress of the installation sequence. This is less clean, but it can help in diagnosing problems with the installation process. A third option is to put in messages during development and testing, but remove them before releasing the package. It's a matter of personal preference.
In the case of the code above, if you wanted to report on the progress, you could put code like this at the end of the chunk section:
$modx->log(modX::LOG_LEVEL_INFO, 'Chunks Checked ' . $count);
$modx->log(modX::LOG_LEVEL_INFO, 'Chunks updated ' . $fixedChunkCount);
Coming Up
In my next three articles, we'll see how to update snippets, snippet default properties, and property sets.
Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.