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.

This module allows you to directly access Drupal's state and private temporary state data within your Twig templates. This eliminates the need for preprocess functions to pass this data to your templates.

Twig State Access module provides new twig extensions to access Drupal state/private tmp state APIs in readonly mode.

Features

Typically in Drupal to pass Drupal state values to twig templates you usually use preprocess hooks to retrieve the value via Drupal state service and then inject it in template variables array, this module gives you access to retrieve any state value directly from the twig file using new twig extensions.

Examples:

Before enabling Twig state access module:

my_module.module file :

   /**
     * Implements hook_preprocess_hook().
     */
    function my_module_preprocess_page(&$variables) {
      ...
      $toto = Drupal::state()->get('toto', 'Default toto value here');
      $all_bars = Drupal::state()->getMultiple(['bar1', 'bar2', 'bar3']);
      $foo = Drupal::service('tempstore.private')->get('my_module')->get('foo');
      $foo_metadata = Drupal::service('tempstore.private')->get('my_module')->getMetadata('foo');
      $variables['toto'] = $toto;
      $variables['all_bars'] = $all_bars;
      $variables['foo'] = $foo;
      $variables['foo_metadata'] = $foo_metadata;
      ...
    }

page.html.twig file :

     ...
    <div>
      <ul>
        <li>Toto: {{ toto }}</li>
        <li>All bars: {{ all_bars|join(', ') }}</li>
        <li>Foo: {{ foo }}</li>
        <li>Foo Metadata: {{ foo_metadata.updated }} - {{ foo_metadata.ownerId }}</li>
      </ul>
    </div>
    ...

After enabling twig state access module:

my_module.module file :

   /**
     * Implements hook_preprocess_hook().
     */
    function my_module_preprocess_page(&$variables) {
      ...
      // No more state API code here.
      ...
    }

page.html.twig file :

     ...
    <div>
      // First param is the key to retrieve, second optional param is the default value.
      {% set toto = drupal_state('toto', 'Default toto value here') %}
      // To retrieve multiple keys at once, return an array of related values.
      {% set all_bars = drupal_state(['bar1', 'bar2', 'bar3']) %}
      // First param is the collection, second param is the key to retrieve.
      {% set foo = drupal_private_state('my_module', 'foo') %}
      // When third param is set to true then key related metadata is retrieved.
      {% set foo_metadata = drupal_private_state('my_module', 'foo', true) %}
      <ul>
        <li>Toto: {{ toto }}</li>
        <li>All bars: {{ all_bars|join(', ') }}</li>
        <li>Foo: {{ foo }}</li>
        <li>Foo Metadata: {{ foo_metadata.updated }} - {{ foo_metadata.ownerId }}</li>
      </ul>
    </div>
    ...

Additional Requirements

This module requires no modules outside of Drupal core.

Activity

Total releases
1
First release
Oct 2025
Latest release
8 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Slowing

Releases

Version Type Release date
1.0.1 Stable Oct 26, 2025