0

I'd like to restrict access to content based on a term referenced in the node.

For example, I have a content type 'submission' and I want users with the role 'curator' to be only able to view nodes that have the taxonomy term 'art'.

Any pointers towards a sustainable solution to this?

  • What version of Drupal are you using? – Shawn Conn Apr 26 '16 at 17:00
  • Sorry, I'm using Drupal 7 – Danny Browne Apr 26 '16 at 18:13
1

If you're wanting a custom module based solution, Drupal has hook_node_access() which lets any module control access to a certain node. In your case, you want check the $node object on a 'view' $op and return the appropriate NODE_ACCESS_* constant which tells Drupal when the user has access to the node.

// @TODO: Replace 2 with your curator RID.
define('MYMODULE_CURATOR_ROLE_ID', 2);

function MYMODULE_has_terms($node) {
  // @TODO: Define logic that checks if node taxonomy field has certain terms.
  return FALSE;
}

/**
 * Implements hook_node_access().
 */
function test_node_access($node, $op, $account) {

  // Only step in when curators are viewing 'submission' nodes 
  // and allow access if a node is tagged with certain terms
  if (is_object($account) && user_has_role(MYMODULE_CURATOR_ROLE_ID, $account) ) {
    if (is_object($node) && $node->type == 'submission' && $op == 'view') {
      return MYMODULE_has_terms($node) ? NODE_ACCESS_ALLOW : NODE_ACCESS_DENY;
    }
  }
  return NODE_ACCESS_IGNORE;
}

Your Answer

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

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