Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active December 21, 2017 12:13
Show Gist options
  • Save sepiariver/a7d6fdc89e2050334086 to your computer and use it in GitHub Desktop.
Save sepiariver/a7d6fdc89e2050334086 to your computer and use it in GitHub Desktop.
OnDocFormSave, process MODX Resource content and save to a TV for search indexing.
<?php
/*
* SaveProcessedContent
* @author @theboxer
* @comments @sepiariver
*
* GPL, no warranties, etc.
*
* USAGE:
* Enable 'OnDocFormSave' event in Plugin properties, and supply a valid "tvName" value.
*
*/
// Escape conditions
if (($modx->context->get('key') !== 'mgr') || $modx->event->name !== 'OnDocFormSave') return;
// Options
$tvName = $modx->getOption('tvName', $scriptProperties, 'processedContent', true);
$processTemplate = $modx->getOption('processTemplate', $scriptProperties, false);
// Save these for later
$currentResource = $modx->resource;
$currentResourceIdentifier = $modx->resourceIdentifier;
$currentElementCache = $modx->elementCache;
// Assign values from event parameters
$modx->resource = $resource;
$modx->resourceIdentifier = $resource->get('id');
$modx->elementCache = array();
// Parse
$resourceOutput = ($processTemplate) ? $modx->resource->process() : $modx->resource->content;
$modx->parser->processElementTags('', $resourceOutput, true, false, '[[', ']]', array(), $maxIterations);
$modx->parser->processElementTags('', $resourceOutput, true, true, '[[', ']]', array(), $maxIterations);
// Replace previous values
$modx->elementCache = $currentElementCache;
$modx->resourceIdentifier = $currentResourceIdentifier;
$modx->resource = $currentResource;
// Save to TV
$resource->setTVValue($tvName, $resourceOutput);
@theboxer
Copy link

theboxer commented Feb 4, 2016

Line #31 $modx->resource->content can be replaced with $modx->resource->process() than the processed string will contain also processed template, not only content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment