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

Add an opportunity to add multiple view fields at once #3883

Merged
merged 5 commits into from
Apr 10, 2019
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
139 changes: 72 additions & 67 deletions src/Command/Generate/PluginViewsFieldCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@

namespace Drupal\Console\Command\Generate;

use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Generator\PluginViewsFieldGenerator;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Command\Shared\ArrayInputTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Generator\PluginViewsFieldGenerator;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Utils\Site;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class PluginViewsFieldCommand
Expand All @@ -27,8 +28,9 @@
*/
class PluginViewsFieldCommand extends Command
{
use ModuleTrait;
use ArrayInputTrait;
use ConfirmationTrait;
use ModuleTrait;

/**
* @var Manager
Expand Down Expand Up @@ -63,12 +65,12 @@ class PluginViewsFieldCommand extends Command
/**
* PluginViewsFieldCommand constructor.
*
* @param Manager $extensionManager
* @param Manager $extensionManager
* @param PluginViewsFieldGenerator $generator
* @param Site $site
* @param StringConverter $stringConverter
* @param Validator $validator
* @param ChainQueue $chainQueue
* @param Site $site
* @param StringConverter $stringConverter
* @param Validator $validator
* @param ChainQueue $chainQueue
*/
public function __construct(
Manager $extensionManager,
Expand Down Expand Up @@ -100,22 +102,10 @@ protected function configure()
$this->trans('commands.common.options.module')
)
->addOption(
'class',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.plugin.views.field.options.class')
)
->addOption(
'title',
'fields',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.views.field.options.title')
)
->addOption(
'description',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.views.field.options.description')
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.generate.plugin.views.field.options.fields')
)
->setAliases(['gpvf']);
}
Expand All @@ -131,10 +121,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$module = $this->validateModule($input->getOption('module'));
$class_name = $this->validator->validateClassName($input->getOption('class'));
$class_machine_name = $this->stringConverter->camelCaseToUnderscore($class_name);
$title = $input->getOption('title');
$description = $input->getOption('description');
$fields = $input->getOption('fields');
$noInteraction = $input->getOption('no-interaction');

// Parse nested data.
if ($noInteraction) {
$fields = $this->explodeInlineArray($fields);
}

$function = $module . '_views_data';
$viewsFile = $module . '.views.inc';
Expand All @@ -149,10 +142,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->generator->generate([
'module' => $module,
'class_machine_name' => $class_machine_name,
'class_name' => $class_name,
'title' => $title,
'description' => $description,
'fields' => $fields,
]);

$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
Expand All @@ -165,37 +155,52 @@ protected function interact(InputInterface $input, OutputInterface $output)
// --module option
$this->getModuleOption();

// --class option
$class_name = $input->getOption('class');
if (!$class_name) {
$class_name = $this->getIo()->ask(
$this->trans('commands.generate.plugin.views.field.questions.class'),
'CustomViewsField',
function ($class_name) {
return $this->validator->validateClassName($class_name);
// --fields option
$fields = $input->getOption('fields');
if (empty($fields)) {
while (true) {
// --class option
$class_name = $this->getIo()->ask(
$this->trans('commands.generate.plugin.views.field.questions.class'),
'CustomViewsField',
function ($class_name) {
return $this->validator->validateClassName($class_name);
}
);

// --title option
$title = $this->getIo()->ask(
$this->trans('commands.generate.plugin.views.field.questions.title'),
$this->stringConverter->camelCaseToHuman($class_name)
);

// --description option
$description = $this->getIo()->ask(
$this->trans('commands.generate.plugin.views.field.questions.description'),
$this->trans('commands.generate.plugin.views.field.questions.description_default')
);

array_push(
$fields,
[
'title' => $title,
'description' => $description,
'class_name' => $class_name,
'class_machine_name' => $this->stringConverter->camelCaseToUnderscore($class_name),
]
);

if (!$this->getIo()->confirm(
$this->trans('commands.generate.plugin.views.field.questions.field-add'),
true
)
) {
break;
}
);
}
$input->setOption('class', $class_name);

// --title option
$title = $input->getOption('title');
if (!$title) {
$title = $this->getIo()->ask(
$this->trans('commands.generate.plugin.views.field.questions.title'),
$this->stringConverter->camelCaseToHuman($class_name)
);
$input->setOption('title', $title);
}

// --description option
$description = $input->getOption('description');
if (!$description) {
$description = $this->getIo()->ask(
$this->trans('commands.generate.plugin.views.field.questions.description'),
$this->trans('commands.generate.plugin.views.field.questions.description_default')
);
$input->setOption('description', $description);
}
} else {
$fields = $this->explodeInlineArray($fields);
}
$input->setOption('fields', $fields);
}
}
15 changes: 9 additions & 6 deletions src/Generator/PluginViewsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
public function generate(array $parameters)
{
$module = $parameters['module'];
$class_name = $parameters['class_name'];
$fields = $parameters['fields'];

$this->renderFile(
'module/module.views.inc.twig',
Expand All @@ -43,10 +43,13 @@ public function generate(array $parameters)
FILE_APPEND
);

$this->renderFile(
'module/src/Plugin/Views/field/field.php.twig',
$this->extensionManager->getPluginPath($module, 'views/field') . '/' . $class_name . '.php',
$parameters
);
foreach ($fields as $field) {
$field['module'] = $module;
$this->renderFile(
'module/src/Plugin/Views/field/field.php.twig',
$this->extensionManager->getPluginPath($module, 'views/field') . '/' . $field['class_name'] . '.php',
$field
);
}
}
}
12 changes: 6 additions & 6 deletions templates/module/module.views.inc.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ function {{module}}_views_data() {
'#global' => [],
];


$data['views']['{{ class_machine_name }}'] = [
'title' => t('{{ title }}'),
'help' => t('{{ description }}'),
{% for field in fields %}
$data['views']['{{ field.class_machine_name }}'] = [
'title' => t('{{ field.title }}'),
'help' => t('{{ field.description }}'),
'field' => [
'id' => '{{ class_machine_name }}',
'id' => '{{ field.class_machine_name }}',
],
];

{% endfor %}
return $data;
}
{% endblock %}