You are here

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

Create a @type option list from a given tree section.

Used to create a psuedo "nested" option list used for @type.

Parameters

string $parent_name: The key of the desired sub-array, if any.

int $depth: The desired depth to retrieve below the parent, -1 for the whole tree.

Return value

array An option array for the given parent.

Overrides SchemaMetatagClientInterface::getOptionList

File

src/SchemaMetatagClient.php, line 348

Class

SchemaMetatagClient
Class SchemaMetatagClient.

Namespace

Drupal\schema_metatag

Code

public function getOptionList($parent_name = NULL, $depth = -1) {
  $tree = $this
    ->getTree($parent_name, $depth);
  $separator = '-';
  $label_list = [];
  $previous_keys = [];
  $key_list = [];
  $iterator = new \RecursiveArrayIterator($tree);
  $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);

  // Some objects have multiple hierarchies, make sure we only provide
  // options for one of them to avoid problems when the keys are used in the
  // final option list. Basically we'll show the first one we encounter and
  // ignore it if it appears later.
  foreach ($iterator as $key => $item) {
    if (!in_array($key, $previous_keys)) {
      $depth = $iterator
        ->getDepth() + 1;
      $label_list[] = str_repeat($separator, $depth) . ' ' . $key;
      $key_list[] = $key;
      $previous_keys[] = $key;
    }
  }

  // We have to create two separate arrays for types and labels rather than
  // using the object name as the key because the numerical keys keep the
  // whole array in the correct order, maintaining the nested hierarchy.
  $option_list = array_combine($key_list, $label_list);
  return $option_list;
}