SocialPrivateMessageInboxBlock.php in Open Social 8
File
modules/social_features/social_private_message/src/Plugin/Block/SocialPrivateMessageInboxBlock.php
View source
<?php
namespace Drupal\social_private_message\Plugin\Block;
use Drupal\Core\Url;
use Drupal\private_message\Plugin\Block\PrivateMessageInboxBlock;
class SocialPrivateMessageInboxBlock extends PrivateMessageInboxBlock {
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;
}
}
public function getCacheContexts() {
$cache_contexts = parent::getCacheContexts();
$cache_contexts[] = 'user';
return $cache_contexts;
}
public function customSort($pmt1, $pmt2) {
if ($pmt1
->getUpdatedTime() == $pmt2
->getUpdatedTime()) {
return 0;
}
return $pmt1
->getUpdatedTime() < $pmt2
->getUpdatedTime() ? -1 : 1;
}
}