Skip to content
Permalink
Browse files

4044 config export all option (#4045)

* Add missing use statements

* Add option ALL for config export entity command. Replace config export content type with a chainQueue

* Add include dependency option for content type export, entity config export. Replace view export command with chainQueue

* Fix undefined variable

* CLean up config export after generation
  • Loading branch information...
LOBsTerr authored and enzolutions committed May 22, 2019
1 parent 813ad24 commit a026e445d3900327692d6d1d0f14b17803662b7b
@@ -36,7 +36,7 @@ services:
- { name: drupal.command }
console.config_export_view:
class: Drupal\Console\Command\Config\ExportViewCommand
arguments: ['@entity_type.manager', '@config.storage', '@console.extension_manager', '@console.validator']
arguments: ['@entity_type.manager', '@config.storage', '@console.extension_manager', '@console.validator', '@console.chain_queue']
tags:
- { name: drupal.command }
console.config_import:
@@ -9,15 +9,15 @@
use Drupal\Console\Command\Shared\ExportTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Utils\Validator;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Extension\Manager;
use Drupal\Core\Config\CachedStorage;
@@ -26,6 +26,8 @@ class ExportContentTypeCommand extends Command
use ModuleTrait;
use ExportTrait;
const ALL = '-all-';
/**
* @var Manager
*/
@@ -86,7 +88,7 @@ protected function configure()
->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
->addArgument(
'content-type',
InputArgument::REQUIRED,
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
$this->trans('commands.config.export.content.type.arguments.content-type')
)->addOption(
'optional-config',
@@ -104,6 +106,12 @@ protected function configure()
InputOption::VALUE_NONE,
$this->trans('commands.config.export.content.type.options.remove-config-hash')
)
->addOption(
'include-module-dependencies',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.config.export.content.type.options.include-module-dependencies')
)
->setAliases(['cect']);
$this->configExport = [];
@@ -119,10 +127,9 @@ protected function interact(InputInterface $input, OutputInterface $output)
// --content-type argument
$contentType = $input->getArgument('content-type');
if (!$contentType || $contentType == 'all') {
if (!$contentType) {
$bundles_entities = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
$bundles = ['all' => $this->trans('commands.config.export.content.type.questions.all')];
$bundles = [ExportContentTypeCommand::ALL => $this->trans('commands.config.export.content.type.questions.all')];
$bundles_ids = [];
foreach ($bundles_entities as $entity) {
@@ -136,12 +143,12 @@ protected function interact(InputInterface $input, OutputInterface $output)
$bundles
);
}
}
if ($contentType == 'all') {
$input->setArgument('content-type', $bundles_ids);
}else{
$input->setArgument('content-type', [$contentType]);
if ($contentType == ExportContentTypeCommand::ALL) {
$input->setArgument('content-type', $bundles_ids);
}else{
$input->setArgument('content-type', [$contentType]);
}
}
$optionalConfig = $input->getOption('optional-config');
@@ -150,8 +157,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
$this->trans('commands.config.export.content.type.questions.optional-config'),
true
);
$input->setOption('optional-config', $optionalConfig);
}
$input->setOption('optional-config', $optionalConfig);
if (!$input->getOption('remove-uuid')) {
$removeUuid = $this->getIo()->confirm(
@@ -167,6 +174,15 @@ protected function interact(InputInterface $input, OutputInterface $output)
);
$input->setOption('remove-config-hash', $removeHash);
}
$includeModuleDependencies = $input->getOption('include-module-dependencies');
if (!$includeModuleDependencies) {
$includeModuleDependencies = $this->getIo()->confirm(
$this->trans('commands.config.export.content.type.questions.include-module-dependencies'),
true
);
$input->setOption('include-module-dependencies', $includeModuleDependencies);
}
}
/**
@@ -179,82 +195,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$optionalConfig = $input->getOption('optional-config');
$removeUuid = $input->getOption('remove-uuid');
$removeHash = $input->getOption('remove-config-hash');
foreach ($contentTypes as $contentType) {
$contentTypeDefinition = $this->entityTypeManager->getDefinition('node_type');
$contentTypeName = $contentTypeDefinition->getConfigPrefix() . '.' . $contentType;
$contentTypeNameConfig = $this->getConfiguration($contentTypeName, $removeUuid, $removeHash);
if (empty($contentTypeNameConfig)) {
throw new InvalidOptionException(sprintf('The content type %s does not exist.', $contentType));
}
$this->configExport[$contentTypeName] = ['data' => $contentTypeNameConfig, 'optional' => $optionalConfig];
$this->getFields($contentType, $optionalConfig, $removeUuid, $removeHash);
$this->getFormDisplays($contentType, $optionalConfig, $removeUuid, $removeHash);
$this->getViewDisplays($contentType, $optionalConfig, $removeUuid, $removeHash);
$this->exportConfigToModule($module, $this->trans('commands.config.export.content.type.messages.content-type-exported'));
}
}
protected function getFields($contentType, $optional = false, $removeUuid = false, $removeHash = false)
{
$fields_definition = $this->entityTypeManager->getDefinition('field_config');
$fields_storage = $this->entityTypeManager->getStorage('field_config');
foreach ($fields_storage->loadMultiple() as $field) {
$field_name = $fields_definition->getConfigPrefix() . '.' . $field->id();
$field_name_config = $this->getConfiguration($field_name, $removeUuid, $removeHash);
// Only select fields related with content type
if ($field_name_config['bundle'] == $contentType) {
$this->configExport[$field_name] = ['data' => $field_name_config, 'optional' => $optional];
// Include dependencies in export files
if ($dependencies = $this->fetchDependencies($field_name_config, 'config')) {
$this->resolveDependencies($dependencies, $optional);
}
}
}
}
protected function getFormDisplays($contentType, $optional = false, $removeUuid = false, $removeHash = false)
{
$form_display_definition = $this->entityTypeManager->getDefinition('entity_form_display');
$form_display_storage = $this->entityTypeManager->getStorage('entity_form_display');
foreach ($form_display_storage->loadMultiple() as $form_display) {
$form_display_name = $form_display_definition->getConfigPrefix() . '.' . $form_display->id();
$form_display_name_config = $this->getConfiguration($form_display_name, $removeUuid, $removeHash);
// Only select fields related with content type
if ($form_display_name_config['bundle'] == $contentType) {
$this->configExport[$form_display_name] = ['data' => $form_display_name_config, 'optional' => $optional];
// Include dependencies in export files
if ($dependencies = $this->fetchDependencies($form_display_name_config, 'config')) {
$this->resolveDependencies($dependencies, $optional);
}
}
}
}
protected function getViewDisplays($contentType, $optional = false, $removeUuid = false, $removeHash = false)
{
$view_display_definition = $this->entityTypeManager->getDefinition('entity_view_display');
$view_display_storage = $this->entityTypeManager->getStorage('entity_view_display');
foreach ($view_display_storage->loadMultiple() as $view_display) {
$view_display_name = $view_display_definition->getConfigPrefix() . '.' . $view_display->id();
$view_display_name_config = $this->getConfiguration($view_display_name, $removeUuid, $removeHash);
// Only select fields related with content type
if ($view_display_name_config['bundle'] == $contentType) {
$this->configExport[$view_display_name] = ['data' => $view_display_name_config, 'optional' => $optional];
// Include dependencies in export files
if ($dependencies = $this->fetchDependencies($view_display_name_config, 'config')) {
$this->resolveDependencies($dependencies, $optional);
}
}
}
$includeModuleDependencies = $input->getOption('include-module-dependencies');
$this->chainQueue->addCommand(
'config:export:entity', [
'entity-type' => 'node_type',
'bundle' => $contentTypes,
'--module' => $module,
'--optional-config' => $optionalConfig,
'--remove-uuid' => $removeUuid,
'--remove-config-hash' => $removeHash,
'--include-module-dependencies' => $includeModuleDependencies
]
);
}
}
@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\Console\Command\Config\ExportContentTypeCommand.
* Contains \Drupal\Console\Command\Config\ExportEntityCommand.
*/
namespace Drupal\Console\Command\Config;
@@ -26,6 +26,8 @@ class ExportEntityCommand extends Command {
use ModuleTrait;
use ExportTrait;
const ALL = '-all-';
/**
* @var EntityTypeManagerInterface
*/
@@ -93,7 +95,7 @@ protected function configure() {
)
->addArgument(
'bundle',
InputArgument::REQUIRED,
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
$this->trans('commands.config.export.entity.arguments.bundle')
)->addOption(
'optional-config',
@@ -111,6 +113,12 @@ protected function configure() {
InputOption::VALUE_NONE,
$this->trans('commands.config.export.entity.options.remove-config-hash')
)
->addOption(
'include-module-dependencies',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.config.export.entity.options.include-module-dependencies')
)
->setAliases(['cee']);
$this->configExport = [];
@@ -132,34 +140,43 @@ protected function interact(InputInterface $input, OutputInterface $output) {
$this->trans('commands.config.export.entity.questions.content-type'),
$entity_types['Configuration']
);
$input->setArgument('entity-type', $entityType);
}
$input->setArgument('entity-type', $entityType);
// --bundle argument
$bundle = $input->getArgument('bundle');
if (!$bundle) {
$bundles_entities = $this->entityTypeManager->getStorage($entityType)
->loadMultiple();
$bundles = [ExportEntityCommand::ALL => $this->trans('commands.config.export.entity.questions.all')];
$bundles_ids = [];
foreach ($bundles_entities as $entity) {
$bundles[$entity->id()] = $entity->label();
$bundles_ids[] = $entity->id();
}
$bundle = $this->getIo()->choice(
$this->trans('commands.config.export.entity.questions.bundle'),
$bundles
);
if ($bundle == ExportEntityCommand::ALL) {
$input->setArgument('bundle', $bundles_ids);
} else {
$input->setArgument('bundle', [$bundle]);
}
}
$input->setArgument('bundle', $bundle);
$optionalConfig = $input->getOption('optional-config');
if (!$optionalConfig) {
$optionalConfig = $this->getIo()->confirm(
$this->trans('commands.config.export.entity.questions.optional-config'),
TRUE
);
}
$input->setOption('optional-config', $optionalConfig);
$input->setOption('optional-config', $optionalConfig);
}
if (!$input->getOption('remove-uuid')) {
$removeUuid = $this->getIo()->confirm(
@@ -168,13 +185,23 @@ protected function interact(InputInterface $input, OutputInterface $output) {
);
$input->setOption('remove-uuid', $removeUuid);
}
if (!$input->getOption('remove-config-hash')) {
$removeHash = $this->getIo()->confirm(
$this->trans('commands.config.export.entity.questions.remove-config-hash'),
TRUE
);
$input->setOption('remove-config-hash', $removeHash);
}
$includeModuleDependencies = $input->getOption('include-module-dependencies');
if (!$includeModuleDependencies) {
$includeModuleDependencies = $this->getIo()->confirm(
$this->trans('commands.config.export.entity.questions.include-module-dependencies'),
true
);
$input->setOption('include-module-dependencies', $includeModuleDependencies);
}
}
/**
@@ -183,36 +210,49 @@ protected function interact(InputInterface $input, OutputInterface $output) {
protected function execute(InputInterface $input, OutputInterface $output) {
$module = $this->validateModule($input->getOption('module'));
$entityType = $input->getArgument('entity-type');
$bundle = $input->getArgument('bundle');
$bundles = $input->getArgument('bundle');
$optionalConfig = $input->getOption('optional-config');
$removeUuid = $input->getOption('remove-uuid');
$removeHash = $input->getOption('remove-config-hash');
$includeModuleDependencies = $input->getOption('include-module-dependencies');
$bundleDefinition = $this->entityTypeManager->getDefinition($entityType);
$bundleName = $bundleDefinition->getConfigPrefix() . '.' . $bundle;
foreach ($bundles as $bundle) {
$bundleDefinition = $this->entityTypeManager->getDefinition($entityType);
$bundleName = "{$bundleDefinition->getConfigPrefix()}.{$bundle}";
$bundleNameConfig = $this->getConfiguration($bundleName,
$removeUuid, $removeHash);
$bundleNameConfig = $this->getConfiguration($bundleName,
$removeUuid, $removeHash);
if (empty($bundleNameConfig)) {
throw new InvalidOptionException(sprintf('The bundle %s does not exist.',
$bundle));
}
if (empty($bundleNameConfig)) {
throw new InvalidOptionException(sprintf('The bundle %s does not exist.',
$bundle));
}
$this->configExport[$bundleName] = [
'data' => $bundleNameConfig,
'optional' => $optionalConfig,
];
$this->configExport[$bundleName] = [
'data' => $bundleNameConfig,
'optional' => $optionalConfig,
];
$this->getFields($bundle, $optionalConfig, $removeUuid, $removeHash);
$this->getFields($bundle, $optionalConfig, $removeUuid, $removeHash);
$this->getFormDisplays($bundle, $optionalConfig, $removeUuid,
$removeHash);
$this->getFormDisplays($bundle, $optionalConfig, $removeUuid,
$removeHash);
$this->getViewDisplays($bundle, $optionalConfig, $removeUuid,
$removeHash);
$this->getViewDisplays($bundle, $optionalConfig, $removeUuid,
$removeHash);
// Include module dependencies in export files if export is not optional
if ($includeModuleDependencies) {
if ($dependencies = $this->fetchDependencies($bundleNameConfig, 'module')) {
$this->exportModuleDependencies($module, $dependencies);
}
}
$this->exportConfigToModule($module,
$this->trans('commands.config.export.entity.messages.content-type-exported'));
$this->exportConfigToModule($module,
sprintf(
$this->trans('commands.config.export.entity.messages.bundle-exported'),
$bundle
));
}
}
}

0 comments on commit a026e44

Please sign in to comment.
You can’t perform that action at this time.