Score:0

How to modify a rendered HTML string by adding custom code to setContent() in core in a module?

jp flag

I want to override the HTML string returned by core using a custom module that replaces $content['#markup'], or the entire $content array returned by HtmlResponse::setContent().

I can override Response::setContent() by calling HtmlResponse::setContent(). Exactly how do I add this to a custom module, for example implementing a service?

My goal is altering all the <a> tags to add a <span> tag to the ones that links to downloadable files.

Jaypan avatar
de flag
You should tell us what your overall goal is, not just how you are trying to accomplish it. This post needs more details.
wildfeed avatar
jp flag
@Jaypan - I want to run a REGEX on the rendered HTML string that will modify all the <a> tags by adding a <span> to the ones that link to downloadable files. I have the regex, Too many links from too many different sources to do this effectively in the theme so I want to try this approach
hoanns avatar
fr flag
do you want to change a Form or Controller output?
Score:0
cn flag

If you want to act on the rendered HTML of a render array rather than the structured content array before rendering, use a #post_render callback. See How to alter page content?

Score:0
fr flag

If you want to change the output of a form see hook_form_alter

If you want to change the output of controllers see this answer. You can also subscribe to other Kernel Events with that method and should be able to alter any output your site creates. (kernel.response is probably what you need)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.