In previous articles, we looked at a very fast and efficient xPDO method for getting the value of a single field from a MODX object using the getValue
method of the xPDO object. For example, this code will get the introtext
field from a Resource with the pagetitle
“Products”:
$query = $modx->newQuery('modResource', array(
'pagetitle' => 'Products',
));
$query->select('introtext');
$intro = $modx->getValue($query->prepare());
In this article, we’ll look at using it to get the content fields of objects like Resources, Chunks, Templates, Snippets, Plugins, and the value field of TVs.
The Code
The main code of our method is similar, but as we learned in an earlier article, you need to know the “name” of the content field. If you have the object, you can get its content with $object->getContent()
, but with our method, we’re not actually getting the object, just the value of a single field. If you’re getting the object by name, as you often are, you also have to know the name of the “name” field.
It’s handy, sometimes, to get the content of a particular object. You might want the main content of a Resource, the PHP code of a Snippet or Plugin, the raw contents of a Chunk, the HTML code of a Template, or the value of a TV for a particular Resource.
Here is our method, modified to get the content field of the various objects. Remember that this will get the raw content and any tags it contains will not be processed in any way.
Resources
/* Make it run in either MODX 2 or MODX 3 */
$prefix = $modx->getVersionData()['version'] >= 3
? 'MODX\Revolution\\'
: '';
$query = $modx->newQuery($prefix . 'modResource', array(
'pagetitle' => 'Products',
));
$query->select('content');
$content = $modx->getValue($query->prepare());
Snippets
$prefix = $modx->getVersionData()['version'] >= 3
? 'MODX\Revolution\\'
: '';
$query = $modx->newQuery($prefix . 'modSnippet', array(
'name' => 'MySnippet',
));
/* 'snippet' is "content" field containing
the snippet code */
$query->select($prefix . 'snippet');
$code = $modx->getValue($query->prepare());
Plugins
$prefix = $modx->getVersionData()['version'] >= 3
? 'MODX\Revolution\\'
: '';
$query = $modx->newQuery($prefix . 'modPlugin', array(
'name' => 'MyPlugin',
));
$query->select('plugincode');
$code = $modx->getValue($query->prepare());
Templates
$prefix = $modx->getVersionData()['version'] >= 3
? 'MODX\Revolution\\'
: '';
$query = $modx->newQuery($prefix . 'modTemplate', array(
'templatename' => 'MySpecialTemplate',
));
$query->select('content');
$code = $modx->getValue($query->prepare());
Chunks
$prefix = $modx->getVersionData()['version'] >= 3
? 'MODX\Revolution\\'
: '';
$query = $modx->newQuery($prefix . 'modChunk', array(
'name' => 'MyChunk',
));
$query->select('snippet');
$code = $modx->getValue($query->prepare());
With Chunks, you can also get the content with $modx->getChunk('MyChunk')
, but our method will be a little faster because it eliminates an unnecessary function call.
One good use of this method is to store data in Chunks. Since that data will usually not contain placeholders or other tags, it won’t need processing. For example, you could put the options for a drop-down list in a Chunk as a comma-separated list and create the HTML for the drop-down list on the fly after retrieving the Chunk’s content. This will be very fast, and it will allow users to change the options just by editing the Chunk. Using this method will be much faster than using a TV to store the options.
TVs
This method is not as useful for TVs, since TV data is actually stored in two separate tables in the database. The value of the TV for a particular Resource is stored in one table, the TV’s default value is stored in another table. Another problem is that you often want the processed value for the TV, say for a date or image TV. The getValue()
method will only give you the raw value of the TV.
The only practical time to use this method is when, 1) you know the ID of the TV; 2) you know the ID of the Resource you want the TV value for; and 3) you want the raw value of the TV. If it’s a text TV, this is fine. Here’s the method:
$prefix = $modx->getVersionData()['version'] >= 3
? 'MODX\Revolution\\'
: '';
$query = $modx->newQuery($prefix . 'modTemplateVarResource', array(
'tmplvarid' => $tvId,
'contentid' => $resourceId,
));
$query->select('value');
$value = $modx->getValue($query->prepare());
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.