You are here

public function PurgersService::setPluginsEnabled in Purge 8.3

Set the plugins used by the service and reload it.

Parameters

string[] $plugin_ids: Unassociative array with plugin IDs to be enabled.

Overrides ModifiableServiceInterface::setPluginsEnabled

See also

\Drupal\purge\ModifiableServiceInterface::getPluginsAvailable()

2 calls to PurgersService::setPluginsEnabled()
PurgersService::movePurgerDown in src/Plugin/Purge/Purger/PurgersService.php
Move the purger instance down in the plugin execution order.
PurgersService::movePurgerUp in src/Plugin/Purge/Purger/PurgersService.php
Move the purger instance up in the plugin execution order.

File

src/Plugin/Purge/Purger/PurgersService.php, line 322

Class

PurgersService
Provides the service that distributes access to one or more purgers.

Namespace

Drupal\purge\Plugin\Purge\Purger

Code

public function setPluginsEnabled(array $plugin_ids) {
  $this
    ->initializePurgers();

  // Validate that the given plugin_id's and instance ID's make sense.
  $definitions = $this->pluginManager
    ->getDefinitions();
  foreach ($plugin_ids as $instance_id => $plugin_id) {
    if (!is_string($instance_id) || empty($instance_id)) {
      throw new \LogicException('Invalid instance ID (key).');
    }
    if (!isset($definitions[$plugin_id])) {
      throw new \LogicException('Invalid plugin_id.');
    }
  }

  // Find out which instances are being deleted and let those purgers cleanup.
  foreach ($this
    ->getPluginsEnabled() as $instance_id => $plugin_id) {
    if (!isset($plugin_ids[$instance_id])) {
      $this->purgers[$instance_id]
        ->delete();
      $this->purgeLogger
        ->deleteChannel(sprintf(self::LOGGER_PURGERS_FORMAT, $plugin_id, $instance_id));
    }
  }

  // Write the new CMI setting and commit it.
  $order_index = 1;
  $setting = [];
  foreach ($plugin_ids as $instance_id => $plugin_id) {
    $order_index = $order_index + 1;
    $setting[] = [
      'order_index' => $order_index,
      'instance_id' => $instance_id,
      'plugin_id' => $plugin_id,
    ];
  }
  $this->configFactory
    ->getEditable('purge.plugins')
    ->set('purgers', $setting)
    ->save();

  // Make sure any new call to this service loads the new configuration.
  $this
    ->reload();
}