You are here

function pm_get_parents in Drupal PM (Project Management) 7.3

Recursively gather all parents for a given entity.

1 call to pm_get_parents()
pm_field_formatter_view in ./pm.module
Implements hook_field_formatter_view().

File

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

Code

function pm_get_parents($entity_type, $entity, $field, $delta) {
  $iteration = 10;
  $field_name = $field['field_name'];
  $parents = array();
  try {
    $e = entity_metadata_wrapper('node', $entity);
    if ($field['cardinality'] == '1') {
      if (isset($e->{$field_name})) {
        $wrapper = $e->{$field_name};
      }
    }
    else {
      if (isset($e->{$field_name})) {
        $wrapper = $e->{$field_name}[$delta];
      }
    }
    while ($iteration and $wrapper) {
      $iteration--;
      $bundle = $wrapper
        ->getBundle();
      if ($wrapper
        ->getIdentifier()) {
        $parents[$wrapper
          ->getIdentifier()] = pm_get_parent_label('node', $wrapper);
      }
      if ($wrapper
        ->__isset($bundle . '_parent')) {
        $parent_field = $bundle . '_parent';
      }
      elseif ($wrapper
        ->__isset('field_' . $bundle . '_parent')) {
        $parent_field = 'field_' . $bundle . '_parent';
      }
      else {
        break;
      }
      if ($bundle == 'pmorganization') {
        break;
      }
      $wrapper = $wrapper->{$parent_field};
    }
  } catch (EntityMetadataWrapperException $exc) {
    watchdog('pm', 'See ' . __FUNCTION__ . '() <pre>' . $exc
      ->getTraceAsString() . '</pre>', NULL, WATCHDOG_ERROR);
  }
  return array_reverse($parents);
}