Skip to content

Commit

Permalink
3515 form trait input multi values (#3734)
Browse files Browse the repository at this point in the history
* Add support of nested data, add checks in the template for data existance, small refactoring

* Add suport of nested data for FormCommand, PluginCommand classes. Update the form field twig templates, add support of more options and check the options are defined

* Update twig template
  • Loading branch information
LOBsTerr authored and jmolivas committed Jan 23, 2018
1 parent bac0de0 commit 52c9638
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 61 deletions.
49 changes: 15 additions & 34 deletions src/Command/Generate/FormAlterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\Console\Command\Generate;

use Drupal\Console\Command\Shared\ArrayInputTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -27,19 +28,20 @@

class FormAlterCommand extends Command
{
use ServicesTrait;
use ModuleTrait;
use FormTrait;
use ConfirmationTrait;
use ArrayInputTrait;
use FormTrait;
use ModuleTrait;
use ServicesTrait;

/**
* @var Manager
*/
* @var Manager
*/
protected $extensionManager;

/**
* @var FormAlterGenerator
*/
* @var FormAlterGenerator
*/
protected $generator;

/**
Expand Down Expand Up @@ -165,8 +167,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$module = $input->getOption('module');
$formId = $input->getOption('form-id');
$inputs = $input->getOption('inputs');
$noInteraction = $input->getOption('no-interaction');
// Parse nested data.
if ($noInteraction) {
$inputs = $this->explodeInlineArray($inputs);
}

$function = $module . '_form_' .$formId . '_alter';
$function = $module . '_form_' . $formId . '_alter';

if ($this->extensionManager->validateModuleFunctionExist($module, $function)) {
throw new \Exception(
Expand All @@ -177,11 +184,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
}

//validate if input is an array
if (!is_array($inputs[0])) {
$inputs= $this->explodeInlineArray($inputs);
}

$this
->generator
->generate($module, $formId, $inputs, $this->metadata);
Expand Down Expand Up @@ -263,27 +265,6 @@ protected function interact(InputInterface $input, OutputInterface $output)
$input->setOption('inputs', $inputs);
}

/**
* @{@inheritdoc}
*/
public function explodeInlineArray($inlineInputs)
{
$inputs = [];
foreach ($inlineInputs as $inlineInput) {
$explodeInput = explode(" ", $inlineInput);
$parameters = [];
foreach ($explodeInput as $inlineParameter) {
list($key, $value) = explode(":", $inlineParameter);
if (!empty($value)) {
$parameters[$key] = $value;
}
}
$inputs[] = $parameters;
}

return $inputs;
}

public function getWebprofilerForms()
{
$tokens = $this->profiler->find(null, null, 1000, null, '', '');
Expand Down
30 changes: 20 additions & 10 deletions src/Command/Generate/FormCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\Console\Command\Generate;

use Drupal\Console\Command\Shared\ArrayInputTrait;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -25,27 +26,28 @@

abstract class FormCommand extends ContainerAwareCommand
{
use ModuleTrait;
use ServicesTrait;
use ArrayInputTrait;
use FormTrait;
use MenuTrait;
use ModuleTrait;
use ServicesTrait;

private $formType;
private $commandName;

/**
* @var Manager
*/
* @var Manager
*/
protected $extensionManager;

/**
* @var FormGenerator
*/
* @var FormGenerator
*/
protected $generator;

/**
* @var ChainQueue
*/
* @var ChainQueue
*/
protected $chainQueue;

/**
Expand Down Expand Up @@ -159,7 +161,7 @@ protected function configure()
->addOption(
'inputs',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.common.options.inputs')
)
->addOption(
Expand Down Expand Up @@ -210,9 +212,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$menu_parent = $input->getOption('menu-parent');
$menu_link_title = $input->getOption('menu-link-title');
$menu_link_desc = $input->getOption('menu-link-desc');
$inputs = $input->getOption('inputs');
$noInteraction = $input->getOption('no-interaction');
// Parse nested data.
if ($noInteraction) {
$inputs = $this->explodeInlineArray($inputs);
}

// if exist form generate config file
$inputs = $input->getOption('inputs');
$build_services = $this->buildServices($services);

$this
Expand Down Expand Up @@ -275,7 +282,10 @@ function ($className) {
// @see \Drupal\Console\Command\Shared\FormTrait::formQuestion
$inputs = $this->formQuestion();
$input->setOption('inputs', $inputs);
} else {
$inputs= $this->explodeInlineArray($inputs);
}
$input->setOption('inputs', $inputs);

$path = $input->getOption('path');
if (!$path) {
Expand Down
18 changes: 15 additions & 3 deletions src/Command/Generate/PluginBlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$services = $input->getOption('services');
$theme_region = $input->getOption('theme-region');
$inputs = $input->getOption('inputs');
$noInteraction = $input->getOption('no-interaction');
// Parse nested data.
if ($noInteraction) {
$inputs = $this->explodeInlineArray($inputs);
}

$theme = $this->configFactory->get('system.theme')->get('default');
$themeRegions = \system_region_list($theme, REGIONS_VISIBLE);
Expand Down Expand Up @@ -261,7 +266,7 @@ function ($class) {
// --theme-region option
$themeRegion = $input->getOption('theme-region');
if (!$themeRegion) {
$themeRegion = $this->getIo()->choiceNoList(
$themeRegion = $this->getIo()->choiceNoList(
$this->trans('commands.generate.plugin.block.questions.theme-region'),
array_values($themeRegions),
'',
Expand All @@ -278,8 +283,15 @@ function ($class) {

$output->writeln($this->trans('commands.generate.plugin.block.messages.inputs'));

// @see Drupal\Console\Command\Shared\FormTrait::formQuestion
$inputs = $this->formQuestion();
// --inputs option
$inputs = $input->getOption('inputs');
if (!$inputs) {
// @see \Drupal\Console\Command\Shared\FormTrait::formQuestion
$inputs = $this->formQuestion();
$input->setOption('inputs', $inputs);
} else {
$inputs = $this->explodeInlineArray($inputs);
}
$input->setOption('inputs', $inputs);
}
}
6 changes: 3 additions & 3 deletions templates/module/src/Form/form-alter.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ function {{ module }}_form_alter(&$form, FormStateInterface $form_state, $form_i
$form['{{ input.name }}'] = [
'#type' => '{{ input.type }}',
'#title' => t('{{ input.label|e }}'),
{%- if input.description is defined -%}
{%- if input.description is defined and input.description is defined -%}
'#description' => t('{{ input.description|e }}'),
{% endif %}
{%- if input.options is defined and input.options|length -%}
'#options' => {{ input.options }},
{% endif %}
{%- if input.maxlength|length -%}
{%- if input.maxlength is defined and input.maxlength|length -%}
'#maxlength' => {{ input.maxlength }},
{% endif %}
{%- if input.size|length -%}
{%- if input.size is defined and input.size|length -%}
'#size' => {{ input.size }},
{% endif %}
{%- if input.default_value is defined and input.default_value|length -%}
Expand Down
2 changes: 1 addition & 1 deletion templates/module/src/Form/form-config.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class {{ class_name }} extends ConfigFormBase {% endblock %}
{% endif %}
'#type' => '{{ input.type }}',
'#title' => $this->t('{{ input.label|e }}'),
{% if input.description is defined and input.description is not empty %}
{% if input.description is defined and input.description|length %}
'#description' => $this->t('{{ input.description|e }}'),
{% endif %}
{% if input.options is defined and input.options is not empty %}
Expand Down
16 changes: 11 additions & 5 deletions templates/module/src/Form/form.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,30 @@ class {{ class_name }} extends FormBase {% endblock %}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
{% for input in inputs %}
{% if input.fieldset|length %}
{% if input.fieldset is defined and input.fieldset|length %}
$form['{{ input.fieldset }}']['{{ input.name }}'] = [
{% else %}
$form['{{ input.name }}'] = [
{% endif %}
'#type' => '{{ input.type }}',
'#title' => $this->t('{{ input.label|e }}'),
{% if input.description|length %}
{% if input.description is defined and input.description|length %}
'#description' => $this->t('{{ input.description|e }}'),
{% endif %}
{% if input.options|length %}
{% if input.options is defined and input.options|length %}
'#options' => {{ input.options }},
{% endif %}
{% if input.maxlength|length %}
{% if input.maxlength is defined and input.maxlength|length %}
'#maxlength' => {{ input.maxlength }},
{% endif %}
{% if input.size|length %}
{% if input.size is defined and input.size|length %}
'#size' => {{ input.size }},
{% endif %}
{% if input.default_value is defined and input.default_value|length %}
'#default_value' => {{ input.default_value }},
{% endif %}
{% if input.weight is defined and input.weight|length %}
'#weight' => '{{ input.weight }}',
{% endif %}
];
{% endfor %}
Expand Down
12 changes: 7 additions & 5 deletions templates/module/src/Plugin/Block/block.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,20 @@ class {{class_name}} extends BlockBase {% if services is not empty %}implements
$form['{{ input.name }}'] = [
'#type' => '{{ input.type }}',
'#title' => $this->t('{{ input.label|escape }}'),
'#description' => $this->t('{{ input.description|escape }}'),
{% if input.options|length %}
{% if input.description is defined and input.description is not empty %}
'#description' => $this->t('{{ input.description|e }}'),
{% endif %}
{% if input.options is defined and input.options|length %}
'#options' => {{ input.options }},
{% endif %}
'#default_value' => $this->configuration['{{ input.name }}'],
{% if input.maxlength|length %}
{% if input.maxlength is defined and input.maxlength|length %}
'#maxlength' => {{ input.maxlength }},
{% endif %}
{% if input.size|length %}
{% if input.size is defined and input.size|length %}
'#size' => {{ input.size }},
{% endif %}
{% if input.weight|length %}
{% if input.weight is defined and input.weight|length %}
'#weight' => '{{ input.weight }}',
{% endif %}
];
Expand Down

0 comments on commit 52c9638

Please sign in to comment.