class BlockPluginBag

Provides a collection of block plugins.

Hierarchy

  • class \Drupal\Component\Plugin\PluginBag implements \Drupal\Component\Plugin\Iterator, \Drupal\Component\Plugin\Countable

Expanded class hierarchy of BlockPluginBag

1 file declares its use of BlockPluginBag
Block.php in drupal/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php
Contains \Drupal\block\Plugin\Core\Entity\Block.

File

drupal/core/modules/block/lib/Drupal/block/BlockPluginBag.php, line 18
Contains \Drupal\block\BlockPluginBag.

Namespace

Drupal\block
View source
class BlockPluginBag extends PluginBag {

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

  /**
   * Constructs a BlockPluginBag object.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
   *   The manager to be used for instantiating plugins.
   * @param array $instance_ids
   *   The ids of the plugin instances with which we are dealing.
   * @param \Drupal\block\Plugin\Core\Entity\Block $entity
   *   The Block entity that holds our configuration.
   */
  public function __construct(PluginManagerInterface $manager, array $instance_ids, Block $entity) {
    $this->manager = $manager;
    $this->entity = $entity;
    $this->instanceIDs = drupal_map_assoc($instance_ids);
  }

  /**
   * {@inheritdoc}
   */
  protected function initializePlugin($instance_id) {
    if (!$instance_id) {
      throw new PluginException(format_string("The block '@block' did not specify a plugin.", array(
        '@block' => $this->entity
          ->id(),
      )));
    }
    if (isset($this->pluginInstances[$instance_id])) {
      return;
    }
    $settings = $this->entity
      ->get('settings');
    try {
      $this->pluginInstances[$instance_id] = $this->manager
        ->createInstance($instance_id, $settings);
    } catch (PluginException $e) {
      $module = $settings['module'];

      // Ignore blocks belonging to disabled modules, but re-throw valid
      // exceptions when the module is enabled and the plugin is misconfigured.
      if (!$module || \Drupal::moduleHandler()
        ->moduleExists($module)) {
        throw $e;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginBag::$manager protected property The manager used to instantiate the plugins.
BlockPluginBag::initializePlugin protected function Initializes a plugin and stores the result in $this->pluginInstances. Overrides PluginBag::initializePlugin
BlockPluginBag::__construct public function Constructs a BlockPluginBag object.
PluginBag::$instanceIDs protected property Stores the IDs of all potential plugin instances.
PluginBag::$pluginInstances protected property Stores all instantiated plugins.
PluginBag::addInstanceID public function Adds an instance ID to the array of available instance IDs.
PluginBag::clear public function Clears all instantiated plugins. 1
PluginBag::count public function Implements \Countable::count().
PluginBag::current public function Implements \Iterator::current().
PluginBag::get public function Retrieves a plugin instance, initializing it if necessary.
PluginBag::getInstanceIDs public function Returns all instance IDs.
PluginBag::has public function Determines if a plugin instance exists.
PluginBag::key public function Implements \Iterator::key().
PluginBag::next public function Implements \Iterator::next().
PluginBag::remove public function Removes an initialized plugin. 1
PluginBag::rewind public function Implements \Iterator::rewind().
PluginBag::set public function Stores an initialized plugin.
PluginBag::setInstanceIDs public function Sets the instance IDs property.
PluginBag::valid public function Implements \Iterator::valid().