Skip to content

Commit

Permalink
added 'all' option for exporting all content types (#4036)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuarez20 authored and enzolutions committed May 14, 2019
1 parent 168b250 commit c2c3efb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config/services/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- { name: drupal.command }
console.config_export_content_type:
class: Drupal\Console\Command\Config\ExportContentTypeCommand
arguments: ['@console.extension_manager', '@entity_type.manager', '@console.validator', '@console.chain_queue']
arguments: ['@console.extension_manager', '@entity_type.manager', '@console.validator', '@console.chain_queue', '@config.storage']
tags:
- { name: drupal.command }
console.config_export_entity:
Expand Down
129 changes: 110 additions & 19 deletions src/Command/Config/ExportContentTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\Console\Command\Config;

use Drupal\Console\Command\Shared\ExportTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -18,9 +19,12 @@
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Extension\Manager;

use Drupal\Core\Config\CachedStorage;

class ExportContentTypeCommand extends Command
{
use ModuleTrait;
use ExportTrait;

/**
* @var Manager
Expand All @@ -42,23 +46,32 @@ class ExportContentTypeCommand extends Command
*/
protected $chainQueue;

/**
* @var CachedStorage
*/
protected $configStorage;

/**
* ExportContentTypeCommand constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
* @param Validator $validator
* @param ChainQueue $chainQueue
* @param CachedStorage $configStorage,
*/
public function __construct(
Manager $extensionManager,
EntityTypeManagerInterface $entityTypeManager,
Validator $validator,
ChainQueue $chainQueue
ChainQueue $chainQueue,
CachedStorage $configStorage

) {
$this->extensionManager = $extensionManager;
$this->entityTypeManager = $entityTypeManager;
$this->validator = $validator;
$this->chainQueue = $chainQueue;
$this->configStorage = $configStorage;
parent::__construct();
}

Expand Down Expand Up @@ -106,18 +119,30 @@ protected function interact(InputInterface $input, OutputInterface $output)

// --content-type argument
$contentType = $input->getArgument('content-type');
if (!$contentType) {

if (!$contentType || $contentType == 'all') {
$bundles_entities = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
$bundles = ['all' => $this->trans('commands.config.export.content.type.questions.all')];
$bundles_ids = [];

foreach ($bundles_entities as $entity) {
$bundles[$entity->id()] = $entity->label();
$bundles_ids[] = $entity->id();
}

$contentType = $this->getIo()->choice(
$this->trans('commands.config.export.content.type.questions.content-type'),
$bundles
);
if (!$contentType) {
$contentType = $this->getIo()->choice(
$this->trans('commands.config.export.content.type.questions.content-type'),
$bundles
);
}
}

if ($contentType == 'all') {
$input->setArgument('content-type', $bundles_ids);
}else{
$input->setArgument('content-type', [$contentType]);
}
$input->setArgument('content-type', $contentType);

$optionalConfig = $input->getOption('optional-config');
if (!$optionalConfig) {
Expand Down Expand Up @@ -149,21 +174,87 @@ protected function interact(InputInterface $input, OutputInterface $output)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$module = $this->validateModule($input->getOption('module'));
$contentType = $input->getArgument('content-type');
$module = $input->getOption('module');
$contentTypes = $input->getArgument('content-type');
$optionalConfig = $input->getOption('optional-config');
$removeUuid = $input->getOption('remove-uuid');
$removeHash = $input->getOption('remove-config-hash');

$this->chainQueue->addCommand(
'config:export:entity', [
'entity-type' => 'node_type',
'bundle' => $contentType,
'--module' => $module,
'--optional-config' => $optionalConfig,
'--remove-uuid' => $removeUuid,
'--remove-config-hash' => $removeHash
]
);
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);
}
}
}
}
}

0 comments on commit c2c3efb

Please sign in to comment.