VocabularyTreeGet.php in Services 8.4
File
src/Plugin/ServiceDefinition/VocabularyTreeGet.php
View source
<?php
namespace Drupal\services\Plugin\ServiceDefinition;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\services\ServiceDefinitionBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\SerializerInterface;
class VocabularyTreeGet extends ServiceDefinitionBase implements ContainerFactoryPluginInterface {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
$vocabulary_id = $request
->get('vocabulary');
$termStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$terms = $termStorage
->loadTree($vocabulary_id);
$terms_array = [];
foreach ($terms as $term) {
$terms_array[] = (array) $term;
}
return $terms_array;
}
}
Classes
Name |
Description |
VocabularyTreeGet |
Plugin annotation
@ServiceDefinition(
id = "vocabulary_tree_get",
methods = {
"GET"
},
translatable = true,
title = @Translation("Taxonomy Vocabulary Get Tree"),
description = @Translation("Returns term hierarchy."),
category =… |