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 form in plugin block #155

Merged
merged 9 commits into from
Jul 14, 2014
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
42 changes: 23 additions & 19 deletions Tests/Command/GeneratorPluginBlockCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class GeneratorPluginBlockCommandTest extends GenerateCommandTest
*/
public function testInteractiveCommand($options, $expected, $input)
{
list($module, $name, $services) = $expected;
list($module, $class_name, $plugin_label, $plugin_id, $services, $inputs) = $expected;

$generator = $this->getGenerator();

$generator
->expects($this->once())
->method('generate')
->with($module,$name,$services)
->with($module, $class_name, $plugin_label, $plugin_id, $services, $inputs)
;

$command = $this->getCommand($generator,$input);
Expand All @@ -32,37 +32,42 @@ public function testInteractiveCommand($options, $expected, $input)

public function getDataInteractive()
{
$service ['twig'] = [
'name'=>'twig',
'machine_name'=>'twig',
'class'=>'Twig_Environment',
'short'=>'Twig_Environment',
];

$inputs = [
['name'=>'text_field', 'type'=>'textfield', 'label'=>'Text Field']
];

return[
// case base
[
[],
['Foo', 'FooBlock', 'Foo label', 'foo_id',[]],
"Foo\nFooBlock\nFoo label\nfoo_id\nno\n"
['Foo', 'FooBlock', 'Foo label', 'foo_id',null, []],
"Foo\nFooBlock\nFoo label\nfoo_id\nno\nno"
],
//case two services
[
[],
['Foo','FooBlock', 'Foo label', 'foo_id',['twig'=>['name'=>'twig','machine_name'=>'twig','class'=>'Twig_Environment','short'=>'Twig_Environment']]],
"Foo\nFooBlock\nFoo label\nfoo_id\nyes\ntwig\n"
['Foo','FooBlock', 'Foo label', 'foo_id', $service, []],
"Foo\nFooBlock\nFoo label\nfoo_id\nyes\nyes\ntwig\n\nno\n"
],
// case three module name in arguments
// case three inputs
[
['--module'=>'Foo'],
['Foo','FooBlock', 'Foo label', 'foo_id',['twig'=>['name'=>'twig','machine_name'=>'twig','class'=>'Twig_Environment','short'=>'Twig_Environment']]],
"FooBlock\nFoo label\nfoo_id\nyes\ntwig\n"
['Foo','FooBlock', 'Foo label', 'foo_id',null, $inputs],
"FooBlock\nFoo label\nfoo_id\nno\nyes\nText Field"
],
//case four default values and not services
//case four services and inputs
[
['--module'=>'Foo'],
['Foo','DefaultBlock', 'Foo label', 'foo_id',[]],
"\nFoo label\nfoo_id\nno\n"
['Foo','FooBlock', 'Foo label', 'foo_id',$service, $inputs],
"FooBlock\nFoo label\nfoo_id\nyes\ntwig\n\nyes\nText Field"
],
// case five default values and clean services
[
['--module'=>'Foo'],
['Foo','DefaultBlock', 'Foo label', 'foo_id', []],
"\nFoo label\nfoo_id\nyes\n\n"
]
];
}

Expand Down Expand Up @@ -99,5 +104,4 @@ private function getGenerator()
->getMock()
;
}

}
18 changes: 17 additions & 1 deletion Tests/Generator/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@

abstract class GeneratorTest extends \PHPUnit_Framework_TestCase
{
var $dir;

public function setUp()
{
$this->setUpTemporalDirectory();
}

public function setUpTemporalDirectory()
{
$this->dir = sys_get_temp_dir() . "/module";

$this->filesystem = new Filesystem();
$this->filesystem->remove($this->dir);
}
Expand All @@ -22,4 +29,13 @@ public function tearDown()
{
$this->filesystem->remove($this->dir);
}

public function getSkeletonDirs()
{
$skeletonDirs = [];
$skeletonDirs[] = __DIR__.'/../../src/Resources/skeleton';
$skeletonDirs[] = __DIR__.'/../../src/Resources';

return $skeletonDirs;
}
}
109 changes: 109 additions & 0 deletions Tests/Generator/PluginBlockGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* @file
* Contains Drupal\AppConsole\Test\Generator\PluginBlockGeneratorTest.
*/

namespace Drupal\AppConsole\Test\Generator;

use Drupal\AppConsole\Generator\PluginBlockGenerator;

class PluginBlockGeneratorTest extends GeneratorTest
{
/**
* @dataProvider commandData
*/
public function testGeneratePluginBlock($parameters)
{
$this->setUpTemporalDirectory();

list($module, $class_name, $plugin_label, $plugin_id, $services, $inputs) = $parameters;
$this->getGenerator()->generate($module, $class_name, $plugin_label, $plugin_id, $services, $inputs);

$this->assertTrue(
file_exists($this->dir . '/src/Plugin/Block/' . $class_name .'.php'),
sprintf('%s has been generated', $this->dir . '/src/Plugin/Block/'.$class_name.'.php')
);

$contains = [
'build',
'@Block',
'id',
'admin_label',
$class_name . ' extends BlockBase',
];

if ($inputs) {
$contains += [
'blockForm()',
'blockForm($form, &$form_state)',
'blockSubmit()',
'blockSubmit($form, &$form_state)',
];
}

if ($services) {
$contains += [
'__construct()',
'create()',
'create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)',
'implements ContainerFactoryPluginInterface',
];
}

$content = file_get_contents($this->dir . '/src/Plugin/Block/' . $class_name .'.php');
foreach ($contains as $contain) {
$this->assertContains($contain, $content);
}
}

public function commandData()
{
$services = [
'twig' => [
'name' => 'twig',
'machine_name' => 'twig',
'class' => 'Twig_Environment',
'short'=>'Twig_Environment',
]
];

$inputs = [
[
'name' => 'foo',
'type' => 'textfield',
'label' => 'Foo',
]
];

return [
[
['foo', 'FooBlock', 'Foo Block', 'foo_block', null, []]
],
[
['foo', 'FooBlock', 'Foo Block', 'foo_block', $services, []]
],
[
['foo', 'FooBlock', 'Foo Block', 'foo_block', null, $inputs]
],
[
['foo', 'FooBlock', 'Foo Block', 'foo_block', $services, $inputs]
],
];
}

protected function getGenerator()
{
$generator = $this->getMockBuilder('\Drupal\AppConsole\Generator\PluginBlockGenerator')
->setMethods(['getPluginPath'])
->getMock();

$generator->setSkeletonDirs($this->getSkeletonDirs());

$generator->expects($this->once())
->method('getPluginPath')
->will($this->returnValue($this->dir . '/src/Plugin/Block'));

return $generator;
}
}
19 changes: 17 additions & 2 deletions src/Command/GeneratorPluginBlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
use Drupal\AppConsole\Generator\PluginBlockGenerator;
use Drupal\AppConsole\Command\Helper\ServicesTrait;
use Drupal\AppConsole\Command\Helper\ModuleTrait;
use Drupal\AppConsole\Command\Helper\FormTrait;

class GeneratorPluginBlockCommand extends GeneratorCommand
{
use ServicesTrait;
use ModuleTrait;
use FormTrait;

protected function configure()
{
Expand All @@ -26,6 +28,7 @@ protected function configure()
new InputOption('class-name','',InputOption::VALUE_OPTIONAL, 'Plugin block class'),
new InputOption('plugin-label','',InputOption::VALUE_OPTIONAL, 'Plugin Label'),
new InputOption('plugin-id','',InputOption::VALUE_OPTIONAL, 'Plugin id'),
new InputOption('plugin-form','',InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Plugin id'),
new InputOption('services','',InputOption::VALUE_OPTIONAL, 'Load services'),
))
->setDescription('Generate plugin block')
Expand All @@ -52,13 +55,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$plugin_label = $input->getOption('plugin-label');
$plugin_id = $input->getOption('plugin-id');
$services = $input->getOption('services');
$inputs = $input->getOption('plugin-form');

// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::buildServices
$build_services = $this->buildServices($services);

$this
->getGenerator()
->generate($module, $class_name, $plugin_label, $plugin_id, $build_services)
->generate($module, $class_name, $plugin_label, $plugin_id, $build_services, $inputs)
;
}

Expand Down Expand Up @@ -111,9 +115,20 @@ protected function interact(InputInterface $input, OutputInterface $output)
$input->setOption('plugin-id', $plugin_id);

// --services option
// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::servicesQuestion
// @see Drupal\AppConsole\Command\Helper\ServicesTrait::servicesQuestion
$services_collection = $this->servicesQuestion($output, $dialog);
$input->setOption('services', $services_collection);

$output->writeln([
'',
'You can add some input fields to create special configurations in each block',
'This is optional, press <info>enter</info> to <info>continue</info>',
''
]);

// @see Drupal\AppConsole\Command\Helper\FormTrait::formQuestion
$form = $this->formQuestion($output, $dialog);
$input->setOption('plugin-form', $form);
}

protected function createGenerator()
Expand Down
12 changes: 6 additions & 6 deletions src/Generator/PluginBlockGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class PluginBlockGenerator extends Generator
* @param $plugin_id
* @param $services
*/
public function generate($module, $class_name, $plugin_label, $plugin_id, $services)
public function generate($module, $class_name, $plugin_label, $plugin_id, $services, $inputs)
{

$parameters = [
'module' => $module,
'class_name' => $class_name,
'module' => $module,
'class_name' => $class_name,
'plugin_label' => $plugin_label,
'plugin_id' => $plugin_id,
'services' => $services,
'plugin_id' => $plugin_id,
'services' => $services,
'inputs' => $inputs,
];

$this->renderFile(
Expand Down
30 changes: 29 additions & 1 deletion src/Resources/skeleton/module/src/Plugin/Block/block.php.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
*@file
* @file
* Contains \Drupal\{{module}}\Plugin\Block\{{class_name}}.php
*/

Expand Down Expand Up @@ -66,4 +66,32 @@ class {{class_name}} extends BlockBase {% if services is not empty %}implements
'#markup' => '{{plugin_label}}',
];
}

{% if inputs %}
/**
* Overrides \Drupal\block\BlockBase::blockForm().
*/
public function blockForm($form, &$form_state)
{
{% for input in inputs %}
$form['{{ input.name }}'] = [
'#type' => '{{ input.type }}',
'#title' => $this->t('{{ input.label }}'),
'#description' => $this->t(''),
'#default_value' => $this->configuration['{{ input.name }}'],
];
{% endfor %}
return $form;
}

/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state)
{
{% for input in inputs %}
$this->configuration['{{ input.name }}'] = $form_state['values']['{{ input.name }}'];
{% endfor %}
}
{% endif %}
}