CategorizingPluginManagerTrait.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Drupal\Core\Plugin;
  3. use Drupal\Core\StringTranslation\StringTranslationTrait;
  4. /**
  5. * Provides a trait for the CategorizingPluginManagerInterface.
  6. *
  7. * The trait provides methods for categorizing plugin definitions based on a
  8. * 'category' key. The plugin manager should make sure there is a default
  9. * category. For that the trait's processDefinitionCategory() method can be
  10. * invoked from the processDefinition() method.
  11. *
  12. * @see \Drupal\Component\Plugin\CategorizingPluginManagerInterface
  13. */
  14. trait CategorizingPluginManagerTrait {
  15. use StringTranslationTrait;
  16. /**
  17. * Processes a plugin definition to ensure there is a category.
  18. *
  19. * If the definition lacks a category, it defaults to the providing module.
  20. *
  21. * @param array $definition
  22. * The plugin definition.
  23. */
  24. protected function processDefinitionCategory(&$definition) {
  25. // Ensure that every plugin has a category.
  26. if (empty($definition['category'])) {
  27. // Default to the human readable module name if the provider is a module;
  28. // otherwise the provider machine name is used.
  29. $definition['category'] = $this->getProviderName($definition['provider']);
  30. }
  31. }
  32. /**
  33. * Gets the name of a provider.
  34. *
  35. * @param string $provider
  36. * The machine name of a plugin provider.
  37. *
  38. * @return string
  39. * The human-readable module name if it exists, otherwise the
  40. * machine-readable name passed.
  41. */
  42. protected function getProviderName($provider) {
  43. $list = $this->getModuleHandler()->getModuleList();
  44. // If the module exists, return its human-readable name.
  45. if (isset($list[$provider])) {
  46. return $this->getModuleHandler()->getName($provider);
  47. }
  48. // Otherwise, return the machine name.
  49. return $provider;
  50. }
  51. /**
  52. * Returns the module handler used.
  53. *
  54. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  55. * The module handler.
  56. */
  57. public function getModuleHandler() {
  58. // If the class has an injected module handler, use it. Otherwise fall back
  59. // to fetch it from the service container.
  60. if (isset($this->moduleHandler)) {
  61. return $this->moduleHandler;
  62. }
  63. return \Drupal::moduleHandler();
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getCategories() {
  69. /** @var \Drupal\Core\Plugin\CategorizingPluginManagerTrait|\Drupal\Component\Plugin\PluginManagerInterface $this */
  70. // Fetch all categories from definitions and remove duplicates.
  71. $categories = array_unique(array_values(array_map(function ($definition) {
  72. return $definition['category'];
  73. }, $this->getDefinitions())));
  74. natcasesort($categories);
  75. return $categories;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getSortedDefinitions(array $definitions = NULL, $label_key = 'label') {
  81. // Sort the plugins first by category, then by label.
  82. /** @var \Drupal\Core\Plugin\CategorizingPluginManagerTrait|\Drupal\Component\Plugin\PluginManagerInterface $this */
  83. $definitions = isset($definitions) ? $definitions : $this->getDefinitions();
  84. uasort($definitions, function ($a, $b) use ($label_key) {
  85. if ($a['category'] != $b['category']) {
  86. return strnatcasecmp($a['category'], $b['category']);
  87. }
  88. return strnatcasecmp($a[$label_key], $b[$label_key]);
  89. });
  90. return $definitions;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label') {
  96. /** @var \Drupal\Core\Plugin\CategorizingPluginManagerTrait|\Drupal\Component\Plugin\PluginManagerInterface $this */
  97. $definitions = $this->getSortedDefinitions(isset($definitions) ? $definitions : $this->getDefinitions(), $label_key);
  98. $grouped_definitions = [];
  99. foreach ($definitions as $id => $definition) {
  100. $grouped_definitions[(string) $definition['category']][$id] = $definition;
  101. }
  102. return $grouped_definitions;
  103. }
  104. }