You are here

function hook_workbench_access_tree in Workbench Access 7

Defines an access array for Workbench Access.

For Workbench Access to find the position in the access hierarchy, we have to have an easily traversable array. Unlike other _tree() functions in Drupal, the primary goal here is to attach the parent ids to the childen. Doing so allows the parents and children to be checked efficiently.

The structure of your array should look like the following example:

array ( 'workbench_access' => array ( 'access_id' => 'workbench_access', 'access_type_id' => 'workbench_access', 'name' => 'Workbench Access', 'description' => 'Access control for editorial content.', 'weight' => 0, 'depth' => 0, 'parent' => 0, ), 1 => array ( 'access_id' => '1', 'access_type_id' => 'workbench_access', 'name' => 'Museum', 'description' => 'Test term for Workbench Access.', 'weight' => '-5', 'depth' => 1, 'parent' => 'workbench_access', ), 2 => array ( 'access_id' => '2', 'access_type_id' => 'workbench_access', 'name' => 'Museum-Guests', 'description' => 'Test child term for Workbench Access.', 'weight' => '0', 'depth' => 2, 'parent' => '1', ), 3 => array ( 'access_id' => '3', 'access_type_id' => 'workbench_access', 'name' => 'Museum-Staff', 'description' => 'Test child term for Workbench Access.', 'weight' => '0', 'depth' => 2, 'parent' => '1', ), );

A few things to note:

Be sure to start at the top of the requested $info tree.

The access_ids are the array keys. These are stored in the database as VARCHARS, which allows us to mix machine names (strings) and serial ids (integers) in our storage. This process allows us to use entire vocabularies as access control sections, with the terms in the vocabulary as the children.

There is a one-to-one ration of children to parent. That is, a child can only have one parent item.

Depth must start at 0, even for storage trees that normally start at 1 (like menus). This lets us maintain a consistent API.

This function is called in two contexts. The first builds a generic tree structure used for access definitions. The second is when checking a user's access rules. In the user case, we only care about the active access keys that have been requested.

Parameters

$info: An array defining the access scheme, as defined in hook_workbench_access_info().

$keys: Boolean value to return only array keys, or all data.

Return value

An array of access_ids or a data array as defined above.

See also

workbench_access_get_active_tree()

workbench_access_get_access_tree()

2 functions implement hook_workbench_access_tree()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

menu_workbench_access_tree in modules/menu.workbench_access.inc
Implements hook_workbench_access_tree().
taxonomy_workbench_access_tree in modules/taxonomy.workbench_access.inc
Implements hook_workbench_access_tree().

File

./workbench_access.api.php, line 206
API documentation file for Workbench Access.

Code

function hook_workbench_access_tree($info, $keys) {
  $tree = array();
  $items = array();
  if (isset($info['access_id'])) {
    if ($info['access_type_id'] != $info['access_id']) {
      $items[$info['access_type_id']] = $info['access_id'];
    }
    else {
      $items[$info['access_type_id']] = 0;
    }
  }
  else {
    foreach (array_filter($info['access_type_id']) as $vid) {
      $items[$vid] = 0;
    }
  }
  foreach ($items as $vid => $tid) {
    $vocabulary = taxonomy_vocabulary_machine_name_load($vid);
    $tree[$vocabulary->machine_name] = array(
      'access_id' => $vocabulary->machine_name,
      'access_type_id' => $vocabulary->machine_name,
      'name' => $vocabulary->name,
      'description' => $vocabulary->description,
      'weight' => 0,
      'depth' => 0,
      'parent' => 0,
    );
    $children = taxonomy_get_tree($vocabulary->vid, $tid);
    foreach ($children as $child) {
      $tree[$child->tid] = array(
        'access_id' => $child->tid,
        'access_type_id' => $vocabulary->machine_name,
        'name' => $child->name,
        'description' => $child->description,
        'weight' => $child->weight,
        'depth' => $child->depth + 1,
        'parent' => !empty($child->parents[0]) ? $child->parents[0] : $vocabulary->machine_name,
      );
    }
  }
  if ($keys) {
    return array_keys($tree);
  }
  return $tree;
}