You are here

public function HierarchyOutlineStorage::hierarchyGetParents in Entity Reference Hierarchy 8

Get all the parents for the given node.

File

src/HierarchyOutlineStorage.php, line 39
Definition of Drupal\entity_hierarchy\HierarchyOutlineStorage.

Class

HierarchyOutlineStorage
Defines a storage class for hierarchies outline.

Namespace

Drupal\entity_hierarchy

Code

public function hierarchyGetParents($node, $limit = NULL) {
  $cnid = $node;

  // If a node object was passed, then the parents may already have been loaded.
  if (is_object($node)) {
    if (isset($node->entity_hierarchy_parents)) {
      return $node->entity_hierarchy_parents;
    }
    $cnid = $node->nid;
  }
  $out = array();
  $query = db_select('entity_hierarchy', 'nh')
    ->fields('nh')
    ->where('cnid = :cnid', array(
    ':cnid' => $cnid,
  ))
    ->orderBy('pweight', 'ASC');
  if ($limit) {
    $query
      ->range(0, $limit);
  }
  $result = $query
    ->execute()
    ->fetchAll();
  foreach ($result as $item) {
    $out[] = $item;
  }
  return $out;
}