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'];
uasort($threads, [
$this,
"customSort",
]);
$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,
],
]);
$block['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-inbox-block';
return $block;
}
}