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;
use Drupal\Console\Generator\FormAlterGenerator;
use Drupal\Console\Command\Shared\ServicesTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
-use Drupal\Console\Command\Shared\MenuTrait;
use Drupal\Console\Command\Shared\FormTrait;
use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Style\DrupalStyle;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
+use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Extension\Manager;
use Drupal\Core\Extension\ModuleHandlerInterface;
class FormAlterCommand extends Command
{
- use ServicesTrait;
- use ModuleTrait;
- use FormTrait;
- use MenuTrait;
use ConfirmationTrait;
- use CommandTrait;
+ use ArrayInputTrait;
+ use FormTrait;
+ use ModuleTrait;
+ use ServicesTrait;
/**
- * @var Manager
-*/
+ * @var Manager
+ */
protected $extensionManager;
/**
- * @var FormAlterGenerator
-*/
+ * @var FormAlterGenerator
+ */
protected $generator;
/**
protected $elementInfoManager;
/**
- * @var Validator
-*/
+ * @var Validator
+ */
protected $validator;
/**
- * @var RouteProviderInterface
-*/
+ * @var RouteProviderInterface
+ */
protected $routeProvider;
/**
ElementInfoManager $elementInfoManager,
Profiler $profiler = null,
$appRoot,
- ChainQueue $chainQueue
+ ChainQueue $chainQueue,
+ Validator $validator
) {
$this->extensionManager = $extensionManager;
$this->generator = $generator;
$this->profiler = $profiler;
$this->appRoot = $appRoot;
$this->chainQueue = $chainQueue;
+ $this->validator = $validator;
parent::__construct();
}
{
$this
->setName('generate:form:alter')
- ->setDescription(
- $this->trans('commands.generate.form.alter.description')
- )
+ ->setDescription($this->trans('commands.generate.form.alter.description'))
->setHelp($this->trans('commands.generate.form.alter.help'))
->addOption(
'module',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.common.options.inputs')
- );
+ )
+ ->setAliases(['gfa']);
}
/**
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
- // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
- if (!$this->confirmGeneration($io)) {
+ // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
+ if (!$this->confirmOperation()) {
return 1;
}
$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(
);
}
- //validate if input is an array
- if (!is_array($inputs[0])) {
- $inputs= $this->explodeInlineArray($inputs);
- }
-
- $this
- ->generator
- ->generate($module, $formId, $inputs, $this->metadata);
+ $this->generator->generate([
+ 'module' => $module,
+ 'form_id' => $formId,
+ 'inputs' => $inputs,
+ 'metadata' => $this->metadata,
+ ]);
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
protected function interact(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
// --module option
- $module = $input->getOption('module');
- if (!$module) {
- // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
- $module = $this->moduleQuestion($io);
- }
-
- $input->setOption('module', $module);
+ $this->getModuleOption();
// --form-id option
$formId = $input->getOption('form-id');
$forms = [];
// Get form ids from webprofiler
if ($this->moduleHandler->moduleExists('webprofiler')) {
- $io->info(
+ $this->getIo()->info(
$this->trans('commands.generate.form.alter.messages.loading-forms')
);
$forms = $this->getWebprofilerForms();
}
if (!empty($forms)) {
- $formId = $io->choiceNoList(
- $this->trans('commands.generate.form.alter.options.form-id'),
+ $formId = $this->getIo()->choiceNoList(
+ $this->trans('commands.generate.form.alter.questions.form-id'),
array_keys($forms)
);
}
$formItems = array_keys($forms[$formId]['form']);
- $formItemsToHide = $io->choice(
+ $formItemsToHide = $this->getIo()->choice(
$this->trans('commands.generate.form.alter.messages.hide-form-elements'),
$formItems,
null,
$inputs = $input->getOption('inputs');
if (empty($inputs)) {
- $io->writeln($this->trans('commands.generate.form.alter.messages.inputs'));
- $inputs = $this->formQuestion($io);
+ $this->getIo()->writeln($this->trans('commands.generate.form.alter.messages.inputs'));
+ $inputs = $this->formQuestion();
} else {
$inputs= $this->explodeInlineArray($inputs);
}
$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;
- }
-
- protected function createGenerator()
- {
- return new FormAlterGenerator();
- }
-
public function getWebprofilerForms()
{
$tokens = $this->profiler->find(null, null, 1000, null, '', '');