View Mode Page
Creates additional URL paths for entities that render them using specific view modes.
view_mode_page
Install
composer require 'drupal/view_mode_page:^4.0'
Overview
View Mode Page allows site builders to create custom URL paths that display entities using different view modes. Instead of only showing content with the default view mode at the canonical URL, this module enables rendering entities with any configured view mode at custom URL patterns.
The module works by defining URL patterns that include a placeholder (%) for the entity's existing URL or alias. When visitors access these custom paths, the module intercepts the request, identifies the entity, and renders it using the specified view mode. This is particularly useful for creating summary pages, print versions, or any alternate representations of content.
The module supports all Drupal entity types that have canonical links, including nodes, taxonomy terms, users, and custom entities. Patterns can be filtered by entity bundle (content type) and language for multilingual sites.
Features
- Create custom URL patterns for displaying entities in different view modes
- Support for all entity types with canonical links (nodes, taxonomy terms, users, custom entities)
- URL pattern syntax using % as placeholder for entity path/alias (e.g., /%/summary, /%/print)
- Filter patterns by entity bundle (content type) to apply patterns only to specific bundles
- Filter patterns by language for multilingual site support
- Drag-and-drop pattern ordering to control pattern priority and matching order
- Selection conditions using bundle and language criteria with AND/OR logic
- Automatic path alias integration - works with both internal paths and URL aliases
- Inbound and outbound path processing for seamless URL handling
- AJAX-enabled pattern editing form that updates dynamically when entity type is selected
- Weight-based pattern ordering with automatic weight adjustment based on conditions
Use Cases
Article Summary Pages
Create teaser/summary pages for articles. Configure a pattern with path /%/summary, entity type Content, bundle Article, and view mode Teaser. When users visit /my-article/summary, they see the article rendered as a teaser instead of the full content.
Print-Friendly Versions
Provide print-optimized versions of content. Create a pattern /%/print with a custom 'Print' view mode that removes navigation, sidebars, and other non-essential elements. Users can access /any-content/print to get a printer-friendly version.
Mobile-Optimized Content
Serve mobile-specific layouts at dedicated URLs. Create patterns like /%/mobile with a view mode configured for mobile display, useful for scenarios where responsive design alone isn't sufficient.
Language-Specific Summary Pages
On multilingual sites, create summary pages only for specific languages. Configure patterns with language conditions to show teasers at /content/summary only for English content, while French content might have /content/resume.
Content Type Preview Pages
Allow editors to preview how content will appear in different contexts. Create patterns for each view mode used in listings, blocks, or widgets, letting editors visit /content/card-preview to see how it appears as a card.
API-like Endpoints for Content
Create lightweight JSON or minimal HTML endpoints. Configure a view mode with minimal markup and use patterns like /%/embed to serve embeddable versions of content for AJAX loading or iframe embedding.
Tips
- Pattern order matters - more specific patterns (with bundle/language conditions) should be ordered before generic patterns
- Use descriptive labels for patterns to easily identify them in the admin interface
- Test patterns with both URL aliases and internal paths to ensure proper functionality
- Consider SEO implications - search engines may index view mode page URLs as separate content
- Use the weight system to control pattern matching priority when multiple patterns could match
- Clear caches after creating or modifying patterns to ensure changes take effect immediately
Technical Details
Admin Pages 4
/admin/config/search/view-mode-page
Main configuration page listing all view mode patterns. Allows reordering patterns via drag-and-drop, editing, and deleting existing patterns. The list shows pattern labels, URL patterns, entity types, view modes, and selection conditions.
/admin/config/search/view-mode-page/add
Form for creating a new view mode pattern. Define a URL pattern, select the entity type, configure bundle and language filters, and choose the view mode for rendering.
/admin/config/search/view-mode-page/{view_mode_page_pattern}
Form for editing an existing view mode pattern. Same fields as the add form with current values pre-populated.
/admin/config/search/view-mode-page/{view_mode_page_pattern}/delete
Confirmation form for deleting a view mode pattern.
Permissions 1
Hooks 1
hook_block_build_alter
Implemented to add url.path cache context to the system_main_block to prevent incorrect caching when using path aliases with different view modes.
Troubleshooting 5
Ensure the pattern includes the % placeholder for the entity path. The % must represent the complete entity URL or alias. Clear all caches after creating or modifying patterns.
The module adds url.path cache context to system_main_block. If issues persist, check that your theme and custom blocks properly vary by URL path.
Patterns should work with both. Ensure the path alias system is working correctly and that aliases are properly configured for your entities.
Check that bundle and language conditions are properly configured. Use the pattern list page to verify conditions are displayed correctly. Ensure the selection logic (and/or) matches your requirements.
Only entity types with canonical link templates and path field support are shown. Custom entities must implement these features to be compatible.
Security Notes 3
- The 'administer view_mode_page' permission is restricted and should only be granted to trusted administrators
- View mode page URLs respect existing content access permissions - users must have 'access content' permission to view entities
- The module does not bypass entity access controls; it only changes how entities are rendered, not who can access them