Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb11a75

Browse files
fracsijaviereguiluz
authored andcommitted
Display as form for actions
1 parent 2af62d7 commit bb11a75

6 files changed

Lines changed: 33 additions & 1 deletion

File tree

assets/css/easyadmin-theme/base.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ a.user-menu-wrapper .user-details:hover {
575575
}
576576
.content-header .page-actions:empty { display: none; }
577577

578-
.content-header .page-actions .btn + .btn {
578+
.content-header .page-actions :is(.btn,form:has(.btn)) + :is(.btn,form:has(.btn)) {
579579
margin-inline-start: 10px;
580580
}
581581

src/Config/Action.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ public function displayAsButton(): self
158158
return $this;
159159
}
160160

161+
public function displayAsForm(): self
162+
{
163+
$this->dto->setHtmlElement('form');
164+
165+
return $this;
166+
}
167+
161168
public function setHtmlAttributes(array $attributes): self
162169
{
163170
$this->dto->setHtmlAttributes($attributes);

src/Dto/ActionDto.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ public function getAsConfigObject(): Action
293293

294294
if ('a' === $this->htmlElement) {
295295
$action->displayAsLink();
296+
} elseif ('form' === $this->htmlElement) {
297+
$action->displayAsForm();
296298
} else {
297299
$action->displayAsButton();
298300
}

templates/crud/action.html.twig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,15 @@
1515
{%- if action.label is not empty -%}<span class="action-label">{{ action.label|trans|raw }}</span>{%- endif -%}
1616
</span>
1717
</button>
18+
{% elseif 'form' == action.htmlElement %}
19+
{%- set form_id = 'form-' ~ action.name ~ '-' ~ random() -%}
20+
{%- do action.setHtmlAttribute('form', form_id) -%}
21+
<form action="{{ action.linkUrl }}" method="POST" id="{{ form_id }}">
22+
<button class="{{ action.cssClass }}" {% for name, value in action.htmlAttributes %}{{ name }}="{{ (value.trans is defined ? value|trans : value)|e('html') }}" {% endfor %}>
23+
<span class="btn-label">
24+
{%- if action.icon %}<twig:ea:Icon name="{{ action.icon }}"/> {% endif -%}
25+
{%- if action.label is not empty -%}<span class="action-label">{{ action.label|trans|raw }}</span>{%- endif -%}
26+
</span>
27+
</button>
28+
</form>
1829
{% endif %}

tests/Controller/ActionsCrudControllerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@ public function testDynamicLabels()
5252
static::assertSame('Action 7: Category 0', $crawler->filter('a.dropdown-item[data-action-name="action7"]')->text());
5353
static::assertSame('Reset', $crawler->filter('a.dropdown-item[data-action-name="action8"]')->text());
5454
}
55+
56+
public function testFormAction()
57+
{
58+
$crawler = $this->client->request('GET', $this->generateIndexUrl());
59+
60+
static::assertCount(1, $crawler->filter('form[id^="form-action9-"]'));
61+
static::assertCount(1, $crawler->filter('form[id^="form-action9-"] > .btn'));
62+
static::assertSame('POST', $crawler->filter('form[id^="form-action9-"]')->attr('method'));
63+
}
5564
}

tests/TestApplication/src/Controller/ActionsCrudController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function configureActions(Actions $actions): Actions
4343
// this tests that the 'Reset' label is interpreted as a string and not as a callable to the PHP reset() function
4444
$action8 = Action::new('action8')->linkToCrudAction('')->setLabel('Reset');
4545

46+
$action9 = Action::new('action9')->linkToCrudAction('')->createAsGlobalAction()->displayAsForm();
47+
4648
return $actions
4749
->add(Crud::PAGE_INDEX, $action1)
4850
->add(Crud::PAGE_INDEX, $action2)
@@ -52,6 +54,7 @@ public function configureActions(Actions $actions): Actions
5254
->add(Crud::PAGE_INDEX, $action6)
5355
->add(Crud::PAGE_INDEX, $action7)
5456
->add(Crud::PAGE_INDEX, $action8)
57+
->add(Crud::PAGE_INDEX, $action9)
5558
->update(Crud::PAGE_INDEX, Action::NEW, function (Action $action) {
5659
return $action->setIcon('fa fa-fw fa-plus')->setLabel(false);
5760
})

0 commit comments

Comments
 (0)