diff --git a/core/modules/block/lib/Drupal/block/BlockPluginBag.php b/core/modules/block/lib/Drupal/block/BlockPluginBag.php new file mode 100644 index 0000000..4247398 --- /dev/null +++ b/core/modules/block/lib/Drupal/block/BlockPluginBag.php @@ -0,0 +1,74 @@ +instanceIDs = array($block->get('plugin')); + $this->manager = $manager; + $this->block = $block; + } + + /** + * Implements \Drupal\Component\Plugin\PluginBag::initializePlugin(). + */ + protected function initializePlugin($instance_id) { + // If the plugin was initialized before, just return. + if (isset($this->pluginInstances[$instance_id])) { + return; + } + + try { + $configuration = $this->block->get('configuration'); + $this->pluginInstances[$instance_id] = $this->manager->createInstance($instance_id, $configuration); + $configuration += $this->pluginInstances[$instance_id]->getConfig(); + $this->block->set('configuration', $configuration); + } + catch (PluginException $e) { + // Ignore blocks belonging to disabled modules, but re-throw valid + // exceptions when the module is enabled and the plugin is misconfigured. + if (module_exists($this->block->get('module'))) { + throw $e; + } + } + } + +} diff --git a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php index e8ccc63..fbe36b1 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php +++ b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php @@ -10,7 +10,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Annotation\Plugin; use Drupal\Core\Annotation\Translation; -use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\block\BlockPluginBag; /** * Defines a Block configuration entity class. @@ -128,26 +128,29 @@ class Block extends ConfigEntityBase { protected $plugin; /** + * Stores instantiated plugins for this block. + * + * @var \Drupal\block\BlockPluginBag + */ + protected $pluginBag; + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::__construct(); + */ + public function __construct(array $values, $entity_type) { + parent::__construct($values, $entity_type); + + $this->pluginBag = new BlockPluginBag($this, drupal_container()->get('plugin.manager.block')); + } + + /** * Returns the plugin instance. * * @return \Drupal\block\BlockInterface * The plugin instance for this block. */ public function getPlugin() { - if (!$this->instance) { - try { - $this->instance = drupal_container()->get('plugin.manager.block')->createInstance($this->plugin, $this->configuration); - $this->configuration += $this->instance->getConfig(); - } - catch (PluginException $e) { - // Ignore blocks belonging to disabled modules, but re-throw valid - // exceptions when the module is enabled and the plugin is misconfigured. - if (module_exists($this->module)) { - throw $e; - } - } - } - return $this->instance; + return $this->pluginBag->get($this->plugin); } /**