Skip to content

Commit dbce4a5

Browse files
committed
minor #11022 Improved the Fragment Naming for Collections section (javiereguiluz)
This PR was merged into the 4.2 branch. Discussion ---------- Improved the Fragment Naming for Collections section The current docs about naming collections were poor and incomplete. This improves it, and fixes #6056, thanks to the great comment published by @HeahDude at #6056 (comment) Commits ------- 2ccc77d Improved the Fragment Naming for Collections section
2 parents e00013d + 2ccc77d commit dbce4a5

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

form/form_themes.rst

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,66 @@ Fragment Naming for Collections
307307
...............................
308308

309309
When using a :doc:`collection of forms </form/form_collections>`, the fragment
310-
of each collection item follows the pattern ``_field-name_entry_part``. For
311-
example, if your form field is named ``tasks``, the fragment for each task will
312-
be named ``_tasks_entry`` (``_tasks_entry_row``, ``_tasks_entry_label``,
313-
``_tasks_entry_widget``, ``_tasks_entry_error``)
310+
of each collection item follows a predefined pattern. For example, consider the
311+
following complex example where a ``TaskManagerType`` has a collection of
312+
``TaskListType`` which in turn has a collection of ``TaskType``::
313+
314+
class TaskManagerType extends AbstractType
315+
{
316+
public function buildForm(FormBuilderInterface $builder, array $options = array())
317+
{
318+
// ...
319+
$builder->add('taskLists', CollectionType::class, array(
320+
'entry_type' => TaskListType::class,
321+
'block_name' => 'task_lists',
322+
));
323+
}
324+
}
325+
326+
class TaskListType extends AbstractType
327+
{
328+
public function buildForm(FormBuilderInterface $builder, array $options = array())
329+
{
330+
// ...
331+
$builder->add('tasks', CollectionType::class, array(
332+
'entry_type' => TaskType::class,
333+
));
334+
}
335+
}
336+
337+
class TaskType
338+
{
339+
public function buildForm(FormBuilderInterface $builder, array $options = array())
340+
{
341+
$builder->add('name');
342+
// ...
343+
}
344+
}
345+
346+
Then you get all the following customizable blocks (where ``*`` can be replaced
347+
by ``row``, ``widget``, ``label``, or ``help``):
348+
349+
.. code-block:: twig
350+
351+
{% block _task_manager_task_lists_* %}
352+
{# the collection field of TaskManager #}
353+
{% endblock %}
354+
355+
{% block _task_manager_task_lists_entry_* %}
356+
{# the inner TaskListType #}
357+
{% endblock %}
358+
359+
{% block _task_manager_task_lists_entry_tasks_* %}
360+
{# the collection field of TaskListType #}
361+
{% endblock %}
362+
363+
{% block _task_manager_task_lists_entry_tasks_entry_* %}
364+
{# the inner TaskType #}
365+
{% endblock %}
366+
367+
{% block _task_manager_task_lists_entry_tasks_entry_name_* %}
368+
{# the field of TaskType #}
369+
{% endblock %}
314370
315371
Template Fragment Inheritance
316372
.............................

0 commit comments

Comments
 (0)