Drupal Media Management: Tips, Tricks, and Secure Attachments

Secure file storage

Drupal Media Management: Tips, Tricks, and Secure Attachments

Handling Media Files in Drupal 10 and 11

Drupal has long been a powerful content management system (CMS) with robust media handling capabilities. With the release of Drupal 10 and the upcoming Drupal 11, managing media files has become even more efficient and user-friendly. In this post, we’ll explore how to handle media files in these versions, covering best practices, new features, and practical use cases.

Media Handling in Drupal: An Overview

Drupal provides a built-in Media module that allows users to manage various types of media, including images, videos, audio files, and documents. The module improves reusability, making it easier to handle assets across different content types.

Key Features of Drupal’s Media System

  • Media Library: A central place to manage and reuse media files.
  • Media Types: Support for images, videos (YouTube, Vimeo), audio, remote files, and more.
  • WYSIWYG Integration: Seamless embedding of media into content using the CKEditor.
  • Entity-Based Media Handling: Media files are stored as entities, allowing better control and flexibility.
  • Access Control: Permissions to regulate who can upload, edit, or delete media.

What’s New in Drupal 10 and 11?

Drupal 10 introduced several improvements in media handling, and Drupal 11 is expected to refine them further:

1. Improved CKEditor 5 Integration

Drupal 10 replaces CKEditor 4 with CKEditor 5, which offers a better user experience, enhanced media embedding, and improved accessibility.

2. Enhanced Media Library

  • Faster search and filtering options.
  • More intuitive UI for managing media assets.
  • Drag-and-drop functionality for uploading and organizing files.

3. Better Media Permissions

New permission settings allow site administrators to define more granular control over who can manage media assets.

4. Modernized Frontend and Theming

Drupal 10 and 11 make it easier to display media with the default Olivero theme and offer better support for responsive media.

How to Use the Media Module in Drupal 10/11

1. Enabling the Media Module

If it’s not already enabled, you can activate it via the admin interface or using Drush:

 drush en media

2. Configuring Media Types

Navigate to Structure > Media Types to configure existing media types or create new ones. Define fields such as alt text, descriptions, or additional metadata.

3. Using the Media Library

  • Go to Content > Media to access the Media Library.
  • Upload new media files or reuse existing ones.
  • Use filters to find specific media assets quickly.

4. Embedding Media in Content

Drupal 10/11 allows easy embedding of media via the CKEditor:

  • Click on the Media button in the editor toolbar.
  • Select an existing media file or upload a new one.
  • Insert the media into your content.

5. Displaying Media in Views

To showcase media in lists, slideshows, or galleries:

  • Create a new View at Structure > Views.
  • Choose Media as the base entity.
  • Add filters and sorting options as needed.
  • Configure display settings for responsive layouts.

Processing Secure Attachments via Command Line

When migrating or managing secure attachments, you may need to create or cleanse media entities programmatically. This would take hours via the GUI. The following Drush commands can really help automate these processes efficiently.

1. Delete Media Entities in the Document Bundle if They Are Secure Attachments

To remove media entities from the “document” bundle if they reside in the “secure-attachments” folder:

drush php:eval '$entity_type_manager = \Drupal::service("entity_type.manager"); $media_storage = $entity_type_manager->getStorage("media"); $file_storage = $entity_type_manager->getStorage("file"); $media_items = $media_storage->loadByProperties(["bundle" => "document"]); foreach ($media_items as $media) { $file = $media->get("field_media_document")->entity; if ($file && str_contains($file->getFileUri(), "secure-attachments")) { $media->delete(); if ($file_storage->load($file->id())) { $file->delete(); } } }'

2. Create Media Entities from a Folder

To scan a directory for PDFs and create media entities in the “document_private” bundle:

drush php:eval '$file_system = \Drupal::service("file_system"); $entity_type_manager = \Drupal::service("entity_type.manager"); $files = $file_system->scanDirectory(".", "/\.pdf$/"); foreach ($files as $file) { $existing_files = $entity_type_manager->getStorage("file")->loadByProperties(["uri" => $file->uri]); $fileObj = $existing_files ? reset($existing_files) : null; if (!$fileObj) { $fileObj = \Drupal\file\Entity\File::create(["uri" => $file->uri, "status" => 1]); $fileObj->save(); } $existing_media = $entity_type_manager->getStorage("media")->loadByProperties(["bundle" => "document_private", "field_media_document.target_id" => $fileObj->id()]); if (!$existing_media) { $media = \Drupal\media\Entity\Media::create(["bundle" => "document_private", "name" => $file->filename, "field_media_document" => ["target_id" => $fileObj->id()]]); $media->save(); } }'

These commands help automate the cleansing and recreation of secure media entities efficiently, ensuring proper security and management of sensitive attachments.

Best Practices for Managing Media in Drupal 10/11

1. Organize Media Files Efficiently

  • Use meaningful file names and categorize media with taxonomy.
  • Enable media folders (using contributed modules like IMCE) for better organization.

2. Optimize Images and Videos

  • Use the Image Styles feature to generate different image sizes automatically.
  • Leverage lazy loading to improve performance.
  • Use external services (like YouTube/Vimeo) for hosting large videos instead of self-hosting.

3. Secure Media Access

  • Set appropriate permissions for different user roles.
  • Use Private File System for sensitive or restricted content.

4. Keep Media SEO-Friendly

  • Add relevant alt text and descriptions for accessibility.
  • Use descriptive filenames to enhance search engine visibility.

Conclusion

Drupal 10 and 11 bring significant improvements in media handling, making it easier for content creators to manage and reuse media assets effectively. By leveraging the built-in features and following best practices, you can enhance both user experience and site performance. Whether you’re managing a blog, a corporate website, or an e-commerce platform, mastering media handling in Drupal is key to delivering rich, engaging content.

Subscribe

Please enter your details below to be notified of product releases and announcements.