diff --git a/core/lib/Drupal/Core/Block/BlockManager.php b/core/lib/Drupal/Core/Block/BlockManager.php index 75f9527..661a3df 100644 --- a/core/lib/Drupal/Core/Block/BlockManager.php +++ b/core/lib/Drupal/Core/Block/BlockManager.php @@ -100,9 +100,9 @@ public function getCategories() { /** * {@inheritdoc} */ - public function getSortedDefinitions() { + public function getSortedDefinitions(array $contexts = array()) { // Sort the plugins first by category, then by label. - $definitions = $this->getDefinitionsForContexts(); + $definitions = $this->getDefinitionsForContexts($contexts); uasort($definitions, function ($a, $b) { if ($a['category'] != $b['category']) { return strnatcasecmp($a['category'], $b['category']); diff --git a/core/lib/Drupal/Core/Block/BlockManagerInterface.php b/core/lib/Drupal/Core/Block/BlockManagerInterface.php index 2fa8611..8b984b2 100644 --- a/core/lib/Drupal/Core/Block/BlockManagerInterface.php +++ b/core/lib/Drupal/Core/Block/BlockManagerInterface.php @@ -25,9 +25,11 @@ public function getCategories(); /** * Gets the sorted definitions. * + * @param array $contexts + * * @return array * An array of plugin definitions, sorted by category and admin label. */ - public function getSortedDefinitions(); + public function getSortedDefinitions(array $contexts = array()); } diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php index 1d11f99..1b507cb 100644 --- a/core/modules/block/src/BlockListBuilder.php +++ b/core/modules/block/src/BlockListBuilder.php @@ -7,6 +7,8 @@ namespace Drupal\block; +use Drupal\block\Event\BlockContextEvent; +use Drupal\block\Event\BlockEvents; use Drupal\Core\Block\BlockManagerInterface; use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\String; @@ -18,6 +20,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; /** @@ -56,6 +59,13 @@ class BlockListBuilder extends ConfigEntityListBuilder implements FormInterface protected $blockManager; /** + * The event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + + /** * Constructs a new BlockListBuilder object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type @@ -64,11 +74,14 @@ class BlockListBuilder extends ConfigEntityListBuilder implements FormInterface * The entity storage class. * @param \Drupal\Core\Block\BlockManagerInterface $block_manager * The block manager. + * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher + * The event dispatcher. */ - public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, BlockManagerInterface $block_manager) { + public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, BlockManagerInterface $block_manager, EventDispatcherInterface $event_dispatcher) { parent::__construct($entity_type, $storage); $this->blockManager = $block_manager; + $this->eventDispatcher = $event_dispatcher; } /** @@ -78,7 +91,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI return new static( $entity_type, $container->get('entity.manager')->getStorage($entity_type->id()), - $container->get('plugin.manager.block') + $container->get('plugin.manager.block'), + $container->get('event_dispatcher') ); } @@ -335,7 +349,9 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['place_blocks']['list']['#attributes']['class'][] = 'entity-meta'; // Sort the plugins first by category, then by label. - $plugins = $this->blockManager->getSortedDefinitions(); + $event = new BlockContextEvent(); + $contexts = $this->eventDispatcher->dispatch(BlockEvents::ADMINISTRATIVE_CONTEXT, $event)->getContexts(); + $plugins = $this->blockManager->getSortedDefinitions($contexts); foreach ($plugins as $plugin_id => $plugin_definition) { $category = String::checkPlain($plugin_definition['category']); $category_key = 'category-' . $category;