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