Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow setting custom attributes on crud #6232

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Config/Crud.php
Expand Up @@ -430,4 +430,23 @@ private function getValidPageNames(): array
{
return [self::PAGE_DETAIL, self::PAGE_EDIT, self::PAGE_INDEX, self::PAGE_NEW];
}

public function getHtmlAttributes(): array
{
return $this->dto->getHtmlAttributes();
}

public function setHtmlAttributes(array $htmlAttributes): self
{
$this->dto->setHtmlAttributes($htmlAttributes);

return $this;
}

public function setHtmlAttribute(string $attribute, mixed $value): self
{
$this->dto->setHtmlAttribute($attribute, $value);

return $this;
}
}
16 changes: 16 additions & 0 deletions src/Dto/CrudDto.php
Expand Up @@ -65,6 +65,7 @@ final class CrudDto
private ?string $contentWidth = null;
private ?string $sidebarWidth = null;
private bool $hideNullValues = false;
private array $htmlAttributes = [];

public function __construct()
{
Expand Down Expand Up @@ -507,4 +508,19 @@ public function hideNullValues(bool $hide): void
{
$this->hideNullValues = $hide;
}

public function getHtmlAttributes(): array
{
return $this->htmlAttributes;
}

public function setHtmlAttributes(array $htmlAttributes): void
{
$this->htmlAttributes = $htmlAttributes;
}

public function setHtmlAttribute(string $attribute, mixed $value): void
{
$this->htmlAttributes[$attribute] = $value;
}
}
8 changes: 7 additions & 1 deletion src/Resources/views/layout.html.twig
Expand Up @@ -52,6 +52,12 @@
{% endblock %}
</head>

{% macro render_html_attributes(attributes) %}
{% for attribute_name, attribute_value in attributes %}
{{ attribute_name }}="{{ attribute_value|e('html_attr') }}"
{% endfor %}
{% endmacro %}

{% block body %}
<body {% block body_attr %}{% endblock %}
id="{% block body_id %}{% endblock %}"
Expand Down Expand Up @@ -176,7 +182,7 @@
{% endif %}
{% endset %}

<div class="wrapper">
<div class="wrapper" {{ _self.render_html_attributes(ea.crud.htmlAttributes) }}>
{% block wrapper %}
<div class="responsive-header">
{% block responsive_header %}
Expand Down
40 changes: 40 additions & 0 deletions tests/Controller/CustomHtmlAttributeCrudControllerTest.php
@@ -0,0 +1,40 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller;

use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\CustomHtmlAttributeCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\SecureDashboardController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Category;

class CustomHtmlAttributeCrudControllerTest extends AbstractCrudTestCase
{
protected EntityRepository $categories;

protected function getControllerFqcn(): string
{
return CustomHtmlAttributeCrudController::class;
}

protected function getDashboardFqcn(): string
{
return SecureDashboardController::class;
}

protected function setUp(): void
{
parent::setUp();
$this->client->followRedirects();
$this->client->setServerParameters(['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);

$this->categories = $this->entityManager->getRepository(Category::class);
}

public function testHasCustomAttributes(): void
{
$crawler = $this->client->request('GET', $this->generateIndexUrl());
static::assertCount(1, $crawler->filter('div[multi-test-one="test1"]'));
static::assertCount(1, $crawler->filter('div[multi-test-two="test2"]'));
}
}
@@ -0,0 +1,50 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller;

use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Category;

class CustomHtmlAttributeCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Category::class;
}

public function configureCrud(Crud $crud): Crud
{
return $crud
->setHtmlAttribute('multi-test-one', 'test1')
->setHtmlAttribute('multi-test-two', 'test2');
}

public function configureFields(string $pageName): iterable
{
return [
TextField::new('name'),
];
}

public function configureActions(Actions $actions): Actions
{
$action1 = Action::new('action1')->linkToCrudAction('');
$action2 = Action::new('action2')->linkToCrudAction('')->setCssClass('foo');
$action3 = Action::new('action3')->linkToCrudAction('')->addCssClass('bar');
$action4 = Action::new('action4')->linkToCrudAction('')->setCssClass('foo')->addCssClass('bar');

return $actions
->add(Crud::PAGE_INDEX, $action1)
->add(Crud::PAGE_INDEX, $action2)
->add(Crud::PAGE_INDEX, $action3)
->add(Crud::PAGE_INDEX, $action4)
->update(Crud::PAGE_INDEX, Action::NEW, function (Action $action) {
return $action->setIcon('fa fa-fw fa-plus')->setLabel(false);
})
;
}
}