View source
<?php
namespace Drupal\block_blacklist;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\block_blacklist\Blacklist;
class PluginAlter implements ContainerInjectionInterface {
protected $configFactory;
protected $entityTypeManager;
protected $blacklistService;
protected function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, Blacklist $blacklist_service) {
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
$this->blacklistService = $blacklist_service;
$this
->setUp();
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('block_blacklist.blacklist'));
}
protected function setUp() {
$settings = $this->configFactory
->get('block_blacklist.settings');
$options = [
'match' => !empty($settings) ? trim($settings
->get('system_match')) : '',
'prefix' => !empty($settings) ? trim($settings
->get('system_prefix')) : '',
'regex' => !empty($settings) ? trim($settings
->get('system_regex')) : '',
];
$this->blacklistService
->setUp($options);
}
public function alterBlocks(&$definitions) {
if (!$this->blacklistService
->hasSettings()) {
return;
}
$callback = [
$this->blacklistService,
'blockIsAllowed',
];
$definitions = array_filter($definitions, $callback, ARRAY_FILTER_USE_KEY);
}
}