BlockManager.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Context\ContextAwarePluginManagerTrait;
  8. use Drupal\Core\Plugin\DefaultPluginManager;
  9. /**
  10. * Manages discovery and instantiation of block plugins.
  11. *
  12. * @todo Add documentation to this class.
  13. *
  14. * @see \Drupal\Core\Block\BlockPluginInterface
  15. */
  16. class BlockManager extends DefaultPluginManager implements BlockManagerInterface, FallbackPluginManagerInterface {
  17. use CategorizingPluginManagerTrait {
  18. getSortedDefinitions as traitGetSortedDefinitions;
  19. }
  20. use ContextAwarePluginManagerTrait;
  21. /**
  22. * Constructs a new \Drupal\Core\Block\BlockManager object.
  23. *
  24. * @param \Traversable $namespaces
  25. * An object that implements \Traversable which contains the root paths
  26. * keyed by the corresponding namespace to look for plugin implementations.
  27. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  28. * Cache backend instance to use.
  29. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  30. * The module handler to invoke the alter hook with.
  31. */
  32. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
  33. parent::__construct('Plugin/Block', $namespaces, $module_handler, 'Drupal\Core\Block\BlockPluginInterface', 'Drupal\Core\Block\Annotation\Block');
  34. $this->alterInfo('block');
  35. $this->setCacheBackend($cache_backend, 'block_plugins');
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function processDefinition(&$definition, $plugin_id) {
  41. parent::processDefinition($definition, $plugin_id);
  42. $this->processDefinitionCategory($definition);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getSortedDefinitions(array $definitions = NULL) {
  48. // Sort the plugins first by category, then by admin label.
  49. $definitions = $this->traitGetSortedDefinitions($definitions, 'admin_label');
  50. // Do not display the 'broken' plugin in the UI.
  51. unset($definitions['broken']);
  52. return $definitions;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getFallbackPluginId($plugin_id, array $configuration = []) {
  58. return 'broken';
  59. }
  60. }