Default content module Drupal 8
CMS

Using the Default Content module in Drupal 8

Why Default Content Module?

Default content gives your module and installation profile a way to ship default content. While developing a Drupal 8 site, there might be chances where we have to reinstall the site due to some issues. When we install a new site, we need to add all the content repeatedly for the entities.  This is where default content module comes into play.  We can simply export all the content of the entities into our custom module and when we install the module all the content for the respective entities will be created prior, rather than creating them again.

Where can it be used?

When you are creating drupal profiles/distributions it would require that we set up some default content which would be set up once you install it so that the user understands how a feature like say Slider looks like and where to set up, an article listing page where we can create some articles which will give user an understanding of how the article information is presented. It can also be used when you create a team and you want to give an idea of how a particular page or piece of content will be presented by the theme.

How does it work?

In this blog, we look at how to get default content during a profile installation. The pre-requisite for this implementation is a community contributed module called default content and couple of other modules listed below as dependencies. Default content module facilitates to create these content on installation by taking the required data in the form of JSON which needs to be provided by a custom module.

Requirements:

Before starting, install Drush and list out the following modules as dependencies in ProfileName.info.yml so that they get installed with the profile.

Custom Module Creation: 

Create a custom module in <root folder>/profiles/<Profilename>/modules/custom folder .In our case,  let content_export be our custom module that has the default_content module as the dependency.
1.  content_export.info.yml

2. All the hal+json files of the respective entity type have to be placed in the content folder of our custom module.  In our case,  it would be content_export/content/<entity type>. Like all the JSON files related to nodes have to be stored in  content_export/content/ node, similarly content_export/content/files to store JSON format of all files.

Run the following commands from the root folder using Drush to get the JSON format of the respective entity type.

After running the following command,  copy the JSON format and save it as <entity id>.json in <module_name>/content/<entity_name> where entity id could be node id,block id, file id etc.,

In order to store the JSON format of a particular entity into a folder rather than copying and saving, run the following Drush command. Now the JSON file would be created in <module_name>/content/<entity_type>/<entity id>.json

Example: To store a content of type article with node_id 123 into a particular folder,  run the following command from your root folder.

In order to send images i.e files, go to admin/config/services/rest. Enable the “file” resource, set the method to post and request formats to hal_json and JSON. Save the configuration

Run the following Drush command from your root folder

Similarly, default content of any entity can be exported during installation profile. Once the profile gets installed, a minimal content will be available for your Drupal Site which is created using the profile.

This should give you an idea of how you are able to populate some default content during profile installation and you can explore further to extend it to different types of content requirements for your implementation.

About The Author

Leave a Reply

*