BlockManager.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Drupal\Core\Block;
  3. use Drupal\Component\Plugin\FallbackPluginManagerInterface;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
  7. use Drupal\Core\Plugin\DefaultPluginManager;
  8. use Drupal\Core\Plugin\FilteredPluginManagerTrait;
  9. use Psr\Log\LoggerInterface;
  10. /**
  11. * Manages discovery and instantiation of block plugins.
  12. *
  13. * @todo Add documentation to this class.
  14. *
  15. * @see \Drupal\Core\Block\BlockPluginInterface
  16. */
  17. class BlockManager extends DefaultPluginManager implements BlockManagerInterface, FallbackPluginManagerInterface {
  18. use CategorizingPluginManagerTrait {
  19. getSortedDefinitions as traitGetSortedDefinitions;
  20. }
  21. use FilteredPluginManagerTrait;
  22. /**
  23. * The logger.
  24. *
  25. * @var \Psr\Log\LoggerInterface
  26. */
  27. protected $logger;
  28. /**
  29. * Constructs a new \Drupal\Core\Block\BlockManager object.
  30. *
  31. * @param \Traversable $namespaces
  32. * An object that implements \Traversable which contains the root paths
  33. * keyed by the corresponding namespace to look for plugin implementations.
  34. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  35. * Cache backend instance to use.
  36. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  37. * The module handler to invoke the alter hook with.
  38. * @param \Psr\Log\LoggerInterface $logger
  39. * The logger.
  40. */
  41. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, LoggerInterface $logger) {
  42. parent::__construct('Plugin/Block', $namespaces, $module_handler, 'Drupal\Core\Block\BlockPluginInterface', 'Drupal\Core\Block\Annotation\Block');
  43. $this->alterInfo($this->getType());
  44. $this->setCacheBackend($cache_backend, 'block_plugins');
  45. $this->logger = $logger;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function getType() {
  51. return 'block';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function processDefinition(&$definition, $plugin_id) {
  57. parent::processDefinition($definition, $plugin_id);
  58. $this->processDefinitionCategory($definition);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getSortedDefinitions(array $definitions = NULL) {
  64. // Sort the plugins first by category, then by admin label.
  65. $definitions = $this->traitGetSortedDefinitions($definitions, 'admin_label');
  66. // Do not display the 'broken' plugin in the UI.
  67. unset($definitions['broken']);
  68. return $definitions;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getFallbackPluginId($plugin_id, array $configuration = []) {
  74. return 'broken';
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function handlePluginNotFound($plugin_id, array $configuration) {
  80. $this->logger->warning('The "%plugin_id" was not found', ['%plugin_id' => $plugin_id]);
  81. return parent::handlePluginNotFound($plugin_id, $configuration);
  82. }
  83. }