only in patch2: unchanged: --- a/composer.json +++ b/composer.json @@ -6,5 +6,12 @@ "minimum-stability": "dev", "require": { "drupal/core": "~8.3" + }, + "extra": { + "drush": { + "services": { + "drush.services.yml": "^9" + } + } } } only in patch2: unchanged: --- a/drush.services.yml +++ b/drush.services.yml @@ -1,6 +1,6 @@ services: default_content.commands: class: \Drupal\default_content\Commands\DefaultContentCommands - arguments: ['@default_content.exporter'] + arguments: ['@default_content.exporter', '@default_content.importer', '@module_handler'] tags: - { name: drush.command } only in patch2: unchanged: --- a/src/Commands/DefaultContentCommands.php +++ b/src/Commands/DefaultContentCommands.php @@ -3,7 +3,11 @@ namespace Drupal\default_content\Commands; use Drupal\default_content\ExporterInterface; +use Drupal\default_content\ImporterInterface; use Drush\Commands\DrushCommands; +use Drush\Log\LogLevel; +use Drupal\Core\StringTranslation\PluralTranslatableMarkup; +use Drupal\Core\Extension\ModuleHandlerInterface; /** * Class DefaultContentCommands. @@ -20,13 +24,32 @@ class DefaultContentCommands extends DrushCommands { protected $defaultContentExporter; /** + * The default content importer. + * + * @var \Drupal\default_content\ImporterInterface + */ + protected $defaultContentImporter; + + /** + * Module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** * SimplesitemapController constructor. * * @param \Drupal\default_content\ExporterInterface $default_content_exporter * The default content exporter. + * + * @param \Drupal\default_content\ImporterInterface $default_content_importer + * The default content importer. */ - public function __construct(ExporterInterface $default_content_exporter) { + public function __construct(ExporterInterface $default_content_exporter, ImporterInterface $default_content_importer, ModuleHandlerInterface $module_handler) { $this->defaultContentExporter = $default_content_exporter; + $this->defaultContentImporter = $default_content_importer; + $this->moduleHandler = $module_handler; } /** @@ -89,11 +112,71 @@ class DefaultContentCommands extends DrushCommands { * @aliases dcem */ public function contentExportModule($module) { + if (!$this->isValidModule($module)) { + return; + } + $serialized_by_type = $this->defaultContentExporter->exportModuleContent($module); $module_folder = \Drupal::moduleHandler() - ->getModule($module) - ->getPath() . '/content'; + ->getModule($module) + ->getPath() . '/content'; $this->defaultContentExporter->writeDefaultContent($serialized_by_type, $module_folder); } + /** + * Imports all the content defined in a module info file. + * + * @param string $module + * The name of the module. + * + * @command default-content:import-module + * @aliases dcim + */ + public function contentImportModule($module) { + if (!$this->isValidModule($module)) { + return; + } + + if ($count = count($this->defaultContentImporter->importContent($module))) { + $message = new PluralTranslatableMarkup($count, 'Total: 1 entity imported.', '@module: @count entries imported.'); + $this->logger()->log(LogLevel::OK, $message); + } + else { + $this->logger()->log(LogLevel::WARNING, dt('No content has been imported.')); + } + } + + /** + * Imports the content provided by installed modules and default profile. + * + * @command default-content:import-all + * @aliases dcia + */ + public function contentImportAll() { + $count = count($this->defaultContentImporter->importAllContent()); + if ($count) { + $message = new PluralTranslatableMarkup($count, 'Total: 1 entry imported.', 'Total: @count entries imported.'); + $this->logger()->log(LogLevel::OK, $message); + } + else { + $this->logger()->log(LogLevel::WARNING, dt('No content has been imported.')); + } + } + + /** + * Validates that a module or a profile exists and is installed. + * + * @param string|null $module + * An installed module or the default profile. If not passed, the validation + * passes. + * + * @return bool + * TRUE if the passed module or profile name validates. + */ + protected function isValidModule($module) { + return $this->moduleHandler->moduleExists($module) || $module === \Drupal::installProfile() ? + TRUE : + drush_set_error('INVALID_MODULE', dt("Module or profile '@module' doesn't exist or is uninstalled.", ['@module' => $module])); + } + }