How to Use the New Media Features in Drupal 8.4

| Drupal
How to Use the New Media Features in Drupal 8.4

Drupal 8.4 is out now, and it contains new media handling features.

For many years, Drupal has shipped with almost no media handling. This was the most commonly requested feature whenever we did Drupal training.

If you missed it, here's our overview of everything new in Drupal 8.4. In this tutorial, we'll walk you through how to use Drupal's new media options. 

Step #1. Install the Modules

The core of Drupal 8.4's media features is a new "Media" module. This module is available, but hidden by default.

To active the Media module you need to install and activate a contrib module for the media type you want to use. At the moment, there are ten modules that are ready:

  1. Document
  2. Image
  3. Audio
  4. Slideshow
  5. Video embed field
  6. Twitter
  7. Instagram
  8. SlideShare
  9. Facebook
  10. Inline entity form

If you want a complete, updated list of Media modules that are ready for 8.4, bookmark this page.

On the Drupal.org page, make sure you're downloading the latest version, which may well be marked as Alpha or Beta. This next image shows the current Twitter download page. I choose 8.x-2.0-alpha1.

drupal twitter media

After installing and enabling your contrib module, Drupal will ask you to enable the Media module too. The Media module is hidden by default and will only become available when enabling contrib modules.

Step #2. Create the Media type

Now, we're going to create the entity that will store our tweets.

  • Go to Structure > Media types.
  • Click "Add media type".

twitter media type

One of the more advanced features of the Media module is field mapping. If you want to, you can extract multiple types of data from the tweet and store them in different fields.

field mapping

  • Finish saving your new media type.
  • Click "Manage display" for "Twitter".

manage display

  • Change the display of "Tweet Url" to "Twitter embed".

twitter embed manage display

Step #3. Create the Fields

Next, we're going to add our new media type to a content type.

  • Go to Structure > Content types.
  • Create a new field for a content type, choosing the "Media" field type:

add drupal media field

  • Click through and choose "Twitter" in the "Bundles" area.
  • Don't check "Create referenced entities if they don't already exist" because that led to errors in my testing. 

media bundles

  • After saving your field, click "Manage display".
  • Change the display for your new field to "Rendered entity":

manage display twitter

Step #4. Add the Media

At this stage of Drupal 8's media journey, we will need to create the media items before we use them.

  • Go to Content > Media.
  • Click "Add media".
  • Choose "Twitter".

drupal84tweet

  • Save the tweet and the next image shows what you will see.
  • If you don't like the display, go back to Structure > Media types > Manage display and tweak the options.

drupal84 tweet

#5. Use the Media

Finally, let use our new created media inside a node:

  • Go to Content > Add content.
  • Create new content. 
  • You'll be able to search for your existing tweet using an autocomplete field:

tweet publishing drupal

  • Save your content type, and there's the embedded tweet:

tweet embedded drupal

Thoughts on the state of media

I'm delighted that real media handling is now available in Drupal. There are some caveats:

Caveat #1: No matter what contrib module I tried, I couldn't create media while creating a content type. I suspect that will be fixed in a future release. Right now, it's a bit of a pain to create the media in advance. I suspect this might be one of the reasons that the core "Image" and "File" fields haven't been moved over to the Media module yet. The Media module does support image and file uploads.

Caveat #2: There's no browsable media library yet. I suspect that may arrive early next year with Drupal 8.5.

Caveat #3: The current approach of hiding the Media module is a little strange. People will probably need to read a blog like this one to understand how the system works.

Despite those concerns, Drupal 8.4's approach to Media is very well done. No, it's not as easy as the oEmbed options that you'll find in WordPress and other platforms. But, this approach does offer a high degree of flexibility in how you store and display media. 

Would I build a Drupal site now with these new media modules? Yes, this is the future of Drupal. These media features work, and using them now will be easier than migrating later.


About the author

Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.