You are here

public function PrivateMessageThreadMessageFormatter::viewElements in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/PrivateMessageThreadMessageFormatter.php \Drupal\private_message\Plugin\Field\FieldFormatter\PrivateMessageThreadMessageFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/PrivateMessageThreadMessageFormatter.php, line 200

Class

PrivateMessageThreadMessageFormatter
Defines the private message thread message field formatter.

Namespace

Drupal\private_message\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  $private_message_thread = $items
    ->getEntity();
  $element = [
    '#prefix' => '<div class="private-message-thread-messages">',
    '#suffix' => '</div>',
  ];
  $view_builder = $this->entityManager
    ->getViewBuilder('private_message');
  $user = $this->userManager
    ->load($this->currentUser
    ->id());
  $messages = $private_message_thread
    ->filterUserDeletedMessages($user);
  $messages = array_slice($messages, -1 * $this
    ->getSetting('message_count'));
  foreach ($messages as $message) {
    $element[$message
      ->id()] = $view_builder
      ->view($message, 'full');
  }
  if ($this
    ->getSetting('message_order') == 'desc') {
    $element = array_reverse($element);
  }
  $new_url = Url::fromRoute('private_message.ajax_callback', [
    'op' => 'get_new_messages',
  ]);
  $token = $this->csrfTokenGenerator
    ->get($new_url
    ->getInternalPath());
  $new_url
    ->setOptions([
    'absolute' => TRUE,
    'query' => [
      'token' => $token,
    ],
  ]);
  $prev_url = Url::fromRoute('private_message.ajax_callback', [
    'op' => 'get_old_messages',
  ]);
  $token = $this->csrfTokenGenerator
    ->get($prev_url
    ->getInternalPath());
  $prev_url
    ->setOptions([
    'absolute' => TRUE,
    'query' => [
      'token' => $token,
    ],
  ]);
  $load_url = Url::fromRoute('private_message.ajax_callback', [
    'op' => 'load_thread',
  ]);
  $load_token = $this->csrfTokenGenerator
    ->get($load_url
    ->getInternalPath());
  $load_url
    ->setOptions([
    'absolute' => TRUE,
    'query' => [
      'token' => $load_token,
    ],
  ]);
  $element['#attached']['drupalSettings']['privateMessageThread'] = [
    'newMessageCheckUrl' => $new_url
      ->toString(),
    'previousMessageCheckUrl' => $prev_url
      ->toString(),
    'messageOrder' => $this
      ->getSetting('message_order'),
    'refreshRate' => $this
      ->getSetting('ajax_refresh_rate') * 1000,
    'loadThreadUrl' => $load_url
      ->toString(),
  ];
  $element['#attached']['library'][] = 'private_message/private_message_thread';
  return $element;
}