Skip to content
Snippets Groups Projects
Commit f71a15f8 authored by Lawxen Liu's avatar Lawxen Liu
Browse files

Issue #3464911 by lawxen: Upload initial functioanl code

parents
No related branches found
No related tags found
No related merge requests found
name: jsonapi_taxonomy_tree
type: module
description: jsonapi_taxonomy_tree
core_version_requirement: '^10 || ^11'
package: Web services
dependencies:
- drupal:jsonapi
jsonapi.taxonomy_tree:
path: '/api/taxonomy_tree/{taxonomy_vocabulary}'
defaults:
_controller: '\Drupal\jsonapi_taxonomy_tree\Controller\JsonapiTaxonomyTree::fetchJsonapiData'
requirements:
_permission: 'access content'
options:
parameters:
taxonomy_vocabulary:
type: 'entity:taxonomy_vocabulary'
<?php
namespace Drupal\jsonapi_taxonomy_tree\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\taxonomy\VocabularyInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* An example controller.
*/
class JsonapiTaxonomyTree extends ControllerBase {
/**
* The HTTP client to fetch the JSON:API data.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Constructs a JsonapiTaxonomyTree object.
*
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP client to fetch the JSON:API data.
*/
public function __construct(ClientInterface $http_client) {
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('http_client')
);
}
/**
* Makes a request to the JSON:API endpoint.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*/
public function fetchJsonapiData(VocabularyInterface $taxonomy_vocabulary) {
$vid = $taxonomy_vocabulary->id();
$request = \Drupal::request();
$queryParams = $request->getQueryString();
$baseUrl = \Drupal::service('url_generator')
->generateFromRoute('jsonapi.taxonomy_term--' . $vid . '.collection', [], ['absolute' => TRUE]);
$baseUrl = $baseUrl . '?' . $queryParams;
$jsonapi_data = [];
$nextUrl = $baseUrl;
try {
do {
$response = $this->httpClient->request('GET', $nextUrl);
$data = json_decode($response->getBody()->getContents(), TRUE);
$jsonapi_data = array_merge($jsonapi_data, $data['data']);
// Check if there is a next page
$nextUrl = isset($data['links']['next']['href']) ? $data['links']['next']['href'] : NULL;
}
while ($nextUrl);
$return_data = $this->processData($vid, $jsonapi_data);
return new JsonResponse($return_data);
}
catch (\Exception $e) {
return new JsonResponse(['error' => $e->getMessage()], 500);
}
}
/**
* Process json api data as taxonomy tree
*
* @param string $vid
* @param $jsonapi_data
*
* @return array
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function processData(string $vid, $jsonapi_data) {
$jsonapi_data = $this->processDataKeyAsTid($jsonapi_data);
$data = [];
/** @var \Drupal\taxonomy\TermStorage $taxonomy_term_storage */
$taxonomy_term_storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$first_level_terms = $taxonomy_term_storage->loadTree($vid, 0, 1, TRUE);
foreach ($first_level_terms as $term) {
$data[] = $this->getTermChildren($vid, $term, $taxonomy_term_storage, $jsonapi_data);
}
return $data;
}
/**
* Process json data's key as tid.
*
* @param $jsonapi_data
*
* @return array
*/
protected function processDataKeyAsTid($jsonapi_data) {
$Processed_jsonapi_data = [];
foreach ($jsonapi_data as $value) {
$Processed_jsonapi_data[$value['drupal_internal__tid']] = $value;
}
return $Processed_jsonapi_data;
}
/**
* Recursively get children of a term.
*
* @param string $vid
* @param $term
* @param $taxonomy_term_storage
* @param $jsonapi_data
*
* @return array
*/
private function getTermChildren(string $vid, $term, $taxonomy_term_storage, $jsonapi_data) {
$tid = $term->id();
$term_data = $jsonapi_data[$tid];
$children = $taxonomy_term_storage->loadTree($vid, $term->id(), 1, TRUE);
/** @var \Drupal\taxonomy\Entity\Term $child */
foreach ($children as $child) {
$term_data['children'][] = $this->getTermChildren($vid, $child, $taxonomy_term_storage, $jsonapi_data);
}
return $term_data;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment