You are here

public function SchemaMetatagClient::getParents in Schema.org Metatag 8.2

Get an array of all parents of a given class.

Parameters

string $child_name: The key of the desired sub-array.

Return value

array An array of parent classes, ordered from the lowest to the highest in the tree hierarchy.

Overrides SchemaMetatagClientInterface::getParents

File

src/SchemaMetatagClient.php, line 317

Class

SchemaMetatagClient
Class SchemaMetatagClient.

Namespace

Drupal\schema_metatag

Code

public function getParents($child_name) {
  $parents = [];
  $tree = $this
    ->getTree();
  $iterator = new \RecursiveArrayIterator($tree);
  $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
  $depth = 0;
  $prev_depth = 0;
  $prev_key = '';
  foreach ($iterator as $key => $value) {
    $depth = $iterator
      ->getDepth();
    if ($depth > $prev_depth) {
      $parents[] = $prev_key;
    }
    elseif ($depth < $prev_depth) {
      for ($i = $depth; $i < $prev_depth; $i++) {
        array_pop($parents);
      }
    }
    if ($key == $child_name) {
      return $parents;
    }
    $prev_depth = $depth;
    $prev_key = $key;
  }
}