Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Twig State Access

Security covered Under active development 3 sites

Part of the Twig ecosystem · 43 projects

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.

Drupal 9–11

Ask your assistant about Twig State Access

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

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
3
Tracked since
Feb 2024
Latest release
11 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Release Timeline

Releases

Version Type Core PHP Release date
1.0.1 Stable 9–11 Oct 26, 2025
1.0.x-dev Dev 9–11 Feb 3, 2024
1.0.0 Stable 9–10 Feb 3, 2024