Getting Multiple Parent Fields in MODX

MODX logoIn a previous article, I showed a way to display a particular field from the current resource’s parent. Sometimes, though, you might want to display information from several of the parent’s fields. It would be pretty wasteful to run a GetParentField snippet multiple time and have each instance retrieve the same resource. In this article I’ll show you how to use a Tpl chunk to display whatever parent info you want with a single snippet call.

The Tpl Chunk

Here’s an example of a chunk you might use to display a parent’s fields (let’s call it ParentInfoTpl):

[code language=”html”]
<div id="parent_info">
<h3>Parent Info</h3>
<p>Pagetitle: [[+pagetitle]]</p>
<p><a href="[[~[[+id]]]]">Link to Parent</a></p>
<p>Summary: [[+introtext]]</p>
<p>Published On: [[+publishedon]]</p>

</div>
[/code]

Any and all of the parent’s fields will be available for use with placeholder tags like the ones above in the Tpl chunk, but you have to use the exact field names, which are listed here. You can use CSS to style the output any way you like, and you can use output modifiers to format date fields, truncate output, and handle empty fields.

The Tag

Put this tag where you want the output to appear. Be sure the &tpl property holds the exact name of the Tpl chunk you create:

[code language=”html”]
[[!ParentInfo? &tpl=`ParentInfoTpl`]]
[/code]

The Snippet Code

Here’s the code of our ParentInfo snippet:

[code language=”php”]
<?php
/* ParentInfo snippet */
$output = ”;
$tpl = $modx->getOption(‘tpl’, $scriptProperties, ‘ParentInfoTpl’, true);
$parent = $modx->getObject(‘modResource’, array(‘id’ => $modx->resource->get(‘parent’));
if ($parent) {
$fields = $parent->toArray();
$output = $modx->getChunk($tpl, $fields);
}
return $output;
[/code]

The snippet first uses $modx->getOption() to get the name of the Tpl chunk from the $scriptProperties array. The third argument to getOption tells MODX to use ParentInfoTpl as the name if the &tpl property is not set. The fourth argument (true) tells MODX to use ParentInfoTpl if the &tpl property is set, but empty.

Next, the snippet tries to get the parent object with $modx->getObject(). If it fails (which it will for resources at the root of the tree since they have no parent), the $output variable will not be modified. It will remain as an empty string since we set it to that at the top of the snippet. The snippet will return an empty string and the snippet tag will be replaced with nothing.

If the parent object is retrieved successfully, the snippet creates the $fields array with $parent->toArray(). This is a bit of xPDO magic that creates a PHP associative array with all the field names and field values from the parent. It would look something like this:

[code language=”php”]
array(
‘id’ => ’12’,
‘pagetitle’ => ‘Dogs of the World’,
‘published’ => ‘1’,
‘introtext’ => ‘The many varieties of dogs around the world’,
/* etc. */
);
[/code]

 

Finally, the snippet calls $modx->getChunk() with the name of the Tpl chunk as the first argument, and the $fields array as the second argument. MODX fetches the chunk and replaces every placeholder in the chunk with the value of its member in the $fields array. The 'pagetitle' placeholder, for example, will be replaced with ‘Dogs of the World’.

Be sure all your placeholders begin with a plus sign (+), not an asterisk (*). If you use an asterisk, getChunk() will not replace the tags (since they are not placeholder tags). They will remain in the text and when the page is rendered, MODX will replace them with the values from the current resource rather than the parent.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *