Context lets you define contexts: named rules that say when something should happen and what should happen then. Each context has conditions, such as the path, content type, user role, language or another active context. When the conditions pass, its reactions run: placing blocks, switching the theme, adding body classes, setting the page title, and more.
You can think of each context as a "section" of your site. Examples are "Blog pages", "Members area" or "Holiday campaign". Everything that changes in that section is managed in one place, instead of in the visibility settings of many separate blocks.
Why use Context?
- Manage layouts by section, not block by block. One context can place a whole set of blocks across several regions. Changing the section's rules updates all of them at once.
- More than blocks. The same conditions can also switch the theme, set the active menu item, hide regions, add CSS classes or suggest page templates.
- Flexible logic. Conditions can require that all of them pass or that any one passes. Contexts can depend on other contexts, using wildcards and negation. A context with no conditions applies to the whole site.
- Uses core's plugin system. Context uses Drupal core's Condition plugins, so conditions from core and from other modules work with it. Developers can add their own reaction plugins.
- Configuration you can deploy. Contexts are config entities, so you export, review and deploy them like the rest of your site configuration.
- Works with caching. Blocks placed by Context are rendered with lazy builders, the same way core renders blocks. They work with Dynamic Page Cache and BigPipe placeholdering.
Reactions
- Blocks: place blocks in any region of any theme, either together with the blocks from core's Block layout or instead of them.
- Theme: switch to another theme, the admin theme or the default theme.
- Menu: set the active menu item for pages that have no menu item of their own, for example to highlight "News" on every article.
- Regions: disable regions of a theme.
- Body class: add CSS classes to the
<body>element. - Page title: override the page title.
- Page template suggestions: add template suggestions for the page.
Conditions
All of core's conditions are available, such as request path, content type, user role, language and current theme, along with any condition plugins from contributed modules. Context adds:
- Context (any) and Context (all): activate when other contexts are active, with
*wildcards and~negation. - Request path exclusion: match every page except the paths you list.
- Request domain: match on the domain of the request.
- HTTP status code: target 403 and 404 pages, for example.
- User profile pages: match user pages by the profile owner's status.
- View inclusion: match pages that are displays of specific views.
Submodules
- Context UI provides the admin interface at Structure > Context, including a condition and reaction library and a "Context inspector" block that shows which contexts are active on the current page. Contexts still work without it, so you can uninstall it on production sites.
Requirements and compatibility
- Requires only Drupal core. Supports Drupal 10.3, 11 and 12.
- The 5.x branch is under active development, and release candidates are available. If you are upgrading, read the release notes and
CHANGELOG.md, and run database updates after updating. - On front-end pages rendered by Canvas, Canvas builds the page, so the Blocks reaction has no effect there. Other reactions still work. See the README for details.
Getting started
- Install Context with Composer (
composer require drupal/context), then enable Context and Context UI. - Go to Administration > Structure > Context and select Add context.
- Add conditions to choose where the context applies. Leave the conditions empty to make it site-wide.
- Add reactions to choose what happens there, then save.
Similar and related projects
- Core Block layout: fine for placing a few blocks one at a time. Use Context when many blocks or other page changes share the same rules.
- Block Visibility Groups: groups core block placements under shared conditions. It only manages block visibility.
- Menu Condition: adds a menu-position condition that you can use in contexts.
Support and contributing
Report bugs and request features in the issue queue. The maintainers are looking for co-maintainers and welcome help reviewing and testing issues, especially for the 5.x branch.
Project information
Seeking co-maintainer(s)
Maintainers are looking for help reviewing issues.- Project categories: Content display
51,098 sites report using this module
- Created by narres on , updated
Stable releases for this project are covered by the security advisory policy.
There are currently no supported stable releases.









