You are here

public function PrivateMessageInboxBlock::build in Private Message 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Block/PrivateMessageInboxBlock.php \Drupal\private_message\Plugin\Block\PrivateMessageInboxBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/PrivateMessageInboxBlock.php, line 113

Class

PrivateMessageInboxBlock
Provides the private message inbox block.

Namespace

Drupal\private_message\Plugin\Block

Code

public function build() {
  if ($this->currentUser
    ->isAuthenticated() && $this->currentUser
    ->hasPermission('use private messaging system')) {
    $config = $this
      ->getConfiguration();
    $thread_info = $this->privateMessageService
      ->getThreadsForUser($config['thread_count']);
    $total_thread = $this->privateMessageService
      ->getCountThreadsForUser();
    if (count($thread_info['threads'])) {
      $view_builder = $this->entityTypeManager
        ->getViewBuilder('private_message_thread');
      $threads = $thread_info['threads'];
      foreach ($threads as $thread) {
        $block[$thread
          ->id()] = $view_builder
          ->view($thread, 'inbox');
      }
      $block['#attached']['library'][] = 'private_message/inbox_block_script';
      $style_disabled = $this->privateMessageConfig
        ->get('remove_css');
      if (!$style_disabled) {
        $block['#attached']['library'][] = 'private_message/inbox_block_style';
      }
      if (count($threads) && $thread_info['next_exists']) {
        $prev_url = Url::fromRoute('private_message.ajax_callback', [
          'op' => 'get_old_inbox_threads',
        ]);
        $prev_token = $this->csrfToken
          ->get($prev_url
          ->getInternalPath());
        $prev_url
          ->setOptions([
          'absolute' => TRUE,
          'query' => [
            'token' => $prev_token,
          ],
        ]);
        $new_url = Url::fromRoute('private_message.ajax_callback', [
          'op' => 'get_new_inbox_threads',
        ]);
        $new_token = $this->csrfToken
          ->get($new_url
          ->getInternalPath());
        $new_url
          ->setOptions([
          'absolute' => TRUE,
          'query' => [
            'token' => $new_token,
          ],
        ]);
        $last_thread = array_pop($threads);
        $block['#attached']['drupalSettings']['privateMessageInboxBlock'] = [
          'oldestTimestamp' => $last_thread
            ->get('updated')->value,
          'loadPrevUrl' => $prev_url
            ->toString(),
          'loadNewUrl' => $new_url
            ->toString(),
          'threadCount' => $config['ajax_load_count'],
        ];
      }
      else {
        $block['#attached']['drupalSettings']['privateMessageInboxBlock'] = [
          'oldestTimestamp' => FALSE,
        ];
      }
    }
    else {
      $block['no_threads'] = [
        '#prefix' => '<p>',
        '#suffix' => '</p>',
        '#markup' => $this
          ->t('You do not have any private messages'),
      ];
    }
    $new_url = Url::fromRoute('private_message.ajax_callback', [
      'op' => 'get_new_inbox_threads',
    ]);
    $new_token = $this->csrfToken
      ->get($new_url
      ->getInternalPath());
    $new_url
      ->setOptions([
      'absolute' => TRUE,
      'query' => [
        'token' => $new_token,
      ],
    ]);
    $block['#attached']['drupalSettings']['privateMessageInboxBlock']['loadNewUrl'] = $new_url
      ->toString();
    $config = $this
      ->getConfiguration();
    $block['#attached']['drupalSettings']['privateMessageInboxBlock']['ajaxRefreshRate'] = $config['ajax_refresh_rate'];
    $block['#attached']['drupalSettings']['privateMessageInboxBlock']['totalThreads'] = $total_thread;
    $block['#attached']['drupalSettings']['privateMessageInboxBlock']['itemsToShow'] = $config['thread_count'];

    // Add the default classes, as these are not added when the block output
    // is overridden with a template.
    $block['#attributes']['class'][] = 'block';
    $block['#attributes']['class'][] = 'block-private-message';
    $block['#attributes']['class'][] = 'block-private-message-inbox-block';

    // Wrapper using in js to place Load Previous button for multiple threads.
    $block['#prefix'] = '<div class="private-message-thread--full-container">';
    $block['#suffix'] = '</div>';
    return $block;
  }
}