I installed the "Private Message" module on Drupal 8.

There is a link to send a message that I want to transform into a Bootstrap button.

How to customize a link on Drupal 8 ?

Here is the HTML code of the link :

<div class="private_message_link_wrapper"><a href="/private_messages/9" class="private_message_link">Envoyer un message</a></div>

The developer of "Private Message" gave me this code, but I do not know what to do with it.

I need to create a custom module or put it in my_theme.info.yml ?

function HOOK_node_view_alter(&$build) {
  $build['private_message_link']['#title'] = t('Click me. Click me now!');

The developer of the module wrote in a comment :

You can implement hook_ENTITY_TYPE_view_alter(). For example, if you are altering the link on the user profile you would implement hook_user_view_alter(). If you are altering the link on nodes, you would implement hook_node_view_alter(). If you are altering the link on comments, you would implement hook_ENTITY_TYPE_view_alter().

In this function, you can change the output of the link by altering $build['private_message_link'] as you see fit.

I created a custom module:

private_message_contact_button.module :


 * @file
 * Hook implementations of private_message_contact_button module.

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;

function private_message_contact_button_node_view_alter(&$build) {
  $build['private_message_link']['class'] = array('button button--primary', 'btn');

The code works, but the button is not applied.

I want to add class btn btn-success

And I want to add an icon make awesome in front of the text <i class="fas fa-comment fa-lg"></i>

I have to put what as code ?

1 Answer 1


The CSS class is a sub-key of the #attributes of a render array like $build:

function private_message_contact_button_node_view_alter(&$build) {
  if (array_key_exists('private_message_link', $build)) {
    $build['private_message_link']['#attributes']['class'] = array('button button--primary', 'btn');
  • Thank you, it works. Can you tell me how to add a font-awesome icon in front of the text? See the end of my question
    – user90656
    Commented Nov 25, 2018 at 8:44
  • I guess the simpliest way would be extending/overwriting the #title as suggested by the module author.
    – Hudri
    Commented Nov 25, 2018 at 8:49
  • yes but I do not know how to insert do awesome in markup
    – user90656
    Commented Nov 25, 2018 at 8:56
  • I updated your answer. The icon is not rendered this way
    – user90656
    Commented Nov 25, 2018 at 9:05
  • Have you tried $build['private_message_link']['#title'] = t('<i class="fontawesome"></i> Click me. Click me now!');
    – Hudri
    Commented Nov 25, 2018 at 9:11

Your Answer

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