ViewsBulkOperationsActionManager.php in Views Bulk Operations (VBO) 8
File
src/Service/ViewsBulkOperationsActionManager.php
View source
<?php
namespace Drupal\views_bulk_operations\Service;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
class ViewsBulkOperationsActionManager extends ActionManager {
const ALTER_ACTIONS_EVENT = 'views_bulk_operations.action_definitions';
protected $eventDispatcher;
protected $alterParameters;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cacheBackend, ModuleHandlerInterface $moduleHandler, EventDispatcherInterface $eventDispatcher) {
parent::__construct($namespaces, $cacheBackend, $moduleHandler);
$this->eventDispatcher = $eventDispatcher;
$this
->setCacheBackend($cacheBackend, 'views_bulk_operations_action_info');
}
protected function findDefinitions() {
$definitions = $this
->getDiscovery()
->getDefinitions();
$incompatible = [
'node_delete_action',
'user_cancel_user_action',
];
foreach ($definitions as $plugin_id => &$definition) {
$this
->processDefinition($definition, $plugin_id);
if (empty($definition) || in_array($definition['id'], $incompatible)) {
unset($definitions[$plugin_id]);
}
}
$this
->alterDefinitions($definitions);
foreach ($definitions as $plugin_id => $plugin_definition) {
if (is_object($plugin_definition) && !($plugin_definition = (array) $plugin_definition)) {
continue;
}
if (isset($plugin_definition['provider']) && !in_array($plugin_definition['provider'], [
'core',
'component',
]) && !$this
->providerExists($plugin_definition['provider'])) {
unset($definitions[$plugin_id]);
}
}
return $definitions;
}
public function getDefinitions(array $parameters = []) {
if (empty($parameters['nocache'])) {
$definitions = $this
->getCachedDefinitions();
}
if (!isset($definitions)) {
$this->alterParameters = $parameters;
$definitions = $this
->findDefinitions($parameters);
$this
->setCachedDefinitions($definitions);
}
return $definitions;
}
public function getDefinition($plugin_id, $exception_on_invalid = TRUE, array $parameters = []) {
$definitions = $this
->getDefinitions($parameters);
if (isset($definitions[$plugin_id])) {
return $definitions[$plugin_id];
}
elseif (!$exception_on_invalid) {
return NULL;
}
throw new PluginNotFoundException($plugin_id, sprintf('The "%s" plugin does not exist.', $plugin_id));
}
public function processDefinition(&$definition, $plugin_id) {
if (!is_array($definition)) {
return;
}
if (!empty($this->defaults) && is_array($this->defaults)) {
$definition = NestedArray::mergeDeep($this->defaults, $definition);
}
$definition += [
'confirm' => FALSE,
'pass_context' => FALSE,
'pass_view' => FALSE,
];
if ($definition['confirm'] && empty($definition['confirm_form_route_name'])) {
$definition['confirm_form_route_name'] = 'views_bulk_operations.confirm';
}
}
protected function alterDefinitions(&$definitions) {
$event = new Event();
$event->alterParameters = $this->alterParameters;
$event->definitions =& $definitions;
$this->eventDispatcher
->dispatch(static::ALTER_ACTIONS_EVENT, $event);
}
}