DefaultSingleLazyPluginCollection.php 2.78 KB
Newer Older
1 2
<?php

3 4 5
namespace Drupal\Core\Plugin;

use Drupal\Component\Plugin\PluginManagerInterface;
6
use Drupal\Component\Plugin\LazyPluginCollection;
7
use Drupal\Component\Plugin\ConfigurablePluginInterface;
8
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
9 10

/**
11
 * Provides a default plugin collection for a plugin type.
12
 *
13
 * A plugin collection usually stores multiple plugins, and is used to lazily
14
 * instantiate them. When only one plugin is needed, it is still best practice
15 16 17
 * to encapsulate all of the instantiation logic in a plugin collection. This
 * class can be used directly, or subclassed to add further exception handling
 * in self::initializePlugin().
18
 */
19
class DefaultSingleLazyPluginCollection extends LazyPluginCollection {
20
  use DependencySerializationTrait;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

  /**
   * The manager used to instantiate the plugins.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $manager;

  /**
   * An array of configuration to instantiate the plugin with.
   *
   * @var array
   */
  protected $configuration;

36
  /**
37
   * The instance ID used for this plugin collection.
38 39 40 41 42
   *
   * @var string
   */
  protected $instanceId;

43
  /**
44
   * Constructs a new DefaultSingleLazyPluginCollection object.
45 46 47
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
   *   The manager to be used for instantiating plugins.
48 49
   * @param string $instance_id
   *   The ID of the plugin instance.
50 51 52
   * @param array $configuration
   *   An array of configuration.
   */
53
  public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration) {
54
    $this->manager = $manager;
55
    $this->addInstanceId($instance_id, $configuration);
56 57 58 59 60 61
  }

  /**
   * {@inheritdoc}
   */
  protected function initializePlugin($instance_id) {
62
    $this->set($instance_id, $this->manager->createInstance($instance_id, $this->configuration));
63 64
  }

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    $plugin = $this->get($this->instanceId);
    if ($plugin instanceof ConfigurablePluginInterface) {
      return $plugin->getConfiguration();
    }
    else {
      return $this->configuration;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration($configuration) {
82
    $this->configuration = $configuration;
83 84 85 86 87 88 89
    $plugin = $this->get($this->instanceId);
    if ($plugin instanceof ConfigurablePluginInterface) {
      $plugin->setConfiguration($configuration);
    }
    return $this;
  }

90 91 92 93
  /**
   * {@inheritdoc}
   */
  public function addInstanceId($id, $configuration = NULL) {
94
    $this->instanceId = $id;
95 96
    // Reset the list of instance IDs since there can be only one.
    $this->instanceIDs = [];
97 98 99 100 101 102
    parent::addInstanceId($id, $configuration);
    if ($configuration !== NULL) {
      $this->setConfiguration($configuration);
    }
  }

103
}