When developing with Drupal 8 at the moment, you may be reinstalling multiple times due to bugs, or simply pulling the latest HEAD release.
If you have custom entities, or even custom content types, this can be tiresome to constantly readd the content, or to have to have some sort of Drush command to do this for you.
This is where Default content comes in handy. You can simply export the entities into your module, then when you reinstall the module those entities will be recreated for you.
It’s quite straight forward to get this to work, you will need the following:
- Drush (tested with latest version)
- Drupal 8 installed and running
- default_content cloned locally into your modules folder, and enabled
- A custom module you want to export to.
For this example, I want to recreate my Dinosaur entity whenever I reinstall the site. I have created a GitHub example repository at https://github.com/joshuataylor/dinodb, which you can take a look at.
To include default_content with our module, we will need to add
default_content as a dependency. Open your modulename.info.yml file and add
default_content as a dependency:
dependencies: - default_content
Now you will need to create the folder
dinosaur under your module, and then the folder
dinosaur under that (where
dinosaur is your entity).
Now you can run the drush command to export your entities one at a time:
drush --uri=http://example.com default-content-export dinosaur 1 --file=modules/custom/dinodb/content/dinosaur/1.json
This will export the
dinosaur entity with ID 1 to your modules content folder.
Now when you reinstall your module, your entities will be created!