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

Twig State Access

5 sites Security covered Drupal 9–11 Twig ecosystem
View on drupal.org

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.

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
1
Tracked since
Oct 2025
Latest release
10 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Slowing

Releases

Version Type Core Release date
1.0.1 Stable 9–11 Oct 26, 2025