Advanced Custom Fields and Drupal's Features module


Drupal's Features Module

One of the things that makes Drupal an excellent CMS is the ability to move site configuration between environments. This is largely thanks to the Features module, which enables you to export things like content types, custom fields and site settings into a neatly packaged module. For example, you might have a blog feature that is of the content type blog; has title, image and body fields, and has different display modes depending on whether it's a teaser or a full blog post. This blog feature might also have a view that lists 5 blog posts and displays a pager after that. It could also have a filter that enables you to filter blog posts by category. As you can see that is a lot of configuration and set up that needs to be done to create this feature. If we were to do this without the features module and we needed to make changes to a live site, we would need to first make the changes on our local database, test them with our code, push that code and then recreate the configuration on the live database manually. With features, we can download the module, track it via version control and move it easily between environments.

What's more, features will detect changes between the code in the module and the data stored in the database. So, if someone makes a change on the live database to the blog feature, features will detect it as overridden, and you can either choose to revert the changes so the configuration matches the code in the module or recreate the feature, which will download a new module with code that matches the database configuration.

Features is one of the many things that makes Drupal robust enough to handle the sites required by government organisations and medium to large enterprises who need something they can rely on.

Advanced Custom Fields

WordPress on the other hand doesn't really have an equivalent to features. You are able to define custom post types and custom fields in code in WordPress but it's easier to use Custom Post Types UI and Advanced Custom Fields to create the site configuration you need. Custom Post Types UI has the ability to export and import the PHP code it generates but currently there is no way to automate this process. The latest versions of Advanced Custom Fields (5+) include two cool features: local JSON and syncronized JSON. What local JSON does is create a JSON file whenever you save your custom field configuration in WordPress. This JSON file contains all the information about your configuration and has a number of benefits.

Firstly, storing field settings in a local JSON file is a similar idea to caching and offers a performance boost by reducing the number of database requests. Furthermore, it allows you to version control your site configuration. The really cool part, introduced in ACF 5.1.5 is synchronized JSON. The easiest way to explain it is through an example. Say you are working on your local environment, adding some custom fields and you save your settings. A JSON file is generated and you then edit your template file to use these newly created fields. Now that you've made your changes you're happy, so you commit them to git and push them to your staging environment (let's say we're using something like Pantheon). This environment has a different database that ordinarily has no idea about your new custom fields. However, because we're using a local JSON file, Advanced Custom Fields detects that there are differences between the configuration stored in the database on the staging environment and the configuration in the JSON file, and it gives you the option to sync your fields. Press this button and wham, your custom fields are now available on the staging environment. Nice!

The opportunity to sync is determined by whether or not the JSON file contains a unique key that is not found in the database or if it is, whether the field in the JSON file was modified more recently than the same field stored in the database. 

So yeah, Advanced Custom Fields JSON feature is very cool but unfortunately there aren't many WordPress plugins that have similar features, and there certainly isn't a plugin that grabs site configuration from all your plugins and packages them up in a plugin like Drupal's features module. If more plugin authors followed Advanced Custom Field's lead it would make our lives as WordPress developers much easier. WordPress is still a great CMS but in terms of configuration portability Drupal is streaks ahead thanks to the Features module.