Skip to content

[Form] Automated label generation for forms #11456

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

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
->info('form configuration')
->canBeEnabled()
->children()
->scalarNode('auto_label')->end()
->arrayNode('csrf_protection')
->treatFalseLike(array('enabled' => false))
->treatTrueLike(array('enabled' => true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ private function registerFormConfiguration($config, ContainerBuilder $container,
$config['form']['csrf_protection']['enabled'] = $config['csrf_protection']['enabled'];
}

if (array_key_exists('auto_label', $config['form'])) {
$loader->load('form_auto_label.xml');
$container->setParameter('form.type_extension.auto_label.auto_label', $config['form']['auto_label']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the parameter name looks weird with auto_label being duplicated. form.type_extension.auto_label.format might be better

}

if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
$loader->load('form_csrf.xml');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="form.type_extension.auto_label" class="Symfony\Component\Form\Extension\AutoLabel\Type\AutoLabelTypeExtension">
<tag name="form.type_extension" alias="form" />
<argument>%form.type_extension.auto_label.auto_label%</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\AutoLabel;

use Symfony\Component\Form\AbstractExtension;

/**
* Extension for automated label generation.
*
* @since 2.7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more likely 2.6, given that it is not yet in feature freeze

* @author Alexandre Salomé <alexandre.salome@gmail.com>
*/
class AutoLabelExtension extends AbstractExtension
{
/**
* @var string
*/
private $autoLabel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather name it format, or something like that


/**
* Constructs a new form extension.
*
* The argument "autoLabel" can have placeholders:
*
* - %type% : the form type name (ex: text, choice, date)
* - %name% : the name of the form (ex: firstname)
* - %fullname%: the full name of the form (ex: user_firstname)
*
* @param string $autoLabel a default label for forms
*/
public function __construct($autoLabel)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this have a default value matching the default of AutoLabelTypeExtension ?

{
$this->autoLabel = $autoLabel;
}

/**
* {@inheritdoc}
*/
protected function loadTypeExtensions()
{
return array(
new Type\AutoLabelTypeExtension($this->autoLabel)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\AutoLabel\Type;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* Extension for automated label generation.
*
* @since 2.7
* @author Alexandre Salomé <alexandre.salome@gmail.com>
*/
class AutoLabelTypeExtension extends AbstractTypeExtension
{
/**
* @var string
*/
private $autoLabel;

/**
* Constructs a new type extension.
*
* The argument "autoLabel" can have placeholders:
*
* - %type% : the form type name (ex: text, choice, date)
* - %name% : the name of the form (ex: firstname)
* - %fullname%: the full name of the form (ex: user_firstname)
*
* @param string $autoLabel a default label for forms
*/
public function __construct($autoLabel = 'form.%type%.label.%name%')
{
$this->autoLabel = $autoLabel;
}

/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return 'form';
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (null === $options['label']) {
$autoLabel = $options['auto_label'];
$parent = $form->getParent();
$fullname = $form->getName();

while ($parent) {
if (null === $autoLabel) {
$autoLabel = $parent->getConfig()->getOption('auto_label');
}
$fullname = $parent->getName().'_'.$fullname;
$parent = $parent->getParent();
}

if (null === $autoLabel) {
$autoLabel = $this->autoLabel;
}


if (null !== $autoLabel) {
$view->vars['label'] = strtr($autoLabel, array(
'%name%' => $form->getName(),
'%fullname%' => $fullname,
'%type%' => $form->getConfig()->getType()->getName(),
));
}
}
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'auto_label' => null
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\AutoLabel;

use Symfony\Component\Form\Extension\AutoLabel\AutoLabelExtension;

/**
* @covers Symfony\Component\Form\Extension\AutoLabel\AutoLabelExtension
*/
class AutoLabelExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var AutoLabelExtension
*/
private $extension;

public function setUp()
{
$this->extension = new AutoLabelExtension('foo');
}

public function testLoadTypeExtensions()
{
$typeExtensions = $this->extension->getTypeExtensions('form');

$this->assertInternalType('array', $typeExtensions);
$this->assertCount(1, $typeExtensions);
$this->assertInstanceOf('Symfony\Component\Form\Extension\AutoLabel\Type\AutoLabelTypeExtension', array_shift($typeExtensions));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\AutoLabel\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Extension\AutoLabel\AutoLabelExtension;

class AutoLabelTypeExtensionTest_ChildType extends AbstractType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type is not used anywhere

{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
}

public function getName()
{
return 'auto_label_test';
}
}

class AutoLabelTypeExtensionTest extends TypeTestCase
{
protected function getExtensions()
{
return array_merge(parent::getExtensions(), array(
new AutoLabelExtension('%fullname%.%name%.%type%'),
));
}

public function testDefaultLabelGeneration()
{
$view = $this->factory->createNamed('firstname', 'text')->createView();
$this->assertEquals('firstname.firstname.text', $view->vars['label']);
}

public function testFullnameGeneration()
{
$view = $this->factory
->createBuilder('form')
->add('form', 'form', array('auto_label' => '%fullname%'))
->getForm()
->createView()
;

$this->assertEquals('form_form', $view['form']->vars['label']);
}

public function testForcedGeneration()
{
$view = $this->factory->createNamed('firstname', 'text', null, array('auto_label' => '%fullname%'))->createView();
$this->assertEquals('firstname', $view->vars['label']);
}

public function testOverridenLabelGeneration()
{
$view = $this->factory->createNamed('firstname', 'text', null, array('auto_label' => 'foo.%name%'))->createView();
$this->assertEquals('foo.firstname', $view->vars['label']);
}

public function testInheritedOption()
{
$builder = $this->factory->createNamedBuilder('user', 'form', null, array(
'auto_label' => '%type%.%fullname%'
));

$view = $builder->add('firstname', 'text')->getForm()->createView();
$this->assertEquals('text.user_firstname', $view['firstname']->vars['label']);
}
}