Skip to content

Commit

Permalink
[console] Add new option for generate ajax commmand. (#3755)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel303 authored and jmolivas committed Feb 1, 2018
1 parent dfbca67 commit 7273643
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/Command/Generate/AjaxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.ajax.command.options.method')
)
->addOption(
'js-name',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.ajax.command.options.js-name')
)
->setAliases(['gac']);
}

Expand All @@ -114,12 +120,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$module = $input->getOption('module');
$class = $this->validator->validateClassName($input->getOption('class'));
$method = $input->getOption('method');
$js_name = $input->getOption('js-name');

$this->generator->generate(
[
'module' => $module,
'class_name' => $class,
'method' => $method,
'js_name' => $js_name,
]
);

Expand Down Expand Up @@ -159,5 +167,15 @@ function ($class) {
);
$input->setOption('method', $method);
}

// --js-name option
$js_name = $input->getOption('js-name');
if (!$js_name) {
$js_name = $this->getIo()->ask(
$this->trans('commands.generate.ajax.command.questions.js-name'),
'script'
);
$input->setOption('js-name', $js_name);
}
}
}
3 changes: 2 additions & 1 deletion src/Generator/AjaxCommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function generate(array $parameters)
{
$class = $parameters['class_name'];
$module = $parameters['module'];
$js_name = $parameters['js_name'];

$moduleInstance = $this->extensionManager->getModule($module);
$moduleDir = $moduleInstance->getPath();
Expand All @@ -51,7 +52,7 @@ public function generate(array $parameters)

$this->renderFile(
'module/js/commands.php.twig',
$moduleDir . '/js/custom.js',
$moduleDir . '/js/' .$js_name. '.js',
$parameters
);

Expand Down
4 changes: 2 additions & 2 deletions templates/module/module-libraries.yml.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ module }}.commands:
{{ module }}-library:
js:
js/custom.js: {}
js/{{ js_name }}.js: {}
dependencies:
- core/drupal.ajax

0 comments on commit 7273643

Please sign in to comment.