Drupal 8 - The Features Module Use Case (Article + Video)

Submitted by TheMain on Tue, 04/11/2017 - 08:05

How to move partial configuration between sites using the Features module.

In this tutorial I will show you how to bundle a view and a content type together as a feature and move it between two sites. First we move the feature manually and then with the help of GIT.

Preparation

Let's create two sites to test this. A fast way to install a site is to use Drush. Go to your document root (like ~/Sites) and do this:

drush dl drupal --drupal-project-rename=features_dev
cd features_dev
drush si standard --db-url='mysql://root:root@localhost/features_dev' --site-name=features_dev --account-name=admin --account-pass=admin

More useful Drush commands: http://www.wdtutorials.com/cheat-sheets/drupal-8-drush-cheat-sheet

Install the features module (run this twice):

drush -y en features features_ui

Git repository

You don't have to setup a GIT repository for the first example, but in the second example we use GIT to move the feature.

Init a GIT repository in the site root and make an initial commit:

cp example.gitignore .gitignore
git init
git add .
git commit -m"initial"

Go to https://bitbucket.org/, create an account and create a repo in here: https://bitbucket.org/repo/create:

18

Follow the instructions:

19

git remote add origin ssh://git@bitbucket.org/samuli_natri/t_features.git 
git push -u origin master 

Note: you can add an SSH key to the account so you don't have to fill in the username / password when accessing the repo: https://confluence.atlassian.com/bitbucket/add-an-ssh-key-to-an-account-302811853.html

Let's duplicate the site as features_live:

cd ~/Sites
cp -fr features_dev features_live

Duplicate the features_dev database as features_live. With MAMP you can do it like this:

Go to http://localhost/phpmyadmin, select the features_dev database and select operations. Use the Copy database to option:

20

Edit the features_live/sites/default/settings.php file:

sudo nano features_live/sites/default/settings.php

Set the database as features_live:

22

Create a view and content type

Create a custom content type and view for the features_dev site.

Go to admin/structure/types/add and add a custom content type:

CustomType

Leave rest of the settings as they are and save the content type.

Go to admin/structure/views/add and create a view:

CustomView

03

Leave rest of the settings as they are and save the view.

Create the feature

Go to admin/config/development/features and select +Create new feature:

04

Fill in the name and description:

07

Select the custom content type and view in the components section to the right:

08

06

Select download archive to download the feature:

09

Install the feature

In the features_live site go to admin/modules/install and select the packet we downloaded in the last section:

10

Select Install.

Go to admin/modules and enable the module:

11

Now you should see the CustomView in admin/structure/views and CustomType in admin/structure/types.

Using GIT to move the feature

Now if you want to make changes to the feature, you have to download and move it manually again. Better way would be to use a version control system. Let's do it with GIT.

In the features_dev site go to admin/config/development/features/edit and create the feature again and use the write button:

12

At the moment 2017.04.10 the write button doesn't seem to work if you have used the download button so you have refresh the page and re-create the feature.

Now the module should be stored in modules/custom/myfeature. Go to admin/modules and enable it as you would enable any module:

13

Add it to the version control system and push it to the repo:

git add .
git commit -m "adds MyFeature"
git push

In the features_live site go to admin/modules/uninstall, uninstall the MyFeature module and delete the folder:

rm -fr modules/myfeature

Also delete the CustomView and CustomType it created.

Next pull our feature with GIT:

git pull

Go to admin/modules and you have again the MyFeature module available. Enable it.

Making changes

In the features_dev site edit the CustomView in admin/structure/views/view/customview and change the Administrative description:

14

15

Go to admin/config/development/features and you should see "changed" indicator next to the feature:

16

Edit the feature in admin/config/development/features/edit/myfeature and select write.

Go to the features_dev site folder and run git status to see that the feature has changed:

git status

17

Add the change, commit it and push it to the repo:

git add .
git commit -m"adds description for the customview"
git push

Go to features_live folder and pull the changes:

git pull

Go to admin/config/development/features and you can see that the state has changed also for the live site:

16

Click the orange Changed link, check MyFeature and select Import changes:

23

Now if you look at the the CustomView in the live site admin/structure/views/view/customview, you can see that the view description has changed:

24

Read more: https://www.drupal.org/docs/8/modules/features/

You might also be interested in this:

Configuration Split Module: https://www.drupal.org/project/config_split

Posted by Samuli Natri on 11 April 2017