Blog

Streamline Your Demo Content Process in Drupal 8

Olu Adeyanju

Developer, Forum One

Being able to demonstrate what content will look like on a website that is being built is a key part of delivering a high-quality Drupal website. It allows both developers and end-users to see and understand how the CMS will function and perform. However, creating demo content can be an onerous task. The good news is that there are good Drupal 8 modules to streamline the process.

Demonstrating the core functionality of a content management system is an opportunity to test and present progress with site builds. Those responsible for the content model, site builder or developers, are more likely to see the effects of their work before it reaches a quality assurance team or the site owner. Also, when using real content, it gives the site owners an opportunity to see their content interact with site features as they’re being built. All in all, it creates more opportunities for valuable feedback.

Configuration management in Drupal 8’s core significantly streamlines module development and site building by allowing you to store and sync settings for various site components in code, but what about content? It would be wonderful to eliminate some of the tedium of recreating content,  free site builders to focus on building good user experiences and make for a more efficient and timely project development process. 

Why making demo content available is hard

Providing demo content consistently takes a fixed amount of time, yet, site features built in an iterative manner often requires deploying the site multiple times and in the process destroying content. When the site’s content is stored in a database, sharing that database is not always a viable or desirable option. For example, you may want to carry over content between multiple environments because a team is working on it or the site exists in various forms of readiness before it goes live. In other cases, a database may be too large to share. The lack of easy methods to share content between environments or teams can result in either having to recreate content or simply skip demo content altogether — both of which can lead to finding bugs too late, or needing to implement reactive changes to the content model.

Drupal contributed modules to the rescue

Fortunately, there are many people in the Drupal community that have identified this problem and developed solutions. A non-exhaustive list includes Demo Content, Content Export YAML, YAML Content (developed by Forum One’s own Stephen Lucero) and Default Content for D8.

What all these modules have in common is an easy way to import content using structured data formats like YAML or JSON. Having the content stored in these formats, especially YAML, makes it easy for people of a wide range of technical abilities to read and manually edit the content. Most of the modules require some level of comfort using Drush, which is a command-line utility for Drupal, however, Demo Content allows you to import from Drupal’s UI directly.

The listed modules differ in their ability to export content created through Drupal’s UI. Content Export YAML and Default Content for D8 provide ways to export existing content. These modules also differ in their ability to interrelate content with just Default Content for D8 reliably exporting content with relationships. YAML Content manages content relationships, but without the ability to export content from the CMS, this has to be managed manually within the content files.

Below, I outline a method for using the Default Content module because it is still actively developed and maintained. Although it lacks a UI to import and export content, its other features are robust.

Using the Default Content module for Drupal 8

Requirements

The Default Content module depends on Drupal 8’s Hal and Serialization modules.

Steps

  1. Create a custom module with an info file and a “content” directory. Mark the Default Content module as a dependency in your info file.
  2. Create your default content in the normal manner using the Drupal interface.
  3. Use drush dcer to export the content and any referenced entity (i.e., when exporting a node that contains a file field, both the node and the file entity will be exported).
    Example for a single node:

    
    drush dcer node 1 --folder=[path/to/custom_module/content]

    PowerShell example for multiple nodes assuming nodes 1 through 24 have been created.

    ForEach ( $id in 1..24 ) { drush dcer node $id --folder=path/to/custom_module/content }

    PowerShell example for multiple taxonomies using a file where each term id is on a new line.

    ForEach ( $line in [System.IO.File]::ReadLines("/path/to/file") ) { drush dcer taxonomy_term $line --folder=path/to/custom_module/content }
  4. Install and enable the module on a new site and your default content will be created there.

Notes

Refer to default_content_test for additional guidance.

When exporting an entity with its referenced entities (i.e., using dcer), a user will also be exported. You will want to delete the users from the export if they already exist within the site where this custom module will be enabled.

Generally, you should install and enable this module on a site that doesn’t have content to avoid conflicts with existing entities.

In closing

Demonstrating content during the build process does not have to be a chore. I’ve presented one module and one method for importing and exporting content, however, site builders and developers still have to do the initial work to lay the content out. After that, they can more easily: test content types without worrying about a required field that depends on a taxonomy that hasn’t been populated with terms; build views without worrying about whether content exists; and, share or redeploy code with the assurance that they have content to work with. Beyond the development workflow, an effective content demo has potential applications for automated testing to ensure the proper functioning of the site after it is in production. Using a module to import and export demo content will bring a new level of efficiency to your next Drupal 8 build.

Written By

Olu Adeyanju

Developer, Forum One

Have questions about Drupal?

Get in touch today. Our Drupal expert developers love it and can help you find the best solutions for your Drupal questions.