You are here

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

Same name in this branch
  1. 8.2 src/SchemaMetatagClient.php \Drupal\schema_metatag\SchemaMetatagClient::getObjectTree()
  2. 8.2 tests/modules/schema_metatag_test/src/SchemaMetatagClient.php \Drupal\schema_metatag_test\SchemaMetatagClient::getObjectTree()

Reorganize the classes into a hierarchical tree.

The raw data doesn't show the whole hierarchy, just the immediate parents. The tree allows us to identify which objects are the topmost level and sort out other relationships between them, or pull out a collection of objects that are children of a specific parent.

Parameters

bool $clear: Whether to clear the cached array created by getObjectTree().

bool $clear_objects: Whether to clear the cached array created by getObjects().

Return value

array A hierarchical array of the object names.

Overrides SchemaMetatagClientInterface::getObjectTree

1 call to SchemaMetatagClient::getObjectTree()
SchemaMetatagClient::getTree in src/SchemaMetatagClient.php
Get some or all of the object tree.
1 method overrides SchemaMetatagClient::getObjectTree()
SchemaMetatagClient::getObjectTree in tests/modules/schema_metatag_test/src/SchemaMetatagClient.php
Reorganize the classes into a hierarchical tree.

File

src/SchemaMetatagClient.php, line 184

Class

SchemaMetatagClient
Class SchemaMetatagClient.

Namespace

Drupal\schema_metatag

Code

public function getObjectTree($clear = FALSE, $clear_objects = FALSE) {
  $cid = "schema_metatag.tree";
  if (!$clear && !$clear_objects && ($cache = $this->cacheBackend
    ->get($cid))) {
    $tree = $cache->data;
  }
  else {
    $objects = $this
      ->objectInfo($clear_objects);
    $tree = [];
    $flat = [];
    foreach ($objects as $child => $item) {
      if (empty($item['parents'])) {
        $tree[$child] =& $flat[$child];
      }
      else {
        foreach ($item['parents'] as $parent) {
          if (!isset($flat[$child])) {
            $flat[$child] = [];
          }
          $flat[$parent][$child] =& $flat[$child];
        }
      }
    }

    // Sort the result, keeping the nested structure.
    $this
      ->sortAssocArray($tree);

    // Cache permanently.
    $this->cacheBackend
      ->set($cid, $tree, CacheBackendInterface::CACHE_PERMANENT);
  }
  return $tree;
}