By: Hashik
March 28 2018

What is Media Entity And Why is it Important in Drupal?

One of the best things about CMSs is they can lessen the human efforts by a huge margin but, even then adding any media is never easy. At least it wasn’t until Media handling modules came into action, let us discuss how it made things easy. To understand about Media Entity, we need to understand the Media modules as a whole and why they exist. Let us go into the days of Drupal 7 when there was no editor nor media management tool out-of-the-box. Hey, have no fear modules are here, today.

We used to have WYSIWYG editor and CKEditor modules, which are great. But even with these handling media meant to copy-paste the HTML. This is where the Media module came into action. A very popular module in Drupal 7, handling media is easy. This module provided decent functionality but it’s a bit buggy with CKEditor. Today, there are other great media handling modules like SCALD and IMCE to handle the situation.

Media Handling in Drupal 8

In Drupal 8 the media module is re-architected into smaller components. A lot of people, contributors of Media module, SCALD got together and rolled up their sleeves to build a proper media management system.

Simply put, now the media module in Drupal 8 is a collection of configurations. They breached Media module into 3 modules, which are:

  1. Media Entity: It is used to store asset’s, it doesn’t do much work it just offers a base entity. If you want to handle Image, Audio, YouTube video etc. You have to download the provider modules. If you failed to comprehend this, worry not we’ll go through this later in the article.

    List of media provider modules in Drupal
  2. Entity Embed: It is used to embed any type of entity content types, users, media entities etc. into the text area. The best part about it is, it is not limited only to the media entities. When you try to embed an entity, it adds a tag to HTML which looks something like this

    <drupal-entity data-embed-button =” media” …

    After the tags are added the module has a filter that searches for this tag and replaces them with a rendered entity.

  3. Entity Browser: It allows you to create a browser where you are allowed to browse through media assets, select them and embed them. We can use entity browser with other modules like inline entity form, Dropzone JS (we use drag and drop functionality to add media) etc.

Installing And Using Media Entity

To get a clear understanding let us install Media Entity, and we’ll use video embed media to add a video to see how it works and to get a solid grip on provider modules.

Install and enable Media Entity and Media embed field, open command prompt or terminal and use the Drush command:
drush en media_entity, video_embed_media -y

Go to Structure --> Select Media Bundles. rebuild the cache before proceeding.
drush cache-rebuild -y
drush cr

Creating A Media Bundle

Adding media entity through media bundle

Give a Label to the media bundle and description.

Giving a label to the media bundle; typing "video" in Label

Aha! now the selection of Provider module here you should select the Media Provider Module as said above, Media Entity uses these modules, since we installed the video embed media module we’ll see it in the drop down of the type provider, select it. 

Side note: When you select it, you’ll see a box saying “ A video embed field will be created on this media bundle when you save this form”, if you are curious as of what it is, the media bundle don’t hold the video a field holds the path of the video which will be later rendered.

Click on Save media bundle.

Go to Content > Media > Click on Add media > Select Image

If you didn’t see the Media tab rebuild the cache.

toolbar in Media; select 'add media'

Click on Video ( It’s the label you assigned to the bundle). Give the media a name, URL of the video and Click Save and Publish.

adding youtube URL in the 'Video URL' section

Yay! video of my favorite TV show is published, so should your video.

video preview of 'Breaking Bad' after the URL has been added.

Now you can add media in Drupal with ease, select from the myriad of media provider modules to insert the asset you wish to. It’s not limited to a video nor an image, check out all the other provider modules to use them on your website. Modules are the best part of Drupal!