You are here

function pm_permission_get_parent_nid_of_node in Drupal PM (Project Management) 8

Same name and namespace in other branches
  1. 7.3 includes/pm.permission.inc \pm_permission_get_parent_nid_of_node()

Recursively hunts and find out the first parent node of given type.

Parameters

object $node: The node for which the parent to be found.

string $parent_type: The content type of parent node.

int $max_depth: How many levels up should the function traverse.

Return value

mixed nid of the parent node if found, FALSE otherwise.

3 calls to pm_permission_get_parent_nid_of_node()
pmorganization_build_node_index in pmorganization/pmorganization.module
Builds and inserts pmorganization index entries for a given node.
pmproject_build_node_index in pmproject/pmproject.module
Builds and inserts pmproject index entries for a given node.
_pm_permission_get_organization_id in includes/pm.permission.inc
Finds the nid of the organization attached to a node.

File

includes/pm.permission.inc, line 511
Main module file for the pm_permission module.

Code

function pm_permission_get_parent_nid_of_node($node, $parent_type, $max_depth = NULL) {
  if ($max_depth == NULL) {
    $max_depth = variable_get('pm_permission_relation_search_depth', 10);
  }
  if ($node->type == $parent_type) {
    return $node->nid;
  }
  if ($max_depth) {
    --$max_depth;
    $bundle_name = $node->type;
    $field_name = _pm_permission_get_field_name($bundle_name, 'parent');
    if (empty($field_name)) {
      return FALSE;
    }
    try {
      $wrapper = entity_metadata_wrapper('node', $node);
      $n = $wrapper->{$field_name}
        ->value();
      if (empty($n)) {
        return FALSE;
      }
      elseif ($n->type == $parent_type) {
        return $n->nid;
      }
      else {
        return pm_permission_get_parent_nid_of_node($n, $parent_type, $max_depth);
      }
    } catch (Exception $e) {
      _pm_permission_watchdog_log($e);
    }
  }
  else {
    return FALSE;
  }
}