Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4044 config export all option #4045

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/services/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
134 changes: 43 additions & 91 deletions src/Command/Config/ExportContentTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,6 +26,8 @@ class ExportContentTypeCommand extends Command
use ModuleTrait;
use ExportTrait;

const ALL = '-all-';

/**
* @var Manager
*/
Expand Down Expand Up @@ -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',
Expand All @@ -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 = [];
Expand All @@ -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) {
Expand All @@ -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');
Expand All @@ -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(
Expand All @@ -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);
}
}

/**
Expand All @@ -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
]
);
}
}
92 changes: 66 additions & 26 deletions src/Command/Config/ExportEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* @file
* Contains \Drupal\Console\Command\Config\ExportContentTypeCommand.
* Contains \Drupal\Console\Command\Config\ExportEntityCommand.
*/

namespace Drupal\Console\Command\Config;
Expand All @@ -26,6 +26,8 @@ class ExportEntityCommand extends Command {
use ModuleTrait;
use ExportTrait;

const ALL = '-all-';

/**
* @var EntityTypeManagerInterface
*/
Expand Down Expand Up @@ -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',
Expand All @@ -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 = [];
Expand All @@ -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(
Expand All @@ -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);
}
}

/**
Expand All @@ -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
));
}
}
}
Loading