diff --git a/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php b/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php index 6e2b870..2a3afab 100644 --- a/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php +++ b/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php @@ -21,20 +21,27 @@ public function getCategories(); /** - * Gets the sorted definitions. + * Gets sorted definitions. + * + * @param array $definitions + * (optional) The definitions to sort. If omitted, all definitions are used. * * @return array[] * An array of plugin definitions, sorted by category and label. */ - public function getSortedDefinitions(); + public function getSortedDefinitions(array $definitions = NULL); /** * Gets definitions grouped by category. * + * @param array $definitions + * (optional) The definitions to group. If omitted, all definitions are + * used. + * * @return array[] * A nested array containing plugin definitions, keyed by category and * plugin id. */ - public function getGroupedDefinitions(); + public function getGroupedDefinitions(array $definitions = NULL); } diff --git a/core/lib/Drupal/Core/Block/BlockManager.php b/core/lib/Drupal/Core/Block/BlockManager.php index ae73467..f87485e 100644 --- a/core/lib/Drupal/Core/Block/BlockManager.php +++ b/core/lib/Drupal/Core/Block/BlockManager.php @@ -58,9 +58,9 @@ public function processDefinition(&$definition, $plugin_id) { /** * {@inheritdoc} */ - public function getSortedDefinitions() { + public function getSortedDefinitions(array $definitions = NULL) { // Sort the plugins first by category, then by label. - $definitions = $this->traitGetSortedDefinitions('admin_label'); + $definitions = $this->traitGetSortedDefinitions($definitions, 'admin_label'); // Do not display the 'broken' plugin in the UI. unset($definitions['broken']); return $definitions; @@ -69,8 +69,8 @@ public function getSortedDefinitions() { /** * {@inheritdoc} */ - public function getGroupedDefinitions() { - $definitions = $this->traitGetGroupedDefinitions(); + public function getGroupedDefinitions(array $definitions = NULL) { + $definitions = $this->traitGetGroupedDefinitions($definitions); // Do not display the 'broken' plugin in the UI. unset($definitions[$this->t('Block')]['broken']); return $definitions; diff --git a/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php index d3cf082..f6f283e 100644 --- a/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php +++ b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php @@ -86,9 +86,9 @@ public function getCategories() { * (optional) The key used for the label in plugin definitions. Defaults to * label. */ - public function getSortedDefinitions($label_key = 'label') { + public function getSortedDefinitions(array $definitions = NULL, $label_key = 'label') { // Sort the plugins first by category, then by label. - $definitions = $this->getDefinitions(); + $definitions = isset($definitions) ? $definitions : $this->getDefinitions(); uasort($definitions, function ($a, $b) use ($label_key) { if ($a['category'] != $b['category']) { return strnatcasecmp($a['category'], $b['category']); @@ -101,9 +101,10 @@ public function getSortedDefinitions($label_key = 'label') { /** * See \Drupal\Component\Plugin\CategorizingPluginManagerInterface::getGroupedDefinitions(). */ - public function getGroupedDefinitions() { + public function getGroupedDefinitions(array $definitions = NULL) { + $definitions = isset($definitions) ? $definitions : $this->getDefinitions(); $grouped_definitions = array(); - foreach ($this->getDefinitions() as $id => $definition) { + foreach ($definitions as $id => $definition) { $grouped_definitions[$definition['category']][$id] = $definition; } return $grouped_definitions; diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php index e5da65d..d4a8a99 100644 --- a/core/modules/block/src/BlockListBuilder.php +++ b/core/modules/block/src/BlockListBuilder.php @@ -333,9 +333,10 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['place_blocks']['list']['#type'] = 'container'; $form['place_blocks']['list']['#attributes']['class'][] = 'entity-meta'; - // Sort the plugins first by category, then by label. - $plugins = $this->blockManager->getSortedDefinitions(); - foreach ($plugins as $plugin_id => $plugin_definition) { + // Only add blocks which work without any available context. + $definitions = $this->blockManager->getDefinitionsForContexts(); + $sorted_definitions = $this->blockManager->getSortedDefinitions($definitions); + foreach ($sorted_definitions as $plugin_id => $plugin_definition) { $category = String::checkPlain($plugin_definition['category']); $category_key = 'category-' . $category; if (!isset($form['place_blocks']['list'][$category_key])) {