2

I have a Drupal 8 site with the Private Message module. I want to display a contact button instead of the contact link.

I created a custom module, it works. But I prefer to put the code in the theme rather than creating a new module.

<?php

/**
 * @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) {
  if (array_key_exists('private_message_link', $build)) {
    $build['private_message_link']['#attributes']['class'] = array('btn-success', 'btn-sm', 'btn');
    $build['private_message_link']['#title'] = t('<i class="fas fa-comment-dots fa-lg"></i> Send a message');
  }
}

function private_message_contact_button_user_view_alter(&$build) {
  if (array_key_exists('private_message_link', $build)) {
    $build['private_message_link']['#attributes']['class'] = array('btn-success', 'btn-sm', 'btn');
    $build['private_message_link']['#title'] = t('<i class="fas fa-comment-dots fa-lg"></i> Send a message');
  }
}

I uninstalled my custom module and copied the code below in my theme but it does not work.

How to use the code in the theme ?

bootstrap_subtheme_front_office_old.theme :

/**
 * Private Message button.
 */
function bootstrap_subtheme_front_office_old_node_view_alter(&$build) {
  if (array_key_exists('private_message_link', $build)) {
    $build['private_message_link']['#attributes']['class'] = array('btn-success', 'btn-sm', 'btn');
    $build['private_message_link']['#title'] = t('<i class="fas fa-comment-dots fa-lg"></i> Send a message');
  }
}
function bootstrap_subtheme_front_office_old_user_view_alter(&$build) {
  if (array_key_exists('private_message_link', $build)) {
    $build['private_message_link']['#attributes']['class'] = array('btn-success', 'btn-sm', 'btn');
    $build['private_message_link']['#title'] = t('<i class="fas fa-comment-dots fa-lg"></i> Send a message');
  }
}
  • Not all hooks are called by theme system, but most of altering hooks should be. I also searched but it's really hard to find what hooks can be implemented by modules and what by themes. Drupal documentations is really poor. Maybe this can be helpful: https://drupal.stackexchange.com/questions/157381/theme-hooks-vs-module-hooks – MilanG Oct 09 '19 at 08:15
  • [Documentation](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_view_alter/8.6.x) states that it should work from theme as well (hook_entity_hook), though after a quick test I get the same behavior : hook_[some]_view[_alter] works only from modules. You can still use preprocess hooks from your theme, but it sounds like a bug to me. – EricLavault Oct 09 '19 at 12:31
  • Ok thank you, I will reactivate my module I have no choice. –  Oct 09 '19 at 13:50
  • A preprocess code could be a solution here: https://drupal.stackexchange.com/questions/270906/processing-the-content-generated-by-a-node, so the functions are `bootstrap_subtheme_front_office_old_preprocess_node` and `bootstrap_subtheme_front_office_old_preprocess_user`. – tompagabor Nov 21 '19 at 09:52

0 Answers0