Pull merge.
[yaffs-website] / web / core / modules / block / src / BlockPluginCollection.php
1 <?php
2
3 namespace Drupal\block;
4
5 use Drupal\Component\Plugin\Exception\PluginException;
6 use Drupal\Component\Plugin\PluginManagerInterface;
7 use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
8
9 /**
10  * Provides a collection of block plugins.
11  */
12 class BlockPluginCollection extends DefaultSingleLazyPluginCollection {
13
14   /**
15    * The block ID this plugin collection belongs to.
16    *
17    * @var string
18    */
19   protected $blockId;
20
21   /**
22    * Constructs a new BlockPluginCollection.
23    *
24    * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
25    *   The manager to be used for instantiating plugins.
26    * @param string $instance_id
27    *   The ID of the plugin instance.
28    * @param array $configuration
29    *   An array of configuration.
30    * @param string $block_id
31    *   The unique ID of the block entity using this plugin.
32    */
33   public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration, $block_id) {
34     parent::__construct($manager, $instance_id, $configuration);
35
36     $this->blockId = $block_id;
37   }
38
39   /**
40    * {@inheritdoc}
41    *
42    * @return \Drupal\Core\Block\BlockPluginInterface
43    */
44   public function &get($instance_id) {
45     return parent::get($instance_id);
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   protected function initializePlugin($instance_id) {
52     if (!$instance_id) {
53       throw new PluginException("The block '{$this->blockId}' did not specify a plugin.");
54     }
55
56     try {
57       parent::initializePlugin($instance_id);
58     }
59     catch (PluginException $e) {
60       $module = $this->configuration['provider'];
61       // Ignore blocks belonging to uninstalled modules, but re-throw valid
62       // exceptions when the module is installed and the plugin is
63       // misconfigured.
64       if (!$module || \Drupal::moduleHandler()->moduleExists($module)) {
65         throw $e;
66       }
67     }
68   }
69
70 }