Skip to content

Instantly share code, notes, and snippets.

@franek
Created October 3, 2013 15:27
Show Gist options
  • Save franek/6811721 to your computer and use it in GitHub Desktop.
Save franek/6811721 to your computer and use it in GitHub Desktop.
A Symfony form extension to generate label (see http://www.elao.com/blog/non-classe/a-nice-way-of-handing-form-label-translation.html for another solution)
<?php
namespace Your\Namespace\Forms\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
/**
* Class FormatLabelTypeExtension is used to format all Form label by default.
*
* It uses <form1>.<field name>.label
* Or If there are child :
*
*
*/
class FormatLabelTypeExtension extends AbstractTypeExtension
{
/**
*
* @param FormView $view
* @param FormInterface $form
* @param array $options
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
// Generate a label only for field which do not have a label already set
if (is_null($view->vars['label']) === true) {
// Not very nice code
// Get all parents form name to generate the prefix
$parent = $form->getParent();
$formNames = array();
while ($parent !== null) {
$name = $parent->getName();
$formNames[] = $name;
$parent = $parent->getParent();
}
// Need to reverse order
$prefix = implode(array_reverse($formNames), '.');
$formName = $form->getName();
if ($form->getParent() instanceof FormInterface) {
// If parent form is a CollectionType, replace integer value by entry
$formType = $form->getParent()->getConfig()->getType()->getInnerType();
if ($formType instanceof CollectionType) {
$formName = 'entry';
}
}
$view->vars['label'] = sprintf('%s.%s.label', $prefix, $formName);
}
}
public function getExtendedType()
{
return 'form';
}
}
<?php
namespace Your\Namespace\Tests\Forms\Extension;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormFactoryInterface;
use Your\Namespace\Forms\Extension\FormatLabelTypeExtension;
class FormatLabelTypeExtensionTest extends \PHPUnit_Framework_TestCase
{
private $factory;
protected function setUp()
{
/** @var FormFactoryInterface factory */
$this->factory = Forms::createFormFactoryBuilder()
->addTypeExtension(new FormatLabelTypeExtension())
->getFormFactory();
}
public function testLabelIsPrefixedByRootFormNameSuffixedByLabel()
{
$parent = $this->factory->createNamed('my_name_form', 'form');
$parent->add($this->factory->createNamed('field1', 'text', null, array(
'auto_initialize' => false
)));
$field = $parent->get('field1');
$this->assertEquals(
'my_name_form.field1.label',
$field->createView()->vars['label']
);
}
public function testFormWithChild()
{
$parent = $this->factory->createNamed('my_name_form', 'form');
$parent->add($this->factory->createNamed('child1', 'form', null, array(
'auto_initialize' => false
)));
$child1 = $parent->get('child1');
$child1->add($this->factory->createNamed('field1', 'text', null, array(
'auto_initialize' => false
)));
$field = $child1->get('field1');
$this->assertEquals(
'my_name_form.child1.field1.label',
$field->createView()->vars['label']
);
}
/**
* Label for collection type replace integer value by entry
* Instead of my_name_form.fieldList.0.label or my_name_form.fieldList.1.label
* get my_name_form.fieldList.entry.label
*/
public function testFormWithCollection()
{
$parent = $this->factory->createNamed('my_name_form', 'form');
$parent->add(
$this->factory->createNamed(
'fieldList',
'collection',
array('a', 'b'),
array(
'type' => 'text',
'auto_initialize' => false
)
)
);
$field = $parent->get('fieldList');
$this->assertEquals(
'my_name_form.fieldList.entry.label',
$field->createView()->children[0]->vars['label']
);
$this->assertEquals(
'my_name_form.fieldList.entry.label',
$field->createView()->children[1]->vars['label']
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment