Features module in Drupal 8
Blog

How to use Features module in Drupal 8 to bundle functionality in reusable module

Features Module has played a significant role in deploying site configuration for Drupal 7. If you’ve ever build a site in Drupal 7, then possibly you have worked with the Features Module. However, CMI in Drupal 8 solved a lot of problems that we faced with Features in Drupal 7. In this blog, we will discuss - Why do we need Features Module in Drupal 8? What is the use of it? We will also attempt to export photo gallery feature.

Let’s take a use-case where you are working on a media and publishing company project and your client wants a feature of ‘Photo Gallery’ on his/her website. In this case, you will have to keep a track of following files:
 

  • content-type.yml
  • my-fields-storage.yml
  • view.yml
  • image-style.yml
     

So now how we will get to know that Photo Gallery feature comprises only of the above-mentioned files or still there are any more dependencies? 

Is there any way where these files are BUNDLED together? Is there any way where configuration (e.g. adding fields, image style to view) changes related to Photo Gallery feature is auto-detected?

This is where Features Module is used to bundle functionality into reusable modules. That’s main difference between CMI and Features.

Let’s see steps involved in exporting photo gallery feature in Drupal 8

Note: We are installing Feature Module using drush commands.

  • Install features module using the following drush command: 

drush en features -y

  • Create a content type My Image Gallery having an Image field.

manage fields

  • Create an Image Style, such as My Image Style. See the screenshot below for an example.

image style

  • Now create a view of the content type ‘My Image Gallery‘ with the image field.

creating a view

  • Add Image field. Select the Format as ‘Image’ and Style as ‘My image Style’.
  • Add Some content and your gallery will look like:

gallery

  • Now to Export this feature, go to Config>Development>Features.
  • Create a bundle to give a common namespace to your feature.

bundle format

  • Ones you create a bundle, Image Gallery View will be auto detected by feature:

auto detect

  • Now click on ‘My Image Gallery’. Add it to ‘xyz’ bundle and click on write.

feature page

  • It will then create a module in your custom folder by the name ‘xyz_my_image_gallery’.

module structure

  • You can place the module in any other instance and enable it. The Feature will be installed with all the configurations and we don't NEED FEATURES MODULE on other instances to install and use this module.

Notably, a Feature Module contains additional information in its .info.yml and .feature.yml file to check, update, or revert configuration programmatically.

.info.yml:

 info.yml

.features.yml:

feature.yml

How to decide what to use?

It depends on the use-cases. If you need to export and deploy simple site configuration then you can use Drupal 8 configuration management system instead of Features. Whereas Features in Drupal 8 can be used to export bundled functionality like a photo gallery feature, a blog or press room.

Media and Publishing companies are constantly transforming, and so is development, we, at Valuebound are one of those companies who specifically help organizations in their path to Digital Transformation. We enhance digital experience for end users, with customized web applications. 

Hope this blog will helpful for you to export features programmatically. If you have any doubts or questions, please comment below we will be happy to help you.

Below given is a presentation on Features Module.
 

Reference

https://www.drupal.org/project/features 
https://events.drupal.org/losangeles2015/sessions/features-drupal-8 
https://www.phase2technology.com/blog/announcing-features-drupal-8