Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Usage Milestone: Google Analytics Module google_analytics crossed 1,000 active installs. Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Release: GraphQL Compose Codegen 1.1.2 Minor update available for module graphql_compose_codegen (1.1.2). Release: Mapy.com 1.1.3 Minor update available for module mapycom (1.1.3). Release: Ckeditor5 entity browser 3.0.3 Minor update available for module ckeditor5_entity_browser (3.0.3). Release: Ckeditor5 entity browser 3.0.1 Minor update available for module ckeditor5_entity_browser (3.0.1). Release: Ckeditor5 entity browser 3.0.2 Minor update available for module ckeditor5_entity_browser (3.0.2). Release: Teamleader Integration 4.0.2 Minor update available for module teamleader (4.0.2). Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

TOC API

3,642 sites Security covered Drupal 10–11
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

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

3 tracked projects depend on this one

Activity

Tracked releases
9
Tracked since
Feb 2025
Latest release
6 months ago
Releases (12 mo)
3 ▼ from 6
Maintenance
Slowing

Release Timeline

Releases

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