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.
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
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"
Follow the instructions:
git remote add origin ssh://firstname.lastname@example.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:
Edit the features_live/sites/default/settings.php file:
sudo nano features_live/sites/default/settings.php
Set the database as features_live:
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:
Leave rest of the settings as they are and save the content type.
Go to admin/structure/views/add and create a view:
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:
Fill in the name and description:
Select the custom content type and view in the components section to the right:
Select download archive to download the feature:
Install the feature
In the features_live site go to admin/modules/install and select the packet we downloaded in the last section:
Go to admin/modules and enable the module:
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:
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:
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:
Go to admin/modules and you have again the MyFeature module available. Enable it.
In the features_dev site edit the CustomView in admin/structure/views/view/customview and change the Administrative description:
Go to admin/config/development/features and you should see "changed" indicator next to the feature:
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:
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:
Go to admin/config/development/features and you can see that the state has changed also for the live site:
Click the orange Changed link, check MyFeature and select Import changes:
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:
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