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

fix: setting html attribute to fields also adds them to the index page #6233

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
20 changes: 20 additions & 0 deletions src/Dto/FieldDto.php
Expand Up @@ -54,6 +54,7 @@ final class FieldDto
/** @internal */
private $uniqueId;
private KeyValueStore $displayedOn;
private array $htmlAttributes = [];

public function __construct()
{
Expand Down Expand Up @@ -476,4 +477,23 @@ public function isDisplayedOn(string $pageName): bool
{
return $this->displayedOn->has($pageName);
}

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

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

return $this;
}

public function setHtmlAttribute(string $attribute, mixed $value): self
{
$this->htmlAttributes[$attribute] = $value;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Field/FieldTrait.php
Expand Up @@ -151,6 +151,7 @@ public function setHtmlAttribute(string $attributeName, $attributeValue): self
}

$this->dto->setFormTypeOption('attr.'.$attributeName, $attributeValue);
$this->dto->setHtmlAttribute($attributeName, $attributeValue);

return $this;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Resources/views/crud/index.html.twig
Expand Up @@ -44,6 +44,12 @@
{%- endapply -%}
{% endblock %}

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

{% set has_batch_actions = batch_actions|length > 0 %}
{% block page_actions %}
{% if filters|length > 0 %}
Expand Down Expand Up @@ -148,7 +154,7 @@
{% for field in entity.fields %}
{% set is_searchable = null == ea.crud.searchFields or field.property in ea.crud.searchFields %}

<td data-column="{{ field.property }}" data-label="{{ field.label|trans|e('html_attr') }}" class="{{ is_searchable ? 'searchable' }} {{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
<td data-column="{{ field.property }}" data-label="{{ field.label|trans|e('html_attr') }}" class="{{ is_searchable ? 'searchable' }} {{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}" {{ _self.render_html_attributes(field) }}>
{{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
</td>
{% endfor %}
Expand Down
41 changes: 41 additions & 0 deletions tests/Controller/CustomFieldAttributeControllerTest.php
@@ -0,0 +1,41 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller;

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

class CustomFieldAttributeControllerTest extends AbstractCrudTestCase
{
protected EntityRepository $categories;

protected function getControllerFqcn(): string
{
return CustomFieldAttributeCrudController::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 testItAddsAttributesToTd(): void
{
$crawler = $this->client->request('GET', $this->generateIndexUrl());

static::assertCount(20, $crawler->filter('td[multi-test-one="test1"]'));
static::assertCount(20, $crawler->filter('td[multi-test-two="test2"]'));
}
}
@@ -0,0 +1,48 @@
<?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;

/**
* Tests the configureActions() method and the generated actions.
*/
class CustomFieldAttributeCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Category::class;
}

public function configureFields(string $pageName): iterable
{
return [
TextField::new('name')
->setHtmlAttribute('multi-test-one', 'test1')
->setHtmlAttribute('multi-test-two', 'test2'),
];
}

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);
})
;
}
}