commit e87ebfa022c6adba24fc14fe1cc0c702bb216a61 Author: fago Date: Fri Oct 3 17:52:44 2014 +0200 Issue #2349991: Added trait and use it for conditions and actions. diff --git a/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php b/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php new file mode 100644 index 0000000..6e2b870 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php @@ -0,0 +1,40 @@ +getModuleName($definition['provider']); - } - } - - /** - * Gets the name of the module. - * - * @param string $module - * The machine name of a module. - * - * @return string - * The human-readable module name if it exists, otherwise the - * machine-readable module name. - */ - protected function getModuleName($module) { - // Gather module data. - if (!isset($this->moduleData)) { - $this->moduleData = system_get_info('module'); - } - // If the module exists, return its human-readable name. - if (isset($this->moduleData[$module])) { - return $this->t($this->moduleData[$module]['name']); - } - // Otherwise, return the machine name. - return $module; - } - - /** - * {@inheritdoc} - */ - public function getCategories() { - $categories = array_unique(array_values(array_map(function ($definition) { - return $definition['category']; - }, $this->getDefinitions()))); - natcasesort($categories); - return $categories; + $this->processDefinitionCategory($definition); } /** @@ -102,13 +67,7 @@ public function getCategories() { */ public function getSortedDefinitions() { // Sort the plugins first by category, then by label. - $definitions = $this->getDefinitionsForContexts(); - uasort($definitions, function ($a, $b) { - if ($a['category'] != $b['category']) { - return strnatcasecmp($a['category'], $b['category']); - } - return strnatcasecmp($a['admin_label'], $b['admin_label']); - }); + $definitions = $this->traitGetSortedDefinitions('admin_label'); // Do not display the 'broken' plugin in the UI. unset($definitions['broken']); return $definitions; diff --git a/core/lib/Drupal/Core/Block/BlockManagerInterface.php b/core/lib/Drupal/Core/Block/BlockManagerInterface.php index 2fa8611..592a9a3 100644 --- a/core/lib/Drupal/Core/Block/BlockManagerInterface.php +++ b/core/lib/Drupal/Core/Block/BlockManagerInterface.php @@ -7,27 +7,12 @@ namespace Drupal\Core\Block; +use Drupal\Component\Plugin\CategorizingPluginManagerInterface; use Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface; /** * Provides an interface for the discovery and instantiation of block plugins. */ -interface BlockManagerInterface extends ContextAwarePluginManagerInterface { - - /** - * Gets the names of all block categories. - * - * @return array - * An array of translated categories, sorted alphabetically. - */ - public function getCategories(); - - /** - * Gets the sorted definitions. - * - * @return array - * An array of plugin definitions, sorted by category and admin label. - */ - public function getSortedDefinitions(); +interface BlockManagerInterface extends ContextAwarePluginManagerInterface, CategorizingPluginManagerInterface { } diff --git a/core/lib/Drupal/Core/Condition/ConditionManager.php b/core/lib/Drupal/Core/Condition/ConditionManager.php index c1e309a..809f28f 100644 --- a/core/lib/Drupal/Core/Condition/ConditionManager.php +++ b/core/lib/Drupal/Core/Condition/ConditionManager.php @@ -7,10 +7,12 @@ namespace Drupal\Core\Condition; +use Drupal\Component\Plugin\CategorizingPluginManagerInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Executable\ExecutableManagerInterface; use Drupal\Core\Executable\ExecutableInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Plugin\CategorizingPluginManagerTrait; use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait; use Drupal\Core\Plugin\DefaultPluginManager; @@ -23,8 +25,9 @@ * * @ingroup plugin_api */ -class ConditionManager extends DefaultPluginManager implements ExecutableManagerInterface { +class ConditionManager extends DefaultPluginManager implements ExecutableManagerInterface, CategorizingPluginManagerInterface { + use CategorizingPluginManagerTrait; use ContextAwarePluginManagerTrait; /** diff --git a/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php new file mode 100644 index 0000000..d4a833e --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php @@ -0,0 +1,112 @@ +getModuleName($definition['provider']); + } + } + + /** + * Gets the name of the module. + * + * @param string $module + * The machine name of a module. + * + * @return string + * The human-readable module name if it exists, otherwise the + * machine-readable module name. + */ + protected function getModuleName($module) { + // Gather module data. + if (!isset($this->moduleData)) { + $this->moduleData = system_get_info('module'); + } + // If the module exists, return its human-readable name. + if (isset($this->moduleData[$module])) { + return $this->t($this->moduleData[$module]['name']); + } + // Otherwise, return the machine name. + return $module; + } + + /** + * See \Drupal\Component\Plugin\CategorizingPluginManagerInterface::getCategories(). + */ + public function getCategories() { + $categories = array_unique(array_values(array_map(function ($definition) { + return $definition['category']; + }, $this->getDefinitions()))); + natcasesort($categories); + return $categories; + } + + /** + * See \Drupal\Component\Plugin\CategorizingPluginManagerInterface::getSortedDefinitions(). + * + * @param string $label_key + * (optional) The key used for the label in plugin definitions. Defaults to + * label. + */ + public function getSortedDefinitions($label_key = 'label') { + // Sort the plugins first by category, then by label. + $definitions = $this->getDefinitions(); + uasort($definitions, function ($a, $b) use ($label_key) { + if ($a['category'] != $b['category']) { + return strnatcasecmp($a['category'], $b['category']); + } + return strnatcasecmp($a[$label_key], $b[$label_key]); + }); + return $definitions; + } + + /** + * See \Drupal\Component\Plugin\CategorizingPluginManagerInterface::getGroupedDefinitions(). + */ + public function getGroupedDefinitions() { + $grouped_definitions = array(); + foreach ($this->getDefinitions() as $id => $definition) { + $grouped_definitions[$definition['category']][$id][$definition]; + } + return $grouped_definitions; + } + +}