entity_agree-2.0.x-dev/src/Plugin/Block/NotificationBlock.php
src/Plugin/Block/NotificationBlock.php
<?php namespace Drupal\entity_agree\Plugin\Block; use Drupal\Core\Access\AccessResultAllowed; use Drupal\Core\Block\BlockBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\entity_agree\Plugin\Field\FieldType\AgreementItem; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Defines a generic custom block type. * * @Block( * id = "entity_agree_notification", * admin_label = @Translation("Agreement notification"), * category = @Translation("Agreements"), * ) */ class NotificationBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * Agreement manager. * * @var \Drupal\entity_agree\AgreementManager */ protected $agreementManager; /** * The current user. * * @var \Drupal\Core\Session\AccountInterface */ protected $currentUser; /** * Entity type manager service. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $block = new static($configuration, $plugin_id, $plugin_definition); $block->agreementManager = $container->get('entity_agree.agreement_manager'); $block->currentUser = $container->get('current_user'); $block->entityTypeManager = $container->get('entity_type.manager'); return $block; } /** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { // Allow access when user has outstanding agreements. // @todo Add check that outstanding agreement is accessible and has a // notification block. $access = AccessResultAllowed::allowedIf(!empty($this->agreementManager->getOutstandingAgreements())); $access->addCacheContexts(['user']) ->addCacheTags(['entity_agree:user:' . $account->id(), 'entity_agree']); return $access; } /** * {@inheritdoc} */ public function build() { $build = []; $user_agreements = $this->agreementManager->getUserAgreements($this->currentUser); $items = []; foreach ($this->agreementManager->getOutstandingAgreementEntities() as $entities) { /** @var \Drupal\Core\Entity\EntityInterface $entity */ foreach ($entities as $entity) { foreach (AgreementItem::getEntityAgreementPlugins($entity) as $plugins) { /** @var \Drupal\entity_agree\AgreementHandlerInterface[] $plugins */ if (empty($plugins['notification_block'])) { continue; } $configuration = $plugins['notification_block']->getConfiguration(); $key = empty($user_agreements[$entity->getEntityTypeId()][$entity->id()]) ? 'new' : 'updated'; $items[] = [ '#type' => 'processed_text', '#text' => $configuration[$key]['value'] ?? NULL, '#format' => $configuration[$key]['format'] ?? NULL, ]; $build['#cache']['tags'][] = "{$entity->getEntityTypeId()}:{$entity->id()}"; } } } if (empty($items)) { return []; } $build['#theme'] = 'item_list'; $build['#items'] = $items; $build['#cache']['contexts'][] = 'user'; $build['#cache']['tags'][] = 'entity_agree'; $build['#cache']['tags'][] = "entity_agree:user:{$this->currentUser->id()}"; return $build; } }