yj-t
2/3/2016 - 9:48 PM

OnDocFormSave, process MODX Resource content and save to a TV for search indexing.

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);