You are here

function pm_get_parent in Drupal PM (Project Management) 7.2

Same name and namespace in other branches
  1. 8 pm.module \pm_get_parent()
  2. 7.3 pm.module \pm_get_parent()

Helper function to return nid of particular bundle in hierarchy.

Parameters

object $node: The node whose parent's should be hunted for.

string $type: The parent node type that is being hunted for.

int $iteration: Number of maximum iterations to try for.

Return value

int Node id of the required parent.

File

./pm.module, line 772
Main module file for the Project Management module.

Code

function pm_get_parent($node, $type = 'pmorganization', $iteration = 10) {
  try {
    $wrapper = entity_metadata_wrapper('node', $node);
    while ($iteration) {
      $iteration--;
      $bundle = $wrapper
        ->getBundle();
      if ($bundle == $type) {
        return $wrapper
          ->getIdentifier();
      }
      if ($wrapper
        ->__isset($bundle . '_parent')) {
        $parent_field = $bundle . '_parent';
      }
      elseif ($wrapper
        ->__isset('field_' . $bundle . '_parent')) {
        $parent_field = 'field_' . $bundle . '_parent';
      }
      else {
        return FALSE;
      }
      $wrapper = $wrapper->{$parent_field};
    }
  } catch (EntityMetadataWrapperException $exc) {
    watchdog('pm', 'See ' . __FUNCTION__ . '() <pre>' . $exc
      ->getTraceAsString() . '</pre>', NULL, WATCHDOG_ERROR);
  }
}