-2

I have been able to change each node's Comment Link 'title' text (that on default renders 'Add new comment') with the below code, but if I add HTML, it renders as plain text. I'm trying to add a FontAwesome icon before this text <i class="fa fa-comments" aria-hidden="true"></i>

enter image description here

function MODULE_node_view_alter(&$build) {
  $node = $build['#node'];
  if (isset($build['links']['comment']['#links']['comment-add'])) {
    $build['links']['comment']['#links']['comment-add']['title'] = '<i class="fa fa-comments" aria-hidden="true"></i> Add Comment';
  }
}

All I have passed is a $build parameter, but I am unsure of whether or not this is the issue.

Any ideas?

2 Answers 2

1

You can render HTML in links by setting the html option to TRUE. Check theme_links() to see all options.

For example

theme('links', array(
  'links' => array(
    'title' => '<span>Title</span>',
    'href' => 'node/1',
    'html' => FALSE, // default
  )
))

would result in the span tag being escaped, while

theme('links', array(
  'links' => array(
    'title' => '<span>Title</span>',
    'href' => 'node/1',
    'html' => TRUE,
  )
))

would render the span tag as html.

So in your case you need to add the following row.

$build['links']['comment']['#links']['comment-add']['html] = TRUE;
0

Titles do not support HTML markup. This is AFAIK a security countermeasure.

There was an HTML Title module but it can no longer be downloaded because of unfixed security issues.

If you want to add an icon, you can add it to the form as #markup and then just adjust the placement as needed.

3
  • Thank you for the advice. Could you please provide an example of how I can add HTML to the comment title as #markup?
    – WebMW
    Sep 28, 2017 at 20:23
  • 1
    @WebMV, put the string in t(). This creates a translatable markup, which is best practice in Drupal for all strings containing translatable strings and also solves your problem with escaping of html tags.
    – 4uk4
    Sep 28, 2017 at 21:29
  • The t() function may be best practice but doesn't seem to make a difference. For now, I'm hacking the icon in with CSS:before & unicode... :(
    – WebMW
    Sep 28, 2017 at 22:43

Your Answer

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

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