In a plugin code, is there any way to tell Fred to find specific elements in the content and do something to them?
I suppose with ‘elements’ you mean Fred-elements and not html-elements.
The problem with Fred is, that the page-content is saved twice. Once unstructured in the field “content” and once structured in the field “properties”.
You can access the entire page content in the plugin with $resource->getContent()
, but you probably have to find the right part to change with a regular expression.
switch ($modx->event->name) {
case 'FredOnBeforeFredResourceSave':
$content = $resource->getContent(); //read content
//make changes to $content
$resource->setContent($content); //set new content
break;
}
You can access the Fred-elements individually in the field “properties” with $resource->getProperty()
and code like this:
switch ($modx->event->name) {
case 'FredOnBeforeFredResourceSave':
$data = $resource->getProperty('data', 'fred');
$elements = &$data['content'];
foreach($elements as &$element) {
$name_element = "data-fred-name-of-element";
if (array_key_exists($name_element,$element['values'])){
$value = &$element['values'][$name_element]['_raw']['_value'];
//make changes to $value
}
}
//$resource->setProperty('data', $data, 'fred'); //set new value
break;
}
But a change in the field “content” doesn’t change anything in the field “properties” and vice versa. I don’t know, if there is a way to regenerate the “content” from the “properties” in PHP, but I didn’t find anything.