diff --git a/cshs.api.php b/cshs.api.php new file mode 100644 index 0000000..fe055a7 --- /dev/null +++ b/cshs.api.php @@ -0,0 +1,41 @@ + $term->name; } + // Contexts for alter hook. + $account = \Drupal::entityTypeManager()->getStorage('user')->load(\Drupal::currentUser()->id()); + $field_name = (isset($this->fieldDefinition) && method_exists($this->fieldDefinition, 'getName')) ? $this->fieldDefinition?->getName() : NULL; + $hooks = [ + 'cshs_term_access', + ]; + if ($field_name) { + $hooks[] = 'cshs_' . $field_name . '_term_access'; + } + foreach ($this->getVocabularies() as $vocabulary) { // Historically vocabulary labels are not translatable // and the `t()` trick should be applied. @@ -356,8 +366,20 @@ trait CshsOptionsFromHelper { \assert(\is_numeric($term->tid)); \assert(\is_string($term->name)); + $hasAccess = TRUE; + + $contexts = [ + 'term' => $term, + 'account' => $account, + 'field_name' => $field_name, + ]; + // Allow other modules to override access. + // Creates hook_cshs_term_access_alter() and + // hook_cshs_FIELD_NAME_term_access_alter(). + \Drupal::moduleHandler()->alter($hooks, $hasAccess, $contexts); + // Allow only published terms, unless configured to allow all. - if ($allow_unpublished || (bool) $term->status) { + if (($allow_unpublished || (bool) $term->status) && $hasAccess) { // The `parents` always has a value. In case there are no parents // the value is `['0']`. Check for an empty value just in case. $parent_tid = ((string) \reset($term->parents)) ?: '0';