logo
Submitted by Mikhail Ismajlov on Mon, 09/30/2019 - 12:40

When you import a configuration from one site to another, some configurations are needed to output content, but the configuration content itself does not contain any. In this tutorial, we will explain how to transfer blocks, nodes and taxonomy terms from one site to another.

Let's create a block on one site and transfer the configurations to another site, you can see how to transfer the configurations from one site to another in the previous article 9.9. Working with configurations in Drupal8. Active, Sync configuration, transfer configurations from DEV to LIVE. Active, Sync configures, transfer of configures from DEV to LIVE.

admin

You will see such an error: "This block is broken or missing. You may be missing content or you might need to enable the original module.":

Test lessions

The block requires an entity block with a specific uuid. UUID is a unique identifier of the entity, even if we create the same block with the same content on the new site, this block will not be caught by the configuration, because the new block will have a different UUID. You can see the UUID you need in the configuration for the block:

block content

Do not confuse the UUID of the block with the Config UUID. The thing is that configs are entity in Drupal too, but unlike blocks and nodes that are content entities, configs are config entities, in one of the next lessons we will look at ContentEntity and ConfigEntity classes.

So now you know which UUID you need for the block and you need to create this block through the code so that when you deployment your block with the correct UUID, it will be created. To do this we will use hook_update_n():

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%

Hook_update_n() you need to add the .install file of the custom module, so that the update is picked up by the module should be added to the site in advance and enabled. Each new hook_update_n() will be executed when starting update.php or database update via drush or drupal console. Unlike other hooks where you only need to change the word hook_in the beginning, in hook_update_n() you need to change more and n at the end for each update to have its own version, usually use numbers 8000 and more for Drupal 8.x, 7000 and more for Drupal 7.x, that is, the first digit is the version of the kernel drupal.

/modules/custom/drupalbook/drupalbook.install:

/**
 * Add simple block with text and image.
 */
function drupalbook_update_8001(&$sandbox) {
  $uuid = 'dc0876cf-a242-4f4c-af0e-8a27fbe9e142';
  $block = \Drupal::entityManager()->loadEntityByUuid('block_content', $uuid);
 
  if (empty($block)) {
    $block = \Drupal\block_content\Entity\BlockContent::create([
      // Block title.
      'info' => 'Simple text block with image',
      // Block bundle.
      'type' => 'basic',
      'uuid' => 'dc0876cf-a242-4f4c-af0e-8a27fbe9e142'
    ]);
    $block->body->value = '
Hello, World!


';
    $block->save();
  }
}?>

Now you need to move this code to the second site and then run the update.php file there:

drupal database

Drupal will find this update and you will see your block after it is done.

If you look at the code that the block creates, then before you create a block, we check that the block exists, so that when you update your source site, where the block already exists, there is no error about the impossibility of creating a block with an existing UUID.

Also note that only the block's text is transferred, images that are loaded online into the text will not be transferred through the image browser CKEditor, because the files will have to be transferred separately. It's possible to upload via drush, ssh, ftp or put into git (if it's 1 or 2 files, if it's bigger, it's better not to clog git with image files). The deploying process varies from project to project and uses different approaches everywhere, so it is important for the project to have at least minimal documentation describing the deploying process.

The same applies to the creation of taxonomy terms, we transfer only dictionary settings and taxonomy term fields in configures, but taxonomy terms themselves should be created separately, for example, so:

\taxonomy\Entity\Term;
...
 
/**
 * Create taxonomy term programmatically.
 */
function drupalbook_update_8002(&$sandbox) {
  $term = Term::create([
    'name' => 'Drupal 8',
    'vid' => 'tags',
  ])->save();
}?>

In the vid field you need to specify the machine name of the taxonomy dictionary (Vocabulary ID).

To create nodes, you need to use approximately this code:

/**
 * Create node programmatically.
 */
function drupalbook_update_8003(&$sandbox) {
  $node = Node::create(['type' => 'page']);
  $node->set('title', 'About us');
  $body = [
    'value' => '
Text about us

',
    'format' => 'basic_html',
  ];
  $node->set('body', $body);
  $node->status = 1;
  $node->enforceIsNew();
  $node->save();
}?>

Approximately, because you can also write instead of set() just equal to:

= [
    'value' => '
Text about us

',
    'format' => 'basic_html',
  ];
  $node->set('body', $body);
 
// Или
 $node->body->format =  'basic_html';
 $node->body->value =  '
Text about us

',?>

Each time you add a new hook_update_n (), you need to run update.php. If you use Acquia or other hosting with auto-deployment, then this process is automated and you need to clarify how the deployment will occur.

Code examples can be viewed on github:
https://github.com/levmyshkin/drupalbook8