1

Edit: I just realized that Taxonomy permissions are not native to Drupal core, they are the result of a module we activated: Permissions by Term. (This will explain why they are not being respected by Drupal). The rest of my question still stands though:

I am able to fetch all the terms in a taxonomy programmatically, by doing the following:

$vid = 'TAXONOMY_NAME';
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, NULL, TRUE);
foreach ($terms as $term) {
   $tid = $term->id();
   $term_name = $term->name;
   //etc.
}

However, I noticed that if I apply "Permissions" to a term, this is not respected. For instance, if a term is limited to "Administrator" only, you will still see it as a "Authenticated User", (non-Administrator).

How can I amend the above code so that it only returns the terms that the logged in user has permissions for?

Edit: I know the answer has to do with using:

$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$access_check_service = \Drupal::service('permissions_by_term.access_check');
$allowed = $access_check_service->isAccessAllowedByDatabase($TERM_ID, $USER_ID, $langcode);

I'm just not sure of the proper implementation...

Many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.