View source
<?php
namespace Drupal\private_message\Plugin\Block;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\private_message\Service\PrivateMessageServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface, ContainerFactoryPluginInterface {
protected $privateMessageService;
protected $currentUser;
protected $entityManager;
protected $csrfToken;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $currentUser, PrivateMessageServiceInterface $privateMessageService, EntityManagerInterface $entityManager, CsrfTokenGenerator $csrfToken) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $currentUser;
$this->privateMessageService = $privateMessageService;
$this->entityManager = $entityManager;
$this->csrfToken = $csrfToken;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'), $container
->get('private_message.service'), $container
->get('entity.manager'), $container
->get('csrf_token'));
}
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'];
foreach ($threads as $thread) {
$block[$thread
->id()] = $view_builder
->view($thread, 'inbox');
}
$block['#attached']['library'][] = 'private_message/inbox_block';
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['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-inbox-block';
return $block;
}
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), [
'private_message_inbox_block:uid:' . $this->currentUser
->id(),
]);
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'user',
]);
}
public function defaultConfiguration() {
return [
'thread_count' => 5,
'ajax_load_count' => 5,
'ajax_refresh_rate' => 15,
];
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$form['thread_count'] = [
'#type' => 'number',
'#title' => $this
->t('Number of threads to show'),
'#description' => $this
->t('The number of threads to be shown in the block'),
'#default_value' => $config['thread_count'],
'#min' => 1,
];
$form['ajax_load_count'] = [
'#type' => 'number',
'#title' => $this
->t('Number of threads to load with ajax'),
'#description' => $this
->t('The number of threads to be loaded when the load previous link is clicked'),
'#default_value' => $config['ajax_load_count'],
'#min' => 1,
];
$form['ajax_refresh_rate'] = [
'#type' => 'number',
'#title' => $this
->t('Ajax refresh rate'),
'#default_value' => $config['ajax_refresh_rate'],
'#min' => 0,
'#description' => $this
->t('The number of seconds after which the inbox should refresh itself. Setting this to a low number will result in more requests to the server, adding overhead and bandwidth. Setting this number to zero will disable ajax refresh, and the inbox will only updated if/when the page is refreshed.'),
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['thread_count'] = $form_state
->getValue('thread_count');
$this->configuration['ajax_load_count'] = $form_state
->getValue('ajax_load_count');
$this->configuration['ajax_refresh_rate'] = $form_state
->getValue('ajax_refresh_rate');
}
}