View source
<?php
namespace Drupal\data_policy\Plugin\Block;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\data_policy\InformBlockInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class DataPolicyInformBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $requestStack;
protected $currentPath;
protected $aliasManager;
protected $pathMatcher;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack, CurrentPathStack $current_path, AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->requestStack = $request_stack;
$this->currentPath = $current_path;
$this->aliasManager = $alias_manager;
$this->pathMatcher = $path_matcher;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('request_stack'), $container
->get('path.current'), $container
->get('path.alias_manager'), $container
->get('path.matcher'), $container
->get('entity_type.manager'));
}
public function getConfiguration() {
$configuration = parent::getConfiguration();
if (!empty($inform_block = $this
->getInformBlock())) {
$configuration['label'] = $inform_block
->label();
}
return $configuration;
}
protected function blockAccess(AccountInterface $account) {
if ($this
->getInformBlock() instanceof InformBlockInterface) {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
public function build() {
$inform_block = $this
->getInformBlock();
$build['text'] = [
'#markup' => $inform_block->summary['value'],
];
if (!empty($inform_block->body['value'])) {
$build['#attached']['library'][] = 'core/drupal.dialog.ajax';
$build['link'] = [
'#type' => 'link',
'#title' => $this
->t('Read more'),
'#url' => Url::fromRoute('data_policy.description', [
'informblock' => $inform_block
->id(),
]),
'#attributes' => [
'class' => [
'use-ajax btn btn-flat',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'title' => $inform_block
->label(),
'width' => 700,
]),
],
];
}
return $build;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['label']['#disabled'] = $form['label_display']['#disabled'] = TRUE;
return $form;
}
private function getInformBlock() {
$inform_blocks = $this->entityTypeManager
->getStorage('informblock')
->loadByProperties([
'status' => TRUE,
]);
foreach ($inform_blocks as $inform_block) {
$link = $inform_block->page;
$request = $this->requestStack
->getCurrentRequest();
$path = $this->currentPath
->getPath($request);
$path = $path === '/' ? $path : rtrim($path, '/');
$path_alias = Unicode::strtolower($this->aliasManager
->getAliasByPath($path));
if ($this->pathMatcher
->matchPath($path_alias, $link) || $path != $path_alias && $this->pathMatcher
->matchPath($path, $link)) {
return $inform_block;
}
}
return NULL;
}
public function getCacheContexts() {
return Cache::mergeTags(parent::getCacheTags(), [
'route',
]);
}
}