Link ID fields in EasyAdmin
If you also use EasyAdmin and are exhausted by clicking twice to see an entity ID when on the entity index page, you can use this small tweak to have your index page generate the detail page link on the ID field.
For this, simply override the template crud/field/id.html.twig
.
The original value is currently this:
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
{# this template is used to display Doctrine entity primary keys #}
{{ field.formattedValue }}
Instead, you could replace its content with this:
{# Adds the entity detail page link #}
<a href="{{ ea_url().setAction('detail').setEntityId(field.value) }}">{{ field.formattedValue }}</a>
The problem is, the `detail` action is not enabled by default or it could be specifically removed from your CRUD configuration. Resulting in having the link when it should not.
In order to fix this permission issue, make the link conditional:
{# Adds the entity detail page link if the CRUD has the "detail" action #}
{% if entity.actions|filter(action => action.name == constant('EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Action::DETAIL'))|length > 0 %}
<a href="{{ ea_url().setAction('detail').setEntityId(field.value) }}">{{ field.formattedValue }}</a>
{% else %}
{{ field.formattedValue }}
{% endif %}
And done!
This isn't a big deal but hopefully helpful for those who need this small improvement.
Have a nice day!