Media Gallery
A simple yet powerful module that allows you to create and manage galleries of media items on your Drupal site, with PhotoSwipe lightbox integration.
media_gallery
Install
composer require 'drupal/media_gallery:^3.2'
Overview
The Media Gallery module provides a custom content entity type for creating and managing media galleries on your Drupal site. It leverages Drupal's core Media and Media Library modules to provide a user-friendly interface for organizing media items into galleries.
The module features tight integration with the PhotoSwipe library for an elegant lightbox experience when viewing gallery images. Galleries can be displayed using various layout options including grid, horizontal, vertical, featured image grid, and even a Swiper carousel (with the Swiper module installed).
Media Gallery supports pagination, reverse ordering, translatable content, and provides comprehensive permission controls for managing who can create, edit, view, and delete galleries. It also includes blocks for displaying the latest gallery items or specific galleries anywhere on your site, with configurable layouts and "View All" link options.
Features
- Custom Media Gallery entity type with title, description, and unlimited media item references
- PhotoSwipe lightbox integration for elegant image viewing experience
- Multiple layout options for gallery blocks: Grid, Horizontal, Vertical, Featured Image Grid, and Swiper (carousel)
- Configurable pagination with items per page settings and reverse ordering option
- Two block plugins: one for displaying latest items from all galleries, another for displaying a specific gallery
- Pathauto integration for automatic URL alias generation
- Full translatable support for multilingual sites
- Layout Builder compatible
- Built-in Views page at /galleries showing all published galleries
- Extensible layout plugin system allowing custom layouts to be added by other modules
- Support for non-image media types (videos, oEmbed content) within galleries
Use Cases
Photo Portfolio Gallery
Create a photographer's portfolio by organizing images into themed galleries (e.g., 'Weddings', 'Portraits', 'Landscapes'). Use the Featured Image Grid layout to highlight the best photo from each gallery, and enable pagination for galleries with many images.
Product Image Gallery
For e-commerce sites, create product galleries showing items from different angles. Use the Swiper layout for a carousel experience on product pages, allowing customers to swipe through product images.
Homepage Gallery Showcase
Use the 'Latest gallery items from all galleries' block to showcase recent additions across all galleries on the homepage. Configure the 'View All' link to direct visitors to the full /galleries page.
Event Photo Albums
Create galleries for events (conferences, parties, etc.) where users can browse photos with PhotoSwipe lightbox. Enable reverse ordering to show the newest photos first.
Mixed Media Gallery
Create galleries that include videos alongside images. The module supports oEmbed media (YouTube, Vimeo) and video files, rendering them appropriately within the gallery layout.
Tips
- Create custom layouts by extending MediaGalleryLayoutBase and adding the #[MediaGalleryLayout] attribute
- Use the grid layout with configurable columns for responsive galleries that adapt to screen size
- The Featured Image Grid layout is perfect for portfolio-style galleries where you want to highlight one item
- Configure different image styles for thumbnails and the PhotoSwipe modal to balance performance and quality
- Use the 'View All' link feature in blocks to drive traffic to your gallery listing page
- For very large galleries, enable pagination to improve page load performance
- Gallery entities are translatable - create multilingual galleries by enabling translation on the entity type
Technical Details
Admin Pages 3
/admin/structure/media-gallery
Configuration page for the Media Gallery entity type. Provides access to manage fields, form display, and view display settings for galleries.
/admin/content/media-gallery
List page showing all media galleries. Provides an overview with ID, title, status, author, created and updated dates. Allows administrators to add, edit, and delete galleries.
/admin/content/media-gallery/add
Form for creating a new media gallery with title, description, media items, pagination settings, and publishing options.
Permissions 9
Hooks 1
hook_media_gallery_layout_info_alter
Allows modules to alter media gallery layout plugin definitions
Troubleshooting 4
Ensure the PhotoSwipe module is properly installed and enabled. Check that the PhotoSwipe library files are correctly downloaded. Verify that the images field is using the 'photoswipe_field_formatter' formatter.
The Swiper layout requires the Swiper module (swiper_formatter) to be installed. Install it via Composer: composer require drupal/swiper_formatter, then enable it and create a Swiper configuration entity.
Verify that the galleries contain published media items. Check that the viewing user has the 'view published media gallery entities' permission. For the specific gallery block, ensure a gallery is selected in the block configuration.
Enable the appropriate migration submodule based on your D7 Media module version (media_gallery_migration for 7.x-1.x or media_gallery_migration2 for 7.x-2.x). Ensure all required migration modules (migrate_plus, migrate_tools) are installed.
Security Notes 3
- The 'Administer media gallery settings' permission is marked as restricted access and should only be granted to trusted administrators
- Media access is checked when rendering gallery items - unpublished media will not appear even if included in a gallery
- Gallery visibility respects the 'view published/unpublished media gallery entities' permissions