Skip to content

Commit 0bf9b81

Browse files
oleg-andreyevclaude
andcommitted
Add isAccessible property to FieldDto for controlling field visibility
- Added isAccessible property to FieldDto with default value true - Added markAsInaccessible() method to mark fields as inaccessible - Added isAccessible() method to check field accessibility - Updated AbstractCrudController to pass fields to index template - Updated index template to filter fields by accessibility and show lock icon for inaccessible fields 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent b5c785b commit 0bf9b81

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/Controller/AbstractCrudController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function index(AdminContext $context)
153153
'pageName' => Crud::PAGE_INDEX,
154154
'templateName' => 'crud/index',
155155
'entities' => $entities,
156+
'fields' => $fields,
156157
'paginator' => $paginator,
157158
'global_actions' => $actions->getGlobalActions(),
158159
'batch_actions' => $actions->getBatchActions(),

src/Dto/FieldDto.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ final class FieldDto
6666
private KeyValueStore $displayedOn;
6767
/** @var array<string, bool|int|float|string> */
6868
private array $htmlAttributes = [];
69+
private bool $isAccessible = true;
6970

7071
public function __construct()
7172
{
@@ -560,4 +561,14 @@ public function setHtmlAttribute(string $attribute, mixed $value): self
560561

561562
return $this;
562563
}
564+
565+
public function markAsInaccessible(): void
566+
{
567+
$this->isAccessible = false;
568+
}
569+
570+
public function isAccessible(): bool
571+
{
572+
return $this->isAccessible;
573+
}
563574
}

templates/crud/index.html.twig

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106

107107
{% set ea_sort_asc = constant('EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Option\\SortOrder::ASC') %}
108108
{% set ea_sort_desc = constant('EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Option\\SortOrder::DESC') %}
109-
{% for field in entities|filter(e => e.isAccessible)|first.fields ?? [] %}
109+
{% for field in fields|filter(f => f.isAccessible) ?? [] %}
110110
{% set is_searchable = null == ea.crud.searchFields or field.property in ea.crud.searchFields %}
111111
{% set is_sorting_field = ea.search.isSortingField(field.property) %}
112112
{% set next_sort_direction = is_sorting_field ? (ea.search.sortDirection(field.property) == ea_sort_desc ? ea_sort_asc : ea_sort_desc) : ea_sort_desc %}
@@ -150,11 +150,15 @@
150150
{% endif %}
151151

152152
{% for field in entity.fields %}
153-
{% set is_searchable = null == ea.crud.searchFields or field.property in ea.crud.searchFields %}
153+
{% if not field.isAccessible %}
154+
<twig:ea:Icon name="internal:lock" class="mr-1" />
155+
{% else %}
156+
{% set is_searchable = null == ea.crud.searchFields or field.property in ea.crud.searchFields %}
154157

155-
<td data-column="{{ field.property }}" data-label="{{ field.label|trans|e('html') }}" class="{{ is_searchable ? 'searchable' }} {{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}" {% for name, value in field.htmlAttributes %}{{ name }}="{{ value|e('html_attr') }}" {% endfor %}>
156-
{{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
157-
</td>
158+
<td data-column="{{ field.property }}" data-label="{{ field.label|trans|e('html') }}" class="{{ is_searchable ? 'searchable' }} {{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}" {% for name, value in field.htmlAttributes %}{{ name }}="{{ value|e('html_attr') }}" {% endfor %}>
159+
{{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
160+
</td>
161+
{% endif %}
158162
{% endfor %}
159163

160164
{% block entity_actions %}

0 commit comments

Comments
 (0)