Symfony allows you to easily customize, extend, or create your own form type widgets. This works great if you want to name your new form type 'foo'. However, if you want to name your custom type with two words like fooBar, how would you go about naming the PHP formType, the Service, and the Twig? There are plenty of examples of symfony core form types that are multiple words separated by underscore, e.g. collection_item_widget. But that doesn't work for custom form types.
So if you want to make a new form type called fooBarType, you would create a controller called fooBarType.php
class FooBarType extends AbstractType {
public function getName() {
return 'fooBar';
}
public function getParent() {
return 'form';
}
}
Set this as a service (optional):
cms.form.type.foo_bar:
class: Cms\FooBundle\Form\Type\FooBar
tags:
- { name: form.type, alias: fooBar }
Then customize the fields.html.twig and create a block for that named foo_bar_widget or foobar_widget or fooBar_widget (none of these work):
{% block foo_bar_widget %}
{{ block('entity_widget') }}
{% endblock %}
NOTE: If this is in a different location, you also have to register this in config.yml
twig:
form:
resources:
- 'CmsFooBundle:Form:fields.html.twig'
Then use that field in a form builder, e.g.
$builder->add('myField', 'fooBar');
Everything works above, except the custom block in the fields.html.twig does not render, unless I change all the names to something simple like 'foo'.
¿Por Que?
