Preface: I don't often work with Drupal, so I may not be using the correct terminology or fully understand the stack. Please bear with me, I'll do my best to be as clear as possible.
I want to filter the rendered output of the primary "Body" field on a "Basic Page" (or "Article," any content type really) to add some classes to various elements. This would be the field labeled "Body" at /node/add/page
. I'd like to achieve this using DOMDocument
in my .theme
file, but I'm having trouble figuring out what the correct "hook" is to tie in to.
So ultimately the goal would be a function something like the following (which is partially psuedo code):
function my_theme_body_alter(&$content) {
$DOM = new DOMDocument();
$DOM->loadHTML(mb_convert_encoding($content, "HTML-ENTITIES", "UTF-8"), LIBXML_HTML_NODEFDTD);
$anchors = $DOM->getElementsByTagName("a");
foreach ($anchors as $anchor) {
$existing_classes = $anchor->getAttribute("class") ? $anchor->getAttribute("class") : "";
$anchor->setAttribute("class", "button {$existing_classes}");
}
$content = $DOM->saveHTML($content);
return $content;
}
To be absolutely clear, I want to modify the output (on the front end) of this field:
What is the name of the hook I'm looking for, or how can I locate it myself?
a
tags found, which may not be the desired outcome.