How to override a feature module by cloning

Description

Collection: Change Management and Version ControlChapter: How to Override Shared Features and Modify Text

Transcript

In previous videos, we looked at overriding various components of a feature module by using hooks, and this way we could use a shared feature module that could have the same base regardless of where it was, and we wouldn't have to actually modify that code, making future updates more difficult. But sometimes, we can simply copy a feature and make it our own module because we don't necessarily mean to pull in future updates to the basic module. This will still save us a lot of time in the initial setup process and from then on out, we'd have the flexibility to do with that particular feature module whatever we needed to do with it.

So what we're going to do is actually take an existing feature module and clone it, and this takes a few steps. If you've ever copied a module to use as a base for another module or copied a theme to use as a base for a custom theme, the process is going to be the same. We'll first copy the folder; we'll rename the files; and then we'll rename any references to those files and any function names within the files.

So let's go ahead and do the first part which is copying the feature. Now, I'm here in the Drupal site directory, and inside of the site/all/modules/features folder we have a couple of feature modules we can work with. I'm going to go ahead and clone this articles feature so you can use the example_blog_feature if you haven't been following along.

This one is from our resource pack. So I'm going to duplicate this. On Mac there is a duplicate option but we can simply copy and paste, and I'm going to rename it newarticles.

Now, this is going to be the name of the new module so I'm going to copy that name and I'm going to go through each file here and rename it. Okay. So now when we look, every file begins with newarticles, and we just need to make sure that any dot features include files are not completely renamed.

Just the first part before the initial period is renamed to the new module name. Then we have newarticles dot info and newarticles dot module. Let's go ahead and begin by modifying the dot info file.

I'm going to open this up. So the only two things we need to change here is the name of the module and the description. We'll call this New Articles and then let's go ahead and leave the description as is.

I'm going to save this file and then I'm going to jump back to our features module directory and I'm going to open up the dot module file. Now, there's just one line here we need to change which is this include_once to not say articles dot features but newarticles dot features dot inc. Let's save that.

And from here, we simply need to replace the function names on all of the files. So I'm going to open up one of these files to show you an example. So this function here is articles_field_default_fields() and this is an implementation of a hook, hook_field_default_fields() and so this hook part or this articles part here needs to be renamed with newarticles.

And we're just going to continue to do this for all of the functions. So in my editor, there is a tool to be able to search and replace across multiple files, and chances are your editor has something similar. So I'm going to go ahead and navigate to the location of this features module inside of the directory browser in my editor.

Okay. So here we are at newarticles and I'm going to right-click it and do a replace. What we're going to do is look for a pattern that indicates that it's a function name.

In this series we uncover how to use powerful tools to speed up and stabilize the Drupal development process. We begin with a thorough walk-through of Git, the version control system. After we cover how to patch modules and upgrade Drupal from one minor version to another, we demonstrate how to use the Features module to version control components of Drupal that aren't typically available to version control. Finally, we wrap up with how to integrate Drush - a powerful command line tool for Drupal - into your workflow to speed up the process of doing everything we discussed earlier in the series.

Some of the key points we'll be covering include:

How to use the command line

How to work with the Features module to capture important database changes and make deployment easier.

How to use Git for version control

Best practices for working on a Drupal team

Who this collection is for

This video collection is mainly for developers who want to improve their deployment strategies and learn how to work on a shared Drupal code base with other developers. However, even if you are not a developer, our coverage of using Features and version control is extremely valuable for deploying Drupal sites and sharing reusable components between projects.

Prerequisites

Even if you are not a developer, you will be able to follow with most of this collection. When we talk about code we do not go into detail about the basic components of the code, so you may want to brush up on some coding basics in the "PHP Programming Basics" collection.

It looks like there might be an issue playing videos in this browser. We're working on better cross-brower experience, but in the meantime please try the latest Chrome or Firefox browsers. Or, you may be able to watch the video directly without progress tracking or transcript: