Upon node save, I want to take the value of a text field that uses a custom input filter and save the HTML-ified final version (what the input filter renders) in a second field.
I have two text fields:
- Field text_main (which is never actually displayed, avoiding the performance penalty of rendering the field)
- Field text_main_display (which is displayed publicly)
The field text_main uses a custom input filter that is very slow to run. So, I want to get the rendered output of the field after running the input filter and save it to the field text_main_display.
/**
* Implements hook_ENTITY_TYPE_presave()
*/
function MYMODULE_node_presave(NodeInterface $entity) {
switch ($entity->getType()) {
case 'custom_page':
$processedResult = $entity->get('field_text_main')->view('custom_display');
// $processedResult is now a render array. How do I render this to HTML?
$entity->set('field_text_main_display', $processedResult);
break;
}
}
Reference: This post on how to render Drupal 8 fields