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).

twig_state_access

9 sites Security covered
View on drupal.org

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
5 months ago
Release cadence
Stability
100% stable

Releases

Version Type Release date
1.0.1 Stable Oct 26, 2025