Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

toc_api

3,771 sites Security covered
View on drupal.org

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
2 months ago
Release cadence
46 days
Stability
56% stable

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