|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Taxonomy; |
| 5 | + |
| 6 | +use Drupal\Core\Cache\RefinableCacheableDependencyInterface; |
| 7 | +use Drupal\Core\Entity\EntityTypeManager; |
| 8 | +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 9 | +use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; |
| 10 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Loads the taxonomy tree. |
| 14 | + * |
| 15 | + * @DataProducer( |
| 16 | + * id = "taxonomy_load_tree", |
| 17 | + * name = @Translation("Load multiple taxonomy terms"), |
| 18 | + * description = @Translation("Loads Taxonomy terms as a tree"), |
| 19 | + * produces = @ContextDefinition("taxonomy tree", |
| 20 | + * label = @Translation("Taxonomy tree") |
| 21 | + * ), |
| 22 | + * consumes = { |
| 23 | + * "vid" = @ContextDefinition("string", |
| 24 | + * label = @Translation("Vocabulary id") |
| 25 | + * ), |
| 26 | + * "parent" = @ContextDefinition("integer", |
| 27 | + * label = @Translation("The term ID under which to generate the tree"), |
| 28 | + * required = FALSE |
| 29 | + * ), |
| 30 | + * "max_depth" = @ContextDefinition("integer", |
| 31 | + * label = @Translation("Maximum tree depth"), |
| 32 | + * required = FALSE |
| 33 | + * ) |
| 34 | + * } |
| 35 | + * ) |
| 36 | + */ |
| 37 | +class TaxonomyLoadTree extends DataProducerPluginBase implements ContainerFactoryPluginInterface { |
| 38 | + |
| 39 | + /** |
| 40 | + * The default max depth to search in taxonomy tree if it is not set. |
| 41 | + */ |
| 42 | + const MAX_DEPTH = 10; |
| 43 | + |
| 44 | + /** |
| 45 | + * The entity type manager service. |
| 46 | + * |
| 47 | + * @var \Drupal\Core\Entity\EntityTypeManager |
| 48 | + */ |
| 49 | + protected $entityTypeManager; |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + * |
| 54 | + * @codeCoverageIgnore |
| 55 | + */ |
| 56 | + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 57 | + return new static( |
| 58 | + $configuration, |
| 59 | + $plugin_id, |
| 60 | + $plugin_definition, |
| 61 | + $container->get('entity_type.manager') |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * EntityLoad constructor. |
| 67 | + * |
| 68 | + * @param array $configuration |
| 69 | + * The plugin configuration array. |
| 70 | + * @param string $pluginId |
| 71 | + * The plugin id. |
| 72 | + * @param array $pluginDefinition |
| 73 | + * The plugin definition array. |
| 74 | + * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager |
| 75 | + * The entity type manager service. |
| 76 | + * |
| 77 | + * @codeCoverageIgnore |
| 78 | + */ |
| 79 | + public function __construct( |
| 80 | + array $configuration, |
| 81 | + string $pluginId, |
| 82 | + array $pluginDefinition, |
| 83 | + EntityTypeManager $entityTypeManager |
| 84 | + ) { |
| 85 | + parent::__construct($configuration, $pluginId, $pluginDefinition); |
| 86 | + $this->entityTypeManager = $entityTypeManager; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Resolves the taxonomy tree for given vocabulary. |
| 91 | + * |
| 92 | + * @param string $vid |
| 93 | + * The vocanulary ID. |
| 94 | + * @param int $parent |
| 95 | + * The ID of the parent's term to load the tree for. |
| 96 | + * @param int|null $max_depth |
| 97 | + * Max depth to search in. |
| 98 | + * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata |
| 99 | + * Refinable cacheability metadata. |
| 100 | + * |
| 101 | + * @return \object[] |
| 102 | + * A list of stdClass terms in the given vocabulary. |
| 103 | + */ |
| 104 | + public function resolve(string $vid, int $parent = 0, ?int $max_depth = NULL, RefinableCacheableDependencyInterface $metadata): array { |
| 105 | + if (!isset($max_depth)) { |
| 106 | + $max_depth = self::MAX_DEPTH; |
| 107 | + } |
| 108 | + // @todo This should use a buffer system similar to other entities are using. |
| 109 | + $terms = $this->entityTypeManager |
| 110 | + ->getStorage('taxonomy_term') |
| 111 | + ->loadTree($vid, $parent, $max_depth); |
| 112 | + |
| 113 | + return $terms; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments