You are here

class ViewsBulkOperationsActionManager in Views Bulk Operations (VBO) 8

Same name and namespace in other branches
  1. 8.3 src/Service/ViewsBulkOperationsActionManager.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager
  2. 8.2 src/Service/ViewsBulkOperationsActionManager.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager
  3. 4.0.x src/Service/ViewsBulkOperationsActionManager.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager

Defines Views Bulk Operations action manager.

Extends the core Action Manager to allow VBO actions define additional configuration.

Hierarchy

Expanded class hierarchy of ViewsBulkOperationsActionManager

5 files declare their use of ViewsBulkOperationsActionManager
ActionsPermissions.php in modules/actions_permissions/src/ActionsPermissions.php
ActionsPermissionsEventSubscriber.php in modules/actions_permissions/src/EventSubscriber/ActionsPermissionsEventSubscriber.php
ConfigureAction.php in src/Form/ConfigureAction.php
ConfirmAction.php in src/Form/ConfirmAction.php
ViewsBulkOperationsBulkForm.php in src/Plugin/views/field/ViewsBulkOperationsBulkForm.php
1 string reference to 'ViewsBulkOperationsActionManager'
views_bulk_operations.services.yml in ./views_bulk_operations.services.yml
views_bulk_operations.services.yml
1 service uses ViewsBulkOperationsActionManager
plugin.manager.views_bulk_operations_action in ./views_bulk_operations.services.yml
Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager

File

src/Service/ViewsBulkOperationsActionManager.php, line 18

Namespace

Drupal\views_bulk_operations\Service
View source
class ViewsBulkOperationsActionManager extends ActionManager {
  const ALTER_ACTIONS_EVENT = 'views_bulk_operations.action_definitions';

  /**
   * Event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Additional parameters passed to alter event.
   *
   * @var array
   */
  protected $alterParameters;

  /**
   * Service constructor.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler to invoke the alter hook with.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
   *   The event dispatcher service.
   */
  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');
  }

  /**
   * {@inheritdoc}
   */
  protected function findDefinitions() {
    $definitions = $this
      ->getDiscovery()
      ->getDefinitions();

    // Incompatible actions.
    $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 the plugin definition is an object, attempt to convert it to an
      // array, if that is not possible, skip further processing.
      if (is_object($plugin_definition) && !($plugin_definition = (array) $plugin_definition)) {
        continue;
      }

      // If this plugin was provided by a module that does not exist, remove the
      // plugin definition.
      if (isset($plugin_definition['provider']) && !in_array($plugin_definition['provider'], [
        'core',
        'component',
      ]) && !$this
        ->providerExists($plugin_definition['provider'])) {
        unset($definitions[$plugin_id]);
      }
    }
    return $definitions;
  }

  /**
   * {@inheritdoc}
   *
   * @param array $parameters
   *   Parameters of the method. Passed to alter event.
   */
  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;
  }

  /**
   * Gets a specific plugin definition.
   *
   * @param string $plugin_id
   *   A plugin id.
   * @param bool $exception_on_invalid
   *   (optional) If TRUE, an invalid plugin ID will throw an exception.
   * @param array $parameters
   *   Parameters of the method. Passed to alter event.
   *
   * @return mixed
   *   A plugin definition, or NULL if the plugin ID is invalid and
   *   $exception_on_invalid is FALSE.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   Thrown if $plugin_id is invalid and $exception_on_invalid is TRUE.
   */
  public function getDefinition($plugin_id, $exception_on_invalid = TRUE, array $parameters = []) {

    // Loading all definitions here will not hurt much, as they're cached,
    // and we need the option to alter a definition.
    $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));
  }

  /**
   * {@inheritdoc}
   */
  public function processDefinition(&$definition, $plugin_id) {

    // Only arrays can be operated on.
    if (!is_array($definition)) {
      return;
    }
    if (!empty($this->defaults) && is_array($this->defaults)) {
      $definition = NestedArray::mergeDeep($this->defaults, $definition);
    }

    // Merge in defaults.
    $definition += [
      'confirm' => FALSE,
      'pass_context' => FALSE,
      'pass_view' => FALSE,
    ];

    // Add default confirmation form if confirm set to TRUE
    // and not explicitly set.
    if ($definition['confirm'] && empty($definition['confirm_form_route_name'])) {
      $definition['confirm_form_route_name'] = 'views_bulk_operations.confirm';
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function alterDefinitions(&$definitions) {

    // Let other modules change definitions.
    // Main purpose: Action permissions bridge.
    $event = new Event();
    $event->alterParameters = $this->alterParameters;
    $event->definitions =& $definitions;
    $this->eventDispatcher
      ->dispatch(static::ALTER_ACTIONS_EVENT, $event);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionManager::getDefinitionsByType public function Gets the plugin definitions for this entity type.
CategorizingPluginManagerTrait::getCategories public function
CategorizingPluginManagerTrait::getGroupedDefinitions public function
CategorizingPluginManagerTrait::getModuleHandler public function Returns the module handler used.
CategorizingPluginManagerTrait::getProviderName protected function Gets the name of a provider.
CategorizingPluginManagerTrait::getSortedDefinitions public function
CategorizingPluginManagerTrait::processDefinitionCategory protected function Processes a plugin definition to ensure there is a category.
DefaultPluginManager::$additionalAnnotationNamespaces protected property Additional namespaces the annotation discovery mechanism should scan for annotation definitions.
DefaultPluginManager::$alterHook protected property Name of the alter hook if one should be invoked.
DefaultPluginManager::$cacheKey protected property The cache key.
DefaultPluginManager::$cacheTags protected property An array of cache tags to use for the cached definitions.
DefaultPluginManager::$defaults protected property A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. 9
DefaultPluginManager::$moduleHandler protected property The module handler to invoke the alter hook. 1
DefaultPluginManager::$namespaces protected property An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations.
DefaultPluginManager::$pluginDefinitionAnnotationName protected property The name of the annotation that contains the plugin definition.
DefaultPluginManager::$pluginInterface protected property The interface each plugin should implement. 1
DefaultPluginManager::$subdir protected property The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace.
DefaultPluginManager::alterInfo protected function Sets the alter hook name.
DefaultPluginManager::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions 5
DefaultPluginManager::extractProviderFromDefinition protected function Extracts the provider from a plugin definition.
DefaultPluginManager::fixContextAwareDefinitions private function Fix the definitions of context-aware plugins.
DefaultPluginManager::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
DefaultPluginManager::getCachedDefinitions protected function Returns the cached plugin definitions of the decorated discovery class.
DefaultPluginManager::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
DefaultPluginManager::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
DefaultPluginManager::getDiscovery protected function Gets the plugin discovery. Overrides PluginManagerBase::getDiscovery 12
DefaultPluginManager::getFactory protected function Gets the plugin factory. Overrides PluginManagerBase::getFactory
DefaultPluginManager::providerExists protected function Determines if the provider of a definition exists. 3
DefaultPluginManager::setCacheBackend public function Initialize the cache backend.
DefaultPluginManager::setCachedDefinitions protected function Sets a cache of plugin definitions for the decorated discovery class.
DefaultPluginManager::useCaches public function Disable the use of caches. Overrides CachedDiscoveryInterface::useCaches 1
DiscoveryCachedTrait::$definitions protected property Cached definitions array. 1
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::hasDefinition public function
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::createInstance public function Creates a pre-configured instance of a plugin. Overrides FactoryInterface::createInstance 12
PluginManagerBase::getInstance public function Gets a preconfigured instance of a plugin. Overrides MapperInterface::getInstance 7
PluginManagerBase::handlePluginNotFound protected function Allows plugin managers to specify custom behavior if a plugin is not found. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag. 1
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.
ViewsBulkOperationsActionManager::$alterParameters protected property Additional parameters passed to alter event.
ViewsBulkOperationsActionManager::$eventDispatcher protected property Event dispatcher service.
ViewsBulkOperationsActionManager::alterDefinitions protected function Invokes the hook to alter the definitions if the alter hook is set. Overrides DefaultPluginManager::alterDefinitions
ViewsBulkOperationsActionManager::ALTER_ACTIONS_EVENT constant
ViewsBulkOperationsActionManager::findDefinitions protected function Finds plugin definitions. Overrides DefaultPluginManager::findDefinitions
ViewsBulkOperationsActionManager::getDefinition public function Gets a specific plugin definition. Overrides DiscoveryCachedTrait::getDefinition
ViewsBulkOperationsActionManager::getDefinitions public function Overrides DefaultPluginManager::getDefinitions
ViewsBulkOperationsActionManager::processDefinition public function Performs extra processing on plugin definitions. Overrides DefaultPluginManager::processDefinition
ViewsBulkOperationsActionManager::__construct public function Service constructor. Overrides ActionManager::__construct