Render context error during story generation with custom Twig functions requiring Drupal render context

Created on 11 June 2025, 17 days ago

Problem/Motivation

Story generation fails with "Render context is empty" error when components use custom Twig functions that require Drupal render context (e.g., bem(), attach_library(), drupal_settings()).

The issue occurs in the TwigStorybook library where $wrapper->render(['_story' => false]) is called during story generation without proper Drupal render context. This affects any Drupal site using component-based theming with custom Twig functions in a Drupal 11 + Storybook 9.x setup with server-side rendering.

Steps to reproduce

1. Create a component that uses a custom Twig function requiring render context
2. Create a .stories.twig file for the component
3. Run story generation: drush storybook:generate-all-stories --force

Expected result: Stories should generate successfully without errors.

Actual result: Story generation fails with error: "Render context is empty, because render() was called outside of a renderRoot() or renderInIsolation() call. Use renderInIsolation()/renderRoot() or #lazy_builder/#pre_render instead."

Proposed resolution

Wrap the TwigStorybook render calls in renderInIsolation() within the Drupal Storybook module integration.

Example fix in src/Drush/Commands/StorybookCommands.php:
- Inject RendererInterface service into StorybookCommands class
- Wrap generateStoriesJsonFile() calls in renderInIsolation()
- Update service definitions in storybook.services.yml

Remaining tasks

- Inject RendererInterface service into StorybookCommands class
- Wrap generateStoriesJsonFile() calls in renderInIsolation()
- Update service definitions in storybook.services.yml
- Test with components using render-context-dependent Twig functions
- Verify story generation completes without errors

User interface changes

None.

API changes

None. This is an internal fix that maintains existing API compatibility.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.0

Component

Storybook

Created by

🇪🇪Estonia tormi Tallinn

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024