Programmatically create nodes in a site that misses the target content type configurations — Drupal 8/9

Pasan Gamage
3 min readMay 27, 2022

--

The why

In this article, I’m going to explain two options which you can choose to create nodes when the site database do not have the relevant target config entities.

The main issue here is that we cannot use update hooks directly to create nodes because standard practice is to execute updb prior to config-import. Therefore, the hooks will not create the nodes programmatically, as the configs are not in the system database yet.

The fix

There are two ways around this.

  1. Partially import all the missing configurations through command line and then deploy the node creation code.
  2. Place the node creation code inside a module and deploy the code. Then use command line.

Option 1 — Partial configuration import.

If you plan to use this, you have to first export all the configuration yml files from your working repository which are missing in the destination site database and manually copy them over to a specific folder. This folder needs to be copied over to the destination site server and placed inside config-export folder. Finally, use drush cim command on the server.

Example:

Let's say we need to programmatically create nodes for a search content type,

First create the content type and all its fields locally and then do a drush config-export.

Then copy all the new and updated YML files that are relevant to the search content type and paste them into a new folder created inside the Drupal config-export folder.

i.e. config-export/search

Now you need to copy this folder to the destination sites config-export.

Using the command line of the destination server, execute the following drush command.

drush cim --partial --source=../config-export/search

This will import the configurations into the destination Drupal site. Verify the configs were imported successfully without errors.

Now that’s done, you can go about deploying the code with the update hooks and all the configuration files in one code commit that will programmatically create new nodes for the Search content type given your deployment process automatically executes updb and config-import in order.

Option 2 — Using a custom module

This option lets you deploy single code commit with the configuration files and instead of using update hooks, you can use a Drupal module.

You can either add the node creation code into a new Drupal module and enable the module. You will need to make sure by doing so the specific code will execute and create the nodes. After this step is complete, at a later stage you can disable this module.

The second method is to use an existing custom module and add a method in the .module file in that custom module. And then manually execute it using drush ev command from the command line.

Example with using an existing module:

Add a new function inside a module file with all the logic.

/*** For one time execution only.*/function _create_initial_content() {   _create_search_page();}
/*** Page creation logic.*/function _create_search_page() { // Add your logic here.
}

From the command line, simply run

drush ev “_create_initial_content();”

Hope this helps, happy coding.

--

--

Pasan Gamage
Pasan Gamage

Written by Pasan Gamage

Backend Developer | Motorbike enthusiast

No responses yet