Problem/Motivation

I'm trying to import content previously exported via this module into a hook_update.

Steps to reproduce

1. Export content from site and put it in the sync directory configured in your settings.php
2. Delete content from site
3. Create a hook_update to import this content again programmatically
4. Infinite loop

Proposed resolution

I don't have one

More information

To add more information, this is my hook update :

/**
 * Import Content 
 */
function global_update_8007() {
  // Import Block
  $drush_import_content = \Drupal::service('content_sync.commands');
  $drush_import_content->import(NULL, ['uuids' => 'ab2ef383-fb2b-47b2-aa01-ad9033102549']);
}

And this is the print from the console :

/var/www/web/sites/default $ drush updb
 --------------- ----------- --------------- ----------------------------------
  Module          Update ID   Type            Description
 --------------- ----------- --------------- ----------------------------------
  global            8007        hook_update_n   Import Content 
 --------------- ----------- --------------- ----------------------------------


 Do you wish to run the specified pending updates? (yes/no) [yes]:
 > y

>  [notice] Update started: global_update_8007
> >  [notice] Update started: global_update_8007
> > >  [notice] Update started: global_update_8007
> > > >  [notice] Update started: global_update_8007
> > > > >  [notice] Update started: global_update_8007
> > > > > >  [notice] Update started: global_update_8007
> > > > > > >  [notice] Update started: global_update_8007
...

Please note that if I run the corresponding drush command, it works :

/var/www/web/sites/default $ drush content-sync:import --uuids=ab2ef383-fb2b-47b2-aa01-ad9033102549
Differences of the export directory to the active content:

+--------------------------------+---------------------------------------------------------------------+-----------+
| Collection                     | Content Name                                                        | Operation |
+--------------------------------+---------------------------------------------------------------------+-----------+
| block_content.current_thematic | block_content.current_thematic.ab2ef383-fb2b-47b2-aa01-ad9033102549 | Create    |
+--------------------------------+---------------------------------------------------------------------+-----------+

 Do you want to import? (yes/no) [yes]:
 > y

Thanks for taking the time to read this post.

Comments

Greg__ created an issue. See original summary.

greg__’s picture

Issue summary: View changes
nicolas bouteille’s picture

Hi, I am about to tryout this module with the same kind of use case in mind. Only exception is we use hook_deploy instead of hook_update for content creation / deployment. This solved some issues we faced in the past. Do you encounter the same infinite loop with hook_deploy?

blanca.esqueda’s picture

I haven't had the chance to troubleshoot the loop on the hook using commands.
But I've imported it in the following way too, in case it is useful.

<?php

use Drupal\Core\Serialization\Yaml;

/**
 * Implements hook_install().
 */
function mymodule_init_install() {
  global $content_directories;
  // filter what you want to import (nodes, paragraphs, etc..)
  $items = glob($content_directories['sync']. '/taxonomy_term.*.yml');

  foreach ($items as $item) {
    $raw = file_get_contents($item);
    $decoded = Yaml::decode($raw);
    $entity = \Drupal::service('content_sync.importer')->importEntity($decoded);
  }
}