View source
<?php
namespace Drupal\social_event\Plugin\Block;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\social_event\EventEnrollmentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EventRequestEnrollmentNotification extends BlockBase implements ContainerFactoryPluginInterface {
protected $event;
protected $entityTypeManager;
protected $translation;
protected $routeMatch;
protected $loggerFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TranslationManager $translation, RouteMatchInterface $route_match, LoggerChannelFactoryInterface $logger_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->event = social_event_get_current_event();
$this->entityTypeManager = $entity_type_manager;
$this->translation = $translation;
$this->routeMatch = $route_match;
$this->loggerFactory = $logger_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('string_translation'), $container
->get('current_route_match'), $container
->get('logger.factory'));
}
public function build() : array {
if (!$this->event instanceof NodeInterface) {
return [];
}
if ((int) $this->event
->getFieldValue('field_enroll_method', 'value') !== EventEnrollmentInterface::ENROLL_METHOD_REQUEST) {
return [];
}
try {
$requests = $this->entityTypeManager
->getStorage('event_enrollment')
->getQuery()
->condition('field_event.target_id', $this->event
->id())
->condition('field_request_or_invite_status.value', EventEnrollmentInterface::REQUEST_PENDING)
->condition('field_enrollment_status.value', '0')
->count()
->execute();
if (!$requests) {
return [];
}
return [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('There @link to enroll in this event.', [
'@link' => Link::fromTextAndUrl($this->translation
->formatPlural($requests, 'is (1) new request', 'are (@count) new requests'), Url::fromRoute('view.event_manage_enrollment_requests.page_manage_enrollment_requests', [
'node' => $this->event
->id(),
]))
->toString(),
]),
'#attributes' => [
'class' => [
'alert',
'alert-warning',
],
],
];
} catch (InvalidPluginDefinitionException $e) {
$this->loggerFactory
->get('social_event')
->error($e
->getMessage());
} catch (PluginNotFoundException $e) {
$this->loggerFactory
->get('social_event')
->error($e
->getMessage());
}
return [];
}
public function access(AccountInterface $account, $return_as_object = FALSE) {
$is_event_page = isset($this->event);
$routes = [
'entity.node.canonical',
'view.event_enrollments.view_enrollments',
'view.event_manage_enrollments.page_manage_enrollments',
'view.event_manage_enrollment_invites.page_manage_enrollment_invites',
'view.manage_enrollments.page',
'view.managers.view_managers',
'social_event_managers.add_enrollees',
'social_event_managers.vbo.execute_configurable',
'social_event_managers.vbo.confirm',
];
if ($this->event instanceof NodeInterface && in_array($this->routeMatch
->getRouteName(), $routes, TRUE)) {
return AccessResult::allowedIf($is_event_page && social_event_manager_or_organizer($this->event));
}
return AccessResult::forbidden();
}
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts = Cache::mergeContexts($contexts, [
'url',
'user.permissions',
]);
return $contexts;
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), [
'event_enrollment_list:' . $this->event
->id(),
]);
}
}