Skip to content

Commit ad02c31

Browse files
committed
[LiveComponent] Move attributes to the LiveCollectionType to ease customization
1 parent 30d4f9e commit ad02c31

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/LiveComponent/src/Form/Type/LiveCollectionType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public function buildView(FormView $view, FormInterface $form, array $options):
4444
if ($form->getConfig()->hasAttribute('button_add_prototype')) {
4545
$prototype = $form->getConfig()->getAttribute('button_add_prototype');
4646
$view->vars['button_add_prototype'] = $prototype->setParent($form)->createView($view);
47+
48+
$attr = $view->vars['button_add_prototype']->vars['attr'];
49+
$attr['data-action'] ??= 'live#action';
50+
$attr['data-action-name'] ??= sprintf('addCollectionItem(name=%s)', $view->vars['full_name']);
51+
$view->vars['button_add_prototype']->vars['attr'] = $attr;
52+
4753
array_splice($view->vars['button_add_prototype']->vars['block_prefixes'], 1, 0, 'live_collection_button_add');
4854
}
4955
}
@@ -76,6 +82,12 @@ public function finishView(FormView $view, FormInterface $form, array $options):
7682

7783
foreach ($view as $k => $entryView) {
7884
$entryView->vars['button_delete_prototype'] = $prototypes[$k]->createView($entryView);
85+
86+
$attr = $entryView->vars['button_delete_prototype']->vars['attr'];
87+
$attr['data-action'] ??= 'live#action';
88+
$attr['data-action-name'] ??= sprintf('removeCollectionItem(name=%s, index=%s)', $view->vars['full_name'], $k);
89+
$entryView->vars['button_delete_prototype']->vars['attr'] = $attr;
90+
7991
array_splice($entryView->vars['button_delete_prototype']->vars['block_prefixes'], 1, 0, 'live_collection_button_delete');
8092
}
8193
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
{%- block live_collection_widget -%}
22
{{ block('form_widget') }}
3-
{%- if button_add_prototype is defined and not button_add_prototype.rendered -%}
4-
{{ form_row(button_add_prototype, { attr: button_add_prototype.vars.attr|merge({
5-
'data-action': 'live#action',
6-
'data-action-name': 'addCollectionItem(name=' ~ form.vars.full_name ~ ')'
7-
}) }) }}
3+
{%- if skip_add_button|default(false) is same as false and button_add_prototype is defined and not button_add_prototype.rendered -%}
4+
{{ form_row(button_add_prototype) }}
85
{%- endif -%}
96
{%- endblock live_collection_widget -%}
107

118
{%- block live_collection_entry_row -%}
129
{{ block('form_row') }}
1310
{%- if button_delete_prototype is defined and not button_delete_prototype.rendered -%}
14-
{{ form_row(button_delete_prototype, { attr: button_delete_prototype.vars.attr|merge({
15-
'data-action': 'live#action',
16-
'data-action-name': 'removeCollectionItem(name=' ~ form.parent.vars.full_name ~ ', index=' ~ form.vars.name ~ ')'
17-
}) }) }}
11+
{{ form_row(button_delete_prototype) }}
1812
{%- endif -%}
1913
{%- endblock live_collection_entry_row -%}

0 commit comments

Comments
 (0)