diff --git a/default_content.services.yml b/default_content.services.yml index 1b12a27..3281706 100644 --- a/default_content.services.yml +++ b/default_content.services.yml @@ -9,3 +9,7 @@ services: default_content.exporter: class: Drupal\default_content\Exporter arguments: ['@serializer', '@entity_type.manager', '@entity.repository', '@rest.link_manager', '@event_dispatcher', '@module_handler', '@info_parser', '%default_content.link_domain%'] + default_content.config_subscriber: + class: Drupal\default_content\Config\DefaultContentConfigSubscriber + arguments: ['@default_content.importer'] + tags: [{ name: event_subscriber }] diff --git a/src/Config/DefaultContentConfigSubscriber.php b/src/Config/DefaultContentConfigSubscriber.php new file mode 100644 index 0000000..9caa747 --- /dev/null +++ b/src/Config/DefaultContentConfigSubscriber.php @@ -0,0 +1,53 @@ +defaultContentImporter = $default_content_importer; + } + + /** + * Creates default content after config synchronization. + * + * @param \Drupal\Core\Config\ConfigImporterEvent $event + * The config importer event. + */ + public function onConfigImport(ConfigImporterEvent $event) { + $modules = $event->getConfigImporter()->getExtensionChangelist('module', 'install'); + foreach ($modules as $module) { + $this->defaultContentImporter->importContent($module); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + return [ConfigEvents::IMPORT => 'onConfigImport']; + } + +} diff --git a/tests/src/Functional/DefaultContentTest.php b/tests/src/Functional/DefaultContentTest.php index caeaf15..d0be741 100644 --- a/tests/src/Functional/DefaultContentTest.php +++ b/tests/src/Functional/DefaultContentTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\default_content\Functional; +use Drupal\Core\Config\ExtensionInstallStorage; +use Drupal\Core\Config\FileStorage; use Drupal\simpletest\BrowserTestBase; use Drupal\simpletest\ContentTypeCreationTrait; use Drupal\simpletest\NodeCreationTrait; @@ -51,4 +53,37 @@ class DefaultContentTest extends BrowserTestBase { $this->assertTrue(!empty($term_id), 'Term reference populated'); } + /** + * Test importing default content via ConfigImporter. + */ + public function testImportViaConfigImporter() { + $sync = $this->container->get('config.storage.sync'); + $this->copyConfig($this->container->get('config.storage'), $sync); + + // Enable the module using the ConfigImporter. + $extensions = $sync->read('core.extension'); + $extensions['module']['default_content_test'] = 0; + $extensions['module'] = module_config_sort($extensions['module']); + $sync->write('core.extension', $extensions); + // Slightly hacky but we need the config from the test module too. + $module_storage = new FileStorage(drupal_get_path('module', 'default_content_test') . '/config/install'); + foreach ($module_storage->listAll() as $name) { + $sync->write($name, $module_storage->read($name)); + } + $this->configImporter()->import(); + + // Login as admin. + $this->drupalLogin($this->drupalCreateUser(array_keys(\Drupal::moduleHandler()->invokeAll(('permission'))))); + + $node = $this->getNodeByTitle('Imported node'); + $this->assertEquals($node->body->value, 'Crikey it works!'); + $this->assertEquals($node->getType(), 'page'); + $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple(); + $term = reset($terms); + $this->assertTrue(!empty($term)); + $this->assertEquals($term->name->value, 'A tag'); + $term_id = $node->field_tags->target_id; + $this->assertTrue(!empty($term_id), 'Term reference populated'); + } + }