1

I want to be able to programmatically enable and set taxonomy access control for the anonymous and authenticated roles. Specifically, I want to deny anonymous and authenticated users from being able to view or edit nodes tagged with a particular term. I know how to do this through the admin interface. But I'd like to be able to do this programmatically.

I can see functions to get the grants for a particular role, but not set them.

Thanks!

1
  • Hoping that adding a comment will push this back to the top, maybe someone knows. I've programmatically created two new terms in a vocabulary and now want to enable taxonomy access control for them both. I'd really rather not step through the debugger, but seems this might be my only choice. Hard to believe no one has done this before.
    – Patrick
    Commented Oct 2, 2013 at 3:20

3 Answers 3

1
/**
 * Implements hook_node_access().
 */
function accommodation_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
  if ($node->bundle() == 'accommodation' && \Drupal::currentUser()->isAuthenticated())  {
    if ($node->get('field_accommodation_category')->entity->getName() == 'Category 1') {
      return \Drupal\Core\Access\AccessResult::allowed();
    }

    return \Drupal\Core\Access\AccessResult::forbidden();
  }

  return \Drupal\Core\Access\AccessResult::neutral();
}
1

Check these two modules, both do your task.
Permissions by Term (Drupal 8/9)
Taxonomy Access Control Lite (D7 & D8/9)

0

Check out this page: http://drupalcontrib.org/api/drupal/contributions!taxonomy_access!taxonomy_access.module/group/tac_grant_api/

I'm currently attempting to implement the same functionality and it seems that this provides everything I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.