public function ShsController::getTermData in Simple hierarchical select 8
Same name and namespace in other branches
- 2.0.x src/Controller/ShsController.php \Drupal\shs\Controller\ShsController::getTermData()
Load term data.
Parameters
string $identifier: Name of field to load the data for.
string $bundle: Bundle (vocabulary) identifier to limit the return list to a specific bundle.
int $entity_id: Id of parent term to load all children for. Defaults to 0.
Return value
CacheableJsonResponse Cacheable Json response.
1 string reference to 'ShsController::getTermData'
File
- src/
Controller/ ShsController.php, line 51
Class
- ShsController
- Controller for getting taxonomy terms.
Namespace
Drupal\shs\ControllerCode
public function getTermData($identifier, $bundle, $entity_id = 0) {
$context = [
'identifier' => $identifier,
'bundle' => $bundle,
'parent' => $entity_id,
];
$response = new ShsCacheableJsonResponse($context);
$cache_tags = [];
$result = [];
$langcode_current = $this
->languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
$storage = $this
->entityTypeManager()
->getStorage('taxonomy_term');
$translation_enabled = FALSE;
if ($this
->moduleHandler()
->moduleExists('content_translation')) {
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $translation_manager */
$translation_manager = $this->container
->get('content_translation.manager');
// If translation is enabled for the vocabulary, we need to load the full
// term objects to get the translation for the current language.
$translation_enabled = $translation_manager
->isEnabled('taxonomy_term', $bundle);
}
$terms = $storage
->loadTree($bundle, $entity_id, 1, $translation_enabled);
foreach ($terms as $term) {
$langcode = $langcode_current;
if ($translation_enabled && $term
->hasTranslation($langcode)) {
$term = $term
->getTranslation($langcode);
}
else {
$langcode = $term->default_langcode;
}
$tid = $translation_enabled ? $term
->id() : $term->tid;
$result[] = (object) [
'tid' => $tid,
'name' => $translation_enabled ? $term
->getName() : $term->name,
'description__value' => $translation_enabled ? $term
->getDescription() : $term->description__value,
'langcode' => $langcode,
'hasChildren' => shs_term_has_children($tid),
];
$cache_tags[] = sprintf('taxonomy_term:%d', $tid);
}
$response
->addCacheableDependency(new ShsTermCacheDependency($cache_tags));
$response
->setData($result, TRUE);
return $response;
}