You are here

public function PrivateMessageThreadViewBuilder::view in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Builder/PrivateMessageThreadViewBuilder.php \Drupal\private_message\Entity\Builder\PrivateMessageThreadViewBuilder::view()

Builds the render array for the provided entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to render.

string $view_mode: (optional) The view mode that should be used to render the entity.

string $langcode: (optional) For which language the entity should be rendered, defaults to the current content language.

Return value

array A render array for the entity.

Throws

\InvalidArgumentException Can be thrown when the set of parameters is inconsistent, like when trying to view a Comment and passing a Node which is not the one the comment belongs to, or not passing one, and having the comment node not be available for loading.

Overrides EntityViewBuilder::view

File

src/Entity/Builder/PrivateMessageThreadViewBuilder.php, line 99

Class

PrivateMessageThreadViewBuilder
Build handler for rpivate message threads.

Namespace

Drupal\private_message\Entity\Builder

Code

public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
  $build = parent::view($entity, $view_mode, $langcode);
  $classes = [
    'private-message-thread',
  ];
  $classes[] = 'private-message-thread-' . $view_mode;
  $last_access_time = $entity
    ->getLastAccessTimestamp($this->currentUser);
  $newest_message_timestamp = $entity
    ->getNewestMessageCreationTimestamp();
  if ($last_access_time <= $newest_message_timestamp) {
    $classes[] = 'unread-thread';
  }
  $id = Html::getUniqueId('private-message-thread-' . $view_mode . '-' . $entity
    ->id());
  if ($view_mode == 'inbox') {
    $url = Url::fromRoute('entity.private_message_thread.canonical', [
      'private_message_thread' => $entity
        ->id(),
    ]);
    $build['inbox_link'] = [
      '#type' => 'link',
      '#url' => $url,
      '#title' => '',
      '#weight' => 9999,
      '#attributes' => [
        'data-thread-id' => $entity
          ->id(),
        'class' => [
          'private-message-inbox-thread-link',
        ],
      ],
    ];
  }
  if ($view_mode == 'full') {
    $tags[] = 'private_message_thread:' . $entity
      ->id() . ':view:uid:' . $this->currentUser
      ->id();
    $tags[] = 'private_message_inbox_block:uid:' . $this->currentUser
      ->id();
    $tags[] = 'private_message_notification_block:uid:' . $this->currentUser
      ->id();
    Cache::invalidateTags($tags);
    $entity
      ->updateLastAccessTime($this->currentUser);
    $build['#prefix'] = '<div id="private-message-page"><div id="' . $id . '" class="' . implode(' ', $classes) . '" data-thread-id="' . $entity
      ->id() . '" data-last-update="' . $entity
      ->get('updated')->value . '">';
    $build['#suffix'] = '</div></div>';
  }
  else {
    $build['#prefix'] = '<div id="' . $id . '" class="' . implode(' ', $classes) . '" data-thread-id="' . $entity
      ->id() . '" data-last-update="' . $entity
      ->get('updated')->value . '">';
    $build['#suffix'] = '</div>';
  }
  return $build;
}