diff --git a/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php b/core/lib/Drupal/Component/Plugin/CategorizingPluginManagerInterface.php new file mode 100644 index 0000000..67e0e8a --- /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; + } + + /** + * 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; + } + +}