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.
Before starting, install Drush and list out the following modules as dependencies in ProfileName.info.yml so that they get installed with the profile.
dependencies: - hal - serialization - default_content - rest - restui - file_entity
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.
name: 'Default content export' type: module description: 'Default content export' package: Web services # core: 8.x dependencies: - default_content core: '8.x'
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.
$ drush dce node <node id> # This would help to export articles, basic pages or any other node type of content. $ drush dce taxonomy_term <taxonomy term id> # This would help to export taxonomy terms of particular term id. $ drush dce file <file id> # This would help to export the files like images. $ drush dce menu_link_content <menu link id> # This would help to export the content related to a specific menu. $ drush dce block_content <block id> # This would help you to export content of block 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
$ drush dce node <node id> --folder=modules/custom/<module_name>/content/node/<node 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.
$ drush dce node 123 --folder=modules/custom/<module_name>/content/node/123.json
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
$ drush dce file <file id> --folder=modules/custom/<module_name>/content/file/filename.json
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.