You are here

function template_preprocess_toc_menu in TOC API 8

Prepares variables for table of contents (select) menu.

Default template: toc-menu.html.twig.

Parameters

array $variables: An associative array containing the following keys:

  • toc: A TOC (table of contents) object.
  • attributes: Attributes to be added to back to top link.

File

./toc_api.module, line 136
Converts header tags into a hierarchical table of contents.

Code

function template_preprocess_toc_menu(&$variables) {

  /** @var \Drupal\toc_api\TocInterface $toc */
  $toc = $variables['toc'];
  $options = $toc
    ->getOptions();
  $variables['options'] = $options;
  $variables['attributes'] = new Attribute($variables['attributes']);
  $variables['title'] = $options['title'] ? $options['title'] : t('- Select -');
  $variables['index'] = $toc
    ->getIndex();
  foreach ($variables['index'] as &$item) {
    $header_options = $options['headers'][$item['tag']];
    if ($options['number_path']) {

      // Make sure there is always a suffix to delimit the path from the title.
      $suffix = $header_options['number_suffix'] ?: ' - ';
      $prefix = $header_options['number_prefix'];
      $item['prefix'] = $prefix . $item['path'] . $suffix;
    }
    else {

      // Prefix using double dash indentation.
      $item['prefix'] = $item['indent'] ? str_repeat('--', $item['indent']) . ' ' : '';
    }
  }
  $variables['#attached']['library'][] = 'toc_api/toc.menu';
}