As a module developer I would like to be able to hook into the content_access modules logic for setting the default access grants for a node and set my own so that I can create different defaults for content of the same type.

For example, I have a need to have hierarchical inheritance of grants such that a node, be default, inherits the grants of another node (referenced in my case via a node reference field). The hook code in my own module would look like this:

/**
 * Implements hook_content_access_default_grant_alter().
 * If no per node grants exist for a seminar, load the grants 
 * for the parent event if it is set.
 */
function mymodule_content_access_default_grant_alter($node, &$grants) {
  if ($node->type == 'seminar' && empty($grants)) {
    if (isset($node->field_event_reference) && is_array($node->field_event_reference)) {
      $event = node_load($node->field_event_reference[0]['nid']);
      if (is_object($event)) {
        $grants = content_access_get_per_node_settings($event);
      }
    }
  }
}

Comments

johnennew’s picture

The attached patch demonstrates a way of adding this hook to content_access_per_node_settings function.

johnennew’s picture

Status: Active » Needs review

switched to needs_review

johnennew’s picture

Just remembered that there is a more elegant way to invoke an alter hook with drupal alter. Here is a new patch suggestion for comment and review.

The code in my own module would now look like this:

/**
 * Implements hook_content_access_default_grant_alter().
 * If no per node grants exist for a seminar, load the grants 
 * for the parent event if it is set.
 */
function mymodule_content_access_default_grant_alter(&$grants, $node) {
  if ($node->type == 'seminar' && empty($grants)) {
    if (isset($node->field_event_reference) && is_array($node->field_event_reference)) {
      $event = node_load($node->field_event_reference[0]['nid']);
      if (is_object($event)) {
        $grants = content_access_get_per_node_settings($event);
      }
    }
  }
}
johnennew’s picture

Slight modification to the patch to remove the deprecated pass by reference that doen't need to be there.

2ndmile’s picture

Thanks for the patch. #4 works well. I needed to be able to change view permissions for a role based on a check box on the node form.

2ndmile’s picture

Issue summary: View changes

changed my code reference

gisle’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)