How can I add classes to the rendered "body" field on a content type?
note:I have tested it in my machine and it works 100%



the Hook_preprocess_node is gonna be executed before the rendering of any node of any content-type, you have to apply your logic here but be sure to apply it only of the body field exist otherwise you'll get an error if some content type doesn't have this field, then inject the updated dom into the node.html.twig template using the $varaibles that has been passed by ref to the current hook:


&lt;?php
/**
* Implements  hook_preprocess_hook
*/
function test_preprocess_node(&amp;$variables){
 // 1. Getting the current node.
 $node = $variables['node'];


 //2. Apply this logic to all nodes that have a body field
 if( $node-&gt;hasField('body')){

   //3.Get the body field
    $body = $node-&gt;body-&gt;value;

    //4.Apply som logic on the body field
     $body = updateBodyDom($body);

   //5.injecting the result into the node template (ex: node.html.twig)
   $variables['body'] =  $body ;
 }

}


function updateBodyDom($body){
 $DOM = new DOMDocument();

 $DOM-&gt;loadHTML(mb_convert_encoding($body, "HTML-ENTITIES", "UTF-8"), LIBXML_HTML_NODEFDTD);

 $anchors = $DOM-&gt;getElementsByTagName("a");

 foreach ($anchors as $anchor) {
   $existing_classes = $anchor-&gt;getAttribute("class") ? $anchor-&gt;getAttribute("class") : "";
   $anchor-&gt;setAttribute("class", "button {$existing_classes}");

   //kint($anchor-&gt; getAttribute('class')); die();
 }

 $content = $DOM-&gt;saveHTML( );
 return   $content;
}




in your templates folder create node.html.twig, to render the
injected body :


{{ body }}