-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather name it |
||
|
||
/** | ||
* 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']); | ||
} | ||
} |
There was a problem hiding this comment.
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