You are here

function private_message_private_message_thread_view in Private Message 8.2

Same name and namespace in other branches
  1. 8 private_message.module \private_message_private_message_thread_view()

Implements hook_ENTITY_TYPE_view().

See also

hook_entity_view()

File

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

Code

function private_message_private_message_thread_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display
    ->getComponent('last_message')) {
    $messages = $entity
      ->getMessages();
    if ($messages) {
      $last_message = array_pop($messages);
      $view_builder = \Drupal::entityTypeManager()
        ->getViewBuilder('private_message');
      $build['last_message'] = $view_builder
        ->view($last_message, 'inbox');
    }
  }
  if ($display
    ->getComponent('private_message_form')) {
    $currentUser = \Drupal::currentUser();
    $members = $entity
      ->getMembers();
    $activeMembers = [];
    foreach ($members as $member) {
      if ($member
        ->isActive() && $member
        ->id() != $currentUser
        ->id()) {
        $activeMembers[] = $member;
      }
    }
    if (empty($activeMembers)) {
      $build['no_active_users'] = [
        '#theme' => 'private_message_no_active_users_notice',
      ];
    }
    else {
      $private_message = PrivateMessage::create();
      $form_object = \Drupal::entityTypeManager()
        ->getFormObject('private_message', 'add')
        ->setEntity($private_message);
      $build['private_message_form'] = Drupal::formBuilder()
        ->getForm($form_object, $entity);
    }
  }
  if ($display
    ->getComponent('delete_link')) {
    $url = Url::fromRoute('entity.private_message_thread.delete_form', [
      'private_message_thread' => $entity
        ->id(),
    ]);
    $build['delete_link'] = [
      '#prefix' => '<div class="private_message_thread_delete_link_wrapper">',
      '#suffix' => '</div>',
      '#type' => 'link',
      '#url' => $url,
      '#title' => t('Delete thread'),
    ];
  }
}