Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

TOC API

4,273 sites Security covered
View on drupal.org

This module provides a framework for generating tables of contents from HTML header tags within content. It offers customizable options for formatting, display, and responsiveness, allowing developers to integrate tables of contents into their Drupal sites.

This module provides a framework for creating table of contents (TOC) from an HTML fragment's header tags.

Classes

The TOC API consists of a several classes, services, and plugin:

  • Toc: A class that parses the header tags from an HTML fragment and stores a index of these headers that represent a hierarchical table of contents.

  • TocType: A configuration entity that contains options for customizing a table of contents.

  • TocFormatter: A service for formatting a table of content's headers, numbering, and ids.

  • TocManager: A service that creates and manages table of contents instances.

  • TocBuilder: A service that builds and renders a table of contents and update an HTML document's headers.

  • TocBlockBase: A base block which displays the current TOC module's TOC in a block.

The TOC API includes templates for formatting a table of contents as a hierarchical tree or jump menu. All TOC types included in the TOC API are responsive with a hierarchical tree being displayed for desktop users and a jump menu displayed for mobile users (browser width less than 768px).

Features

  • Only display TOC if the content contains a specified amount of HTML headers.
  • Define min and max headers to be included in table of contents
  • Set list style type and formatting for each header
  • Set back to top appearance and labelling for each header
  • Display entire path for hierarchical headers
  • Limit allowed HTML tags with table of contents headers and links
  • Provides the ability to move a TOC into a block.

Contrib Modules implementing or extending the TOC API module

Implementations

Note that this module does NOT directly expose any mechanisms for displaying a table of contents, you must create a custom module and/or install one of the below modules to implement a table of contents.

See README.md for toc_api

Custom Module Implementation

Below is very simple example of a hook from the TOC API example module (tocapiexample.module) that adds a TOC to all page and article nodes on a website.

/**
 * @file
 * Example of a custom implementation of the TOC API that adds a table of contents to specified content types.
 */

use Drupal\node\NodeInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\toc_api\Entity\TocType;

/**
 * Implements hook_node_view().
 */
function toc_api_example_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  // Add TOC to 'page' and 'article' content types that are being viewed as a full (page) with a body field.
  if (in_array($node->getType(), ['page', 'article']) && $view_mode == 'full' && isset($build['body'][0])) {
    // Get the completely render (and filtered) body value.
    $body = (string) \Drupal::service('renderer')->render($build['body'][0]);

    // Get 'default' TOC type options.
    /** @var Drupal\toc_api\TocTypeInterface $toc_type */
    $toc_type = TocType::load('default');
    $options = ($toc_type) ? $toc_type->options : [];

    // Create a TOC instance using the TOC manager.
    /** @var \Drupal\toc_api\TocManagerInterface $toc_manager */
    $toc_manager = \Drupal::service('toc_api.manager');
    /** @var \Drupal\toc_api\TocInterface $toc */
    $toc = $toc_manager->create('toc_filter', $body, $options);

    // If the TOC is visible (ie has more than X headers), replace the body
    // render array with the TOC and update body content using the TOC builder.
    if ($toc->isVisible()) {
      /** @var \Drupal\toc_api\TocBuilderInterface $toc_builder */
      $toc_builder = \Drupal::service('toc_api.builder');
      $build['body'][0] = [
        'toc' => $toc_builder->buildToc($toc),
        'content' => $toc_builder->buildContent($toc),
      ];
    }
  }
}

Notes

  • The TOC API is based on Drupal 11's OO architecture and patterns while the supported feature set is based on the Table of contents module.

  • Only TOC parsing and TOC building are included in the TOC API. JavaScript enhancements for hide/show logic and smooth scrolling are not being included and should be handled via custom code and/or a contrib module.

  • Even though, the TOC API's module's namespace is 'toc_api', all classes, services, and templates are using the 'toc' or 'Toc' namespace, because there is minimal risk of namespace conflicts with the TOC module because the TOC module does not implement any APIs. The TOC module just generates a table of contents for specific page content using JavaScript.

References

Author/Maintainer

Activity

Total releases
9
First release
Feb 2025
Latest release
5 months ago
Releases (12 mo)
3 ▼ from 6
Maintenance
Active

Release Timeline

Releases

Version Type Release date
2.0.2 Stable Feb 4, 2026
2.0.2-rc1 Pre-release Feb 4, 2026
2.0.1 Stable Oct 16, 2025
2.0.1-rc1 Pre-release Jul 16, 2025
2.0.0 Stable Apr 3, 2025
2.0.0-beta1 Pre-release Apr 3, 2025
8.x-1.7 Stable Feb 6, 2025
8.x-1.6 Stable Feb 4, 2025
2.0.x-dev Dev Feb 4, 2025