0

I have a Drupal 7 site and I'd like to be able to add an HTML link to part of the title of a node. By default Drupal has this as plain text. Is there any way that I can allow HMTL?

Example:

This is a <a href="#">title</a>

3 Answers 3

5

That happens because template_process_page() contains the following code.

if (!isset($variables['title'])) {
  $variables['title'] = drupal_get_title();
}

drupal_get_title() calls check_plain().

// During a bootstrap, menu.inc is not included and thus we cannot provide a title.
if (!isset($title) && function_exists('menu_get_active_title')) {
  $title = check_plain(menu_get_active_title());
}

The only way you have to add HTML to the title is using hook_preprocess_page() and set $variables['title'].

Keep in mind that $variables['title'] is also used as value for the <title> tag, and browsers should not expect HTML tags inside that tag.

Titles may contain character entities (for accented characters, special characters, etc.), but may not contain other markup (including comments).

To change just the node title, you should implement hook_preprocess_node() and set $variables['title']. By default, template_preprocess_node() sets it using $variables['title'] = check_plain($node->title);.
As side note, for the node title, Drupal 8 uses $variables['label'], not $variables['title'].

1
0

composer require 'drupal/html_title:^1.4' did the trick for me.

The <br> tag (which was required for my client's request) was even enabled by default. Additionally, the /admin/config/user-interface/html_title path allows you to enable arbitrary html tags as serves your needs.

I seem unable for some reason to upvote or comment on Toki's answer, so will try this, instead. Thanks, Toki. Investigating your response resolved this ticket in about ten minutes, call it 30 minutes total to account for investigation to find your note, and to come back and affirm for others that this module works and is fit for purpose as of drupal 9.3.9.

-1

I had the same question and I found a great and simple module HTML Title. You can enter HTML markups inside your node title and it's done. At least, it was OK for me for italic terms.

EDIT : Indeed my answer deserves a -1, the module is now obsolete and creates strange things on node titles, forget it!

1
  • 1
    I want to point out that at the time this answer was edited the module did become unsupported. That is no longer the case and is actively maintained. It is a tried and true method that has security coverage. Oct 23, 2021 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.