Jump to main content Jump to doc navigation

Read and write TV values through the resource API; the examples below rely on the xPDO method getObject and related functions.

getTVValue

string|null getTVValue (str|integer $tv_name OR ID of TV)

See core/src/Revolution/modResource.php (methods getTVValue() and setTVValue())

getTVValue Usage

Read the TV bio from resource 123:

$page = $modx->getObject('modResource', 123);
return $page->getTVValue('bio');

getTVValue reads the resource cache when a cached value exists. Saving a resource normally clears that cache, but setTVValue below does not, so a value it just wrote may stay invisible to getTVValue. When you need the current data, bypass the cache and read the TV value record with getObject:

$tvr = $modx->getObject('modTemplateVarResource', array(
  'tmplvarid' => $tvId,
  'contentid' => $resourceId
));
if ($tvr) {
  return $tvr->get('value');
}
else {
  $tv = $modx->getObject('modTemplateVar', $tvId);
  if ($tv) return $tv->get('default_text');
}
return '';

setTVValue

Use setTVValue to save a new value to a TV. Unlike some other xPDO API methods, this method stores values to the database immediately, so you do not need to invoke a separate call to a save() method. This method does not clear the resource cache.

boolean setTVValue (str|integer $tv_name OR ID of TV, string $value)

If the new value equals the TV’s default_text, setTVValue does not write a row — it removes the existing modTemplateVarResource record instead, so the TV falls back to its default. An immediate getTVValue after setTVValue may still return a cached value.

setTVValue Usage

$page = $modx->getObject('modResource', 123);
if (!$page->setTVValue('bio', 'This is my new bio...')) {
    $modx->log(xPDO::LOG_LEVEL_ERROR, 'There was a problem saving your TV...');
}

See Also

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
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • A. Moreno
  • Alex
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

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

Learn more