You are here

public function Toc::getTree in TOC API 8

Returns a hierarchical array of headers.

Return value

array An hierarchical array of headers.

Overrides TocInterface::getTree

File

src/Toc.php, line 394

Class

Toc
Defines A class that parses the header tags from an HTML document.

Namespace

Drupal\toc_api

Code

public function getTree() {
  if (!isset($this->tree)) {
    $this->tree = [
      'title' => $this->options['title'],
    ];

    // Collect all the header that do not have parents.
    $children = [];
    foreach ($this->index as $key => $item) {
      if (empty($item['parent'])) {
        $children[] = $key;
      }
    }
    $this
      ->buildTree($this->tree, $children);
  }
  return $this->tree;
}