Jump to main content Jump to doc navigation

What is getResourceField?

getResourceField is a simple snippet which can be used to display a single field, including template variables, of a different resource for MODX Revolution.

History

getResourceField was first released on September 16th, 2010 by paulmerchant, co-authored by Shaun McCormick.

Download

The snippet can be retrieved through the Package Manager, or downloaded manually from the Repository.

Usage

The minimum tag is as follows:

[[getResourceField]]

This will output the pagetitle of the current resource.

getResourceField parameters

Name Description Default value
id The resource ID to get the field from. $modx->resource->get('id')
field The field or template variable name to return. ?pagetitle
isTV When set to 1 or true, the field is considered to be a template variable. false
processTV When set to 1 or true, the template variable will be processed according to its output settings. false
default The value (string) to return if the field is not found or is empty.

Examples

Return the pagetitle from the resource with id 123:

[[getResourceField? &id=`123`]]

Return the processed TV with name myTV of the parent resource, and if it is empty output 'Sorry, no data available':

[[getResourceField? &id=`[[*parent]]` &field=`myTV` &processTV=`1` &default=`Sorry, no data available`]]

Return the introtext field of the ultimate parent (note that this requires the UltimateParent snippet to be installed):

[[getResourceField? &id=`[[UltimateParent]]` &field=`introtext`]]

Errors

One error that this Snippet is prone to is deceptively subtle: you can end up with a redirect loop. Consider the example where you've defined a template variable on your homepage, e.g. [[*featured_article]], and you use this in your getResourceField Snippet call:

 [[getResourceField? &id=`[[*featured_article]]` &field=`content`]]

If that TV is not set, the default behavior is to use the id of the current page. If your Snippet call is in the content of your page and you are requesting the content as your field, your Snippet can go down the rabbit hole and infinitely loop on itself, causing your page request to time out with too many redirects.

Bottom line: if the Snippet call is inside the same field that you are requesting (e.g. content), then make sure the id parameter never points to the current page.

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • Chris Fickling
  • CrewMark
  • deJaya
  • Lefthandmedia
  • Murray Wood
  • eydolan
  • Following Sea
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • A. Moreno
  • Nick Clark
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

$301 per month—let's make that $500!

Learn more