Default Content¶
Drupal 11.3 and later¶
As of version 11.3.0, Drupal core includes the ability to export default content at the command line. To use it, switch to the Drupal root and run the following command:
For example, to export node 3: This will output a YAML representation of the node, with all its fields and translations. You can pipe the output to a file to save it: But exporting a single entity isn't always useful. You usually want to export it along with its dependencies -- for example, you probably want to export any images that the content references, or any taxonomy terms it's tagged with. You can do this with the--with-dependencies option (or -W for short):
This will create a content directory in the Drupal root, which will contain the exported node and all of its dependencies, segmented into different subdirectories by entity type.
The content:export command also allows you to export content in bulk. For example, you can export all content of a specific type:
--bundle more than once:
And you can combine that with --with-dependencies to include the dependencies of all content:
To really cast a wide net, you can export all content of a given entity type:
So, as a final, realistic example, here's how you might export all image media into a recipe:
php core/scripts/drupal content:export media --bundle=image --with-dependencies --dir=../recipes/my_recipe/content
For developers, more information about core's default content exporter is available at https://www.drupal.org/node/3533854.
Drupal 11.2 and earlier: using the Default Content module¶
If you are using Drupal 11.2 or earlier, you need to use the Default Content module to export content. The export process is the same for modules and recipes; let's review that now.
Getting set up¶
Install the module as you normally would. If using the module for creating content for recipes, install it in the dev section of your composer requirements as it is not needed in production.
And enable the module: The module doesn’t have a user interface. Instead, it provides drush commands that can be run to export content.What can we export?¶
Out of the box, we can export the following entity types:
- Content type nodes: node
- Taxonomy Terms: taxonomy_term
- Files: file
- Media: media
- Menu Links: menu_link_content
- Content blocks: block_content
- Shortcuts: shortcut
At this time, there isn’t a way to export by bundle type, think all nodes of type Article, or all menu links from the main menu, but there is a patch for it.
Let’s take a look at the three commands that the module provides to export content.
Default content drush commands¶
default-content-export-references¶
Exports an entity and all its referenced entities. This is my go to command as it helps you export referenced media, files, taxonomy terms, and users from the parent node.
Alias: * dcer
Arguments: * entity_type – The entity type to export. * entity_id – The ID of the entity to export
One of these arguments is required.
Options:
- folder – The module or recipe’s content folder to export to. Entities are automatically grouped by entity type into directories.
Examples:
# Exports node 123 and all its dependencies.
# To a module:
drush dcer node 123 module_name
# To a recipe:
drush dcer node 123 --folder=recipes/recipe_name/content
# Export all nodes and all their dependencies.
drush dcer node --folder=modules/custom/my_custom_module/content
default-content-export¶
Exports a single entity. If you want to manage your own dependencies.
Alias: * dce
Arguments: * entity_type – The entity type to export. * entity_id – The ID of the entity to export
Both of these arguments are required.
Options: * file – Write out the exported content to a file instead of stdout
Examples:
default-content-export-module¶
Exports all the content defined in a module info file. This is most helpful for distribution and install profile maintainers where they would want to maintain and update default content from a canonical source.
Alias: * dcem
Arguments: * module – The machine name of the module
The argument is required.
Options: * None
Examples:
# Exports all the content defined in a module info file.
drush dcem module_name
# The module's info file would need to have a section like this:
default_content:
node:
- c9a89616-7057-4971-8337-555e425ed782
- b6d6d9fd-4f28-4918-b100-ffcfb15c9374
file:
- 59674274-f1f5-4d6a-be00-fecedfde6534
- 0fab901d-36ba-4bfd-9b00-d6617ffc2f1f
media:
- ee63912a-6276-4081-93af-63ca66285594
- bcb3c719-e266-45c1-8b90-8f630f86dcc7
menu_link_content:
- 9fbb684c-156d-49d6-b24b-755501b434e6
- 19f38567-4051-4682-bf00-a4f19de48a01
block_content:
- af171e09-fcb2-4d93-a94d-77dc61aab213
- a608987c-1b74-442b-b900-a54f40cda661