You are here

function private_message_user_view in Private Message 8

Same name and namespace in other branches
  1. 8.2 private_message.module \private_message_user_view()

Implements hook_ENTITY_TYPYE_view().

Adds new elements to the User entity.

See also

hook_entity_view()

File

./private_message.module, line 148
Contains hooks for the private message module.

Code

function private_message_user_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display
    ->getComponent('linked_username')) {
    if (\Drupal::currentUser()
      ->hasPermission('access user profiles')) {
      $url = Url::fromRoute('entity.user.canonical', [
        'user' => $entity
          ->id(),
      ]);
      $build['linked_username'] = [
        '#prefix' => '<p class="username">',
        '#suffix' => '</p>',
        '#type' => 'link',
        '#url' => $url,
        '#title' => $entity
          ->getDisplayName(),
      ];
    }
    else {
      $build['linked_username'] = [
        '#prefix' => '<p class="username">',
        '#suffix' => '</p>',
        '#markup' => $entity
          ->getDisplayName(),
      ];
    }
  }
  if ($display
    ->getComponent('private_message_link')) {
    $current_user = \Drupal::currentUser();
    if ($current_user
      ->isAuthenticated()) {
      if ($current_user
        ->hasPermission('use private messaging system') && $current_user
        ->id() != $entity
        ->id()) {
        $members = [
          $current_user,
          $entity,
        ];
        $thread_id = \Drupal::service('private_message.mapper')
          ->getThreadIdForMembers($members);
        if ($thread_id) {
          $url = Url::fromRoute('entity.private_message_thread.canonical', [
            'private_message_thread' => $thread_id,
          ], [
            'attributes' => [
              'class' => [
                'private_message_link',
              ],
            ],
          ]);
          $build['private_message_link'] = [
            '#type' => 'link',
            '#url' => $url,
            '#title' => t('Send private message'),
            '#prefix' => '<div class="private_message_link_wrapper">',
            '#suffix' => '</div>',
          ];
        }
        else {
          $url = Url::fromRoute('private_message.private_message_create', [], [
            'query' => [
              'recipient' => $entity
                ->id(),
            ],
          ]);
          $build['private_message_link'] = [
            '#type' => 'link',
            '#url' => $url,
            '#title' => t('Send private message'),
            '#prefix' => '<div class="private_message_link_wrapper">',
            '#suffix' => '</div>',
          ];
        }
      }
    }
    else {
      $url = Url::fromRoute('user.login');
      $build['private_message_link'] = [
        '#type' => 'link',
        '#url' => $url,
        '#title' => t('Send private message'),
        '#prefix' => '<div class="private_message_link_wrapper">',
        '#suffix' => '</div>',
      ];
    }
  }
}