BlockPluginCollection.php 1.8 KB

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