I'm using a hook_preprocess function (hook_preprocess_entity_print
) to alter the content within a paragraph field prior to rendering (replacing some strings based on a regex). This works great when I'm working on the node, but not when I'm trying to do the same on a revision of the node.
This works for the node:
/**
* Implements hook_preprocess_entity_print().
*/
function mymodule_preprocess_entity_print(&$variables) {
$route = \Drupal::routeMatch();
$entity_id = $route->getParameter('entity_id');
$node = \Drupal::entityTypeManager()->getStorage('node')->load($entity_id);
$type = $node->getType();
if ($type === 'product_child') {
foreach ($node->get('field_submittal_wysiwyg') as $paragraph) {
$type = $paragraph->entity->getType();
switch ($type) {
case 'submittal_2_col':
$field_submittal_text = $paragraph->entity->field_submittal_text_1->value;
$field_submittal_text = mymodule_token_replace($field_submittal_text, $variables);
$paragraph->entity->field_submittal_text_1->value = $field_submittal_text;
break;
}
}
}
}
When I decided to make these same replacements work when rendering a revision of the node, I tried this first:
/**
* Implements hook_preprocess_entity_print().
*/
function mymodule_preprocess_entity_print(&$variables) {
$route = \Drupal::routeMatch();
$entity_id = $route->getParameter('entity_id');
$revision_id = $route->getParameter('revision_id');
$node_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id);
$type = $node_revision->getType();
if ($type === 'product_child') {
foreach ($node_revision->get('field_submittal_wysiwyg') as $paragraph) {
$type = $paragraph->entity->getType();
switch ($type) {
case 'submittal_2_col':
$field_submittal_text = $paragraph->entity->field_submittal_text_1->value;
$field_submittal_text = mymodule_token_replace($field_submittal_text, $variables);
$paragraph->entity->field_submittal_text_1->value = $field_submittal_text;
break;
}
}
}
}
I can confirm that I'm getting the correct values for the draft revision in $field_submittal_text
. But when I try to set the value of the paragraph, it works only for the published node.
I figured I must not be loading the paragraph revisions referenced within the node revisions. So I then tried to address that:
/**
* Implements hook_preprocess_entity_print().
*/
function mymodule_preprocess_entity_print(&$variables) {
$route = \Drupal::routeMatch();
$entity_id = $route->getParameter('entity_id');
$revision_id = $route->getParameter('revision_id');
$node_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id);
$type = $node_revision->getType();
if ($type === 'product_child') {
$field_submittal_wysiwyg = $node_revision->field_submittal_wysiwyg->referencedEntities();
foreach ($field_submittal_wysiwyg as $paragraph) {
$paragraph_type = $paragraph->getType();
switch ($paragraph_type) {
case 'submittal_2_col':
$field_submittal_text = $paragraph->field_submittal_text_1->value;
$field_submittal_text = mymodule_token_replace($field_submittal_text, $variables);
$paragraph->field_submittal_text_1->value = $field_submittal_text;
break;
}
}
}
}
Again, I can confirm that I'm getting the correct values for $field_submittal_text
based on the revision that I'm trying to load. But the last line before the break isn't changing what is rendered.
What do I need to do to set the value of the paragraph field for a revision before rendering?
ETA: I'm attempting to replace some product attribute values in several paragraph WYSIWYG fields with values from another field, based on a product_id sent in through the route parameters. So I need access to multiple fields in order to do these replacements.