You are here

public function SocialPrivateMessageInboxBlock::build in Open Social 8

File

modules/social_features/social_private_message/src/Plugin/Block/SocialPrivateMessageInboxBlock.php, line 21

Class

SocialPrivateMessageInboxBlock
Provides a 'SocialPrivateMessageInboxBlock' block.

Namespace

Drupal\social_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']);
    if (count($thread_info['threads'])) {
      $view_builder = $this->entityManager
        ->getViewBuilder('private_message_thread');
      $threads = $thread_info['threads'];

      /* @var \Drupal\private_message\Entity\PrivateMessageThread $thread */

      // This custom sort, sorts based on newestmessage timestamp in the
      // thread.
      uasort($threads, [
        $this,
        "customSort",
      ]);

      // The above sorts ascending... so:
      $threads = array_reverse($threads);
      foreach ($threads as $thread) {
        $block[$thread
          ->id()] = $view_builder
          ->view($thread, 'inbox');
      }
    }
    else {
      $block['no_threads'] = [
        '#prefix' => '<p>',
        '#suffix' => '</p>',
        '#markup' => $this
          ->t('You do not have any private messages yet. Click on the button on the right to start a new chat.'),
      ];
    }
    $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,
      ],
    ]);

    // 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';
    return $block;
  }
}