Ignacio Calvo
Ignacio Calvo

Reputation: 834

Adding a prefix to a form label for translation

I have some *Type form classes and a forms.html.twig to customize form appearance. By default, in this file the labels are rendered with this block:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }}</label>

I'd like to add a prefix to the label ir order to organize my translations. For example let's say I have a CustomerType, then I'd like my labels to be like:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ 'Customer.' ~ label|trans }}
</label>

I want to be able to pass that 'Customer' string to the FormBuilder in such a way that I am able to use it like:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ prefix ~ '.' ~ label|trans }}
</label>

or maybe:

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{ form.prefix ~ '.' ~ label|trans }
}</label>

Does someone know how to achieve this?

Upvotes: 4

Views: 4393

Answers (4)

Thomas Marchant
Thomas Marchant

Reputation: 237

I found that the given answer didn't work, but I'm not allowed to comment. The reason it didn't work is that the $view->getVar() and $view->setVar() methods don't actually exist. To get this work, I had to make my buildView() method look like this:

public function buildView(Form\FormView $view, Form\FormInterface $form, array $options)
{
    if ($options['label_prefix']) {
        $view->vars['label'] = $options['label_prefix'] . ': ' . $view->vars['label'];
    }
}

then set it to default to null like so:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults([
        'label_prefix' => null,
    ]);
}

Hope this helps!

Upvotes: 0

Abhoryo
Abhoryo

Reputation: 36

With Symfony 2.1

<?php
namespace MyProject\MyBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class FieldTypeExtendedExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->setAttribute('label_prefix', $options['label_prefix']);
    }

    public function buildView(FormViewInterface $view, FormInterface $form, array $options)
    {
        $labelPrefix = $form->getRoot()->hasAttribute('label_prefix') ? $form->getRoot()->getAttribute('label_prefix') : '';
        $view->setVar('label', $labelPrefix.$view->getVar('label'));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'label_prefix' => ''
        ));
    }

    public function getExtendedType()
    {
        return 'form';
    }
}

Declare this extension as a service

<service id="form.type_extension.fieldextended" class="MyProject\MyBundle\Form\Extension\FieldTypeExtendedExtension">
    <tag name="form.type_extension" alias="form" />
</service>

For more informations, see the documentation.

Upvotes: 2

Abhoryo
Abhoryo

Reputation: 21

There is a simple solution: "Type extension"

Create a class like this:

<?php

namespace MyProject\MyBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class FieldTypeExtendedExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->setAttribute('label_prefix', $options['label_prefix']);
    }

    public function buildView(FormView $view, FormInterface $form)
    {
        $labelPrefix = $form->getRoot()->hasAttribute('label_prefix') ? $form->getRoot()->getAttribute('label_prefix') : '';
        $view->set('label', $labelPrefix.$form->getAttribute('label'));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'label_prefix' => '',
        );
    }

    public function getExtendedType()
    {
        return 'field';
    }
}

Declare this extension as a service

<service id="form.type_extension.fieldextended" class="MyProject\MyBundle\Form\Extension\FieldTypeExtendedExtension">
    <tag name="form.type_extension" alias="field" />
</service>

And use this option in your form:

$form = $this->createFormBuilder($entity, array('label_prefix' => 'mylabelprefix.'))
    ->add('link', 'url')
    ->getForm();

You'll have a label "mylabelprefix.link".

Enjoy!

Upvotes: 2

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44831

To do that, just set the label explicitly:

$builder->add('firstName', 'text', array(
    'label' => 'customer.first_name'
));

Upvotes: 2

Related Questions