PluginHierarchyTrait.php in Plugin 8.2
File
src/PluginHierarchyTrait.php
View source
<?php
namespace Drupal\plugin;
use Drupal\plugin\PluginDefinition\PluginDefinitionInterface;
use Drupal\plugin\PluginDefinition\PluginLabelDefinitionInterface;
use Drupal\plugin\PluginDefinition\PluginHierarchyDefinitionInterface;
use Drupal\plugin\PluginDiscovery\TypedDiscoveryInterface;
trait PluginHierarchyTrait {
protected function buildPluginHierarchy(TypedDiscoveryInterface $plugin_discovery) {
$parents = [];
$children = [];
$definitions = $plugin_discovery
->getDefinitions();
uasort($definitions, function (PluginDefinitionInterface $definition_a, PluginDefinitionInterface $definition_b) {
$label_a = $definition_a instanceof PluginLabelDefinitionInterface ? $definition_a
->getLabel() : $definition_a
->getId();
$label_b = $definition_b instanceof PluginLabelDefinitionInterface ? $definition_b
->getLabel() : $definition_b
->getId();
return strcmp($label_a, $label_b);
});
foreach ($definitions as $plugin_id => $plugin_definition) {
if ($plugin_definition instanceof PluginHierarchyDefinitionInterface && $plugin_definition
->getParentId()) {
$children[$plugin_definition
->getParentId()][] = $plugin_id;
}
else {
$parents[] = $plugin_id;
}
}
return $this
->buildPluginHierarchyLevel($parents, $children);
}
protected function buildPluginHierarchyLevel(array $parent_plugin_ids, array $child_plugin_ids) {
$hierarchy = [];
foreach ($parent_plugin_ids as $plugin_id) {
$hierarchy[$plugin_id] = isset($child_plugin_ids[$plugin_id]) ? $this
->buildPluginHierarchyLevel($child_plugin_ids[$plugin_id], $child_plugin_ids) : [];
}
return $hierarchy;
}
}