You are here

function pmpermission_get_parent_nid_of_node in Drupal PM (Project Management) 7.2

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.

1 call to pmpermission_get_parent_nid_of_node()
_pmpermission_get_organization_id in pmpermission/pmpermission.module
Finds the nid of the organization attached to a node.

File

pmpermission/pmpermission.module, line 594
Main module file for the pmpermission module.

Code

function pmpermission_get_parent_nid_of_node($node, $parent_type, $max_depth = 10) {
  if ($node->type == $parent_type) {
    return $node->nid;
  }
  if ($max_depth) {
    --$max_depth;
    $bundle_name = $node->type;
    $field_name = _pmpermission_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 pmpermission_get_parent_nid_of_node($n, $parent_type, $max_depth);
      }
    } catch (Exception $e) {
      _pmpermission_watchdog_log($e);
    }
  }
  else {
    return FALSE;
  }
}