How I can change rendered Html in Drupal 9

Viewed 20

I created custom module and it need to be able to change rendered html of each page on the site. I wand to change all external links. How I can realize it? Can I use hooks? How I can do in in right way?

1 Answers

The answer is to understand the Drupal templating system. Any Twig template on Drupal can be overridden with the current theme by copying the Twig-template into your theme and overriding the template content. You can also use theme_hooks with modules to override templates in modules instead of themes.

Take a look at the documentation: https://www.drupal.org/docs/theming-drupal/twig-in-drupal/working-with-twig-templates

For your specific link-Template you can use:

  • core/modules/system/templates/links.html.twig

or depending on your base theme:

  • web/core/themes/classy/templates/navigation/links.html.twig
  • web/core/themes/olivero/templates/navigation/links.html.twig
  • web/core/themes/stable/templates/navigation/links.html.twig
  • web/core/themes/stable9/templates/navigation/links.html.twig
  • web/core/themes/starterkit_theme/templates/navigation/links.html.twig
Related