public function VocabularyTreeGet::processRequest in Services 9.0.x
Same name and namespace in other branches
- 8.4 src/Plugin/ServiceDefinition/VocabularyTreeGet.php \Drupal\services\Plugin\ServiceDefinition\VocabularyTreeGet::processRequest()
Processes the request and returns an array of data as appropriate.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request object.
\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match object.
\Symfony\Component\Serializer\SerializerInterface $serializer: The serializer. Some methods might require the plugin to leverage the serializer after extracting the request contents.
Return value
array The response.
Throws
\Symfony\Component\HttpKernel\Exception\HttpException
Overrides ServiceDefinitionInterface::processRequest
File
- src/
Plugin/ ServiceDefinition/ VocabularyTreeGet.php, line 45
Class
- VocabularyTreeGet
- Plugin annotation @ServiceDefinition( id = "vocabulary_tree_get", methods = { "GET" }, translatable = true, title = @Translation("Taxonomy Vocabulary Get Tree"), description = @Translation("Returns term hierarchy."), category =…
Namespace
Drupal\services\Plugin\ServiceDefinitionCode
public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
// The query string parameter 'path' must exist in order to load the
// node that correlates to path value provided.
/*if (!$request->query->has('vocabulary')) {
throw new HttpException(404);
}*/
$vocabulary_id = $request
->get('vocabulary');
/* @var $termStorage \Drupal\taxonomy\TermStorageInterface */
$termStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
// Load taxonomy terms for tax menu vocab.
/* @var $terms \Drupal\taxonomy\TermInterface[] */
$terms = $termStorage
->loadTree($vocabulary_id);
$terms_array = [];
/* @var $term \Drupal\taxonomy\TermInterface */
foreach ($terms as $term) {
$terms_array[] = (array) $term;
}
return $terms_array;
}