Configure ArrayField to show items as hyperlinks on edit from instead of inputs

62 views Asked by At

I have a list of ids which are fetched using Doctrine from database and which I need to display as hyperlinks on edit form.

I am trying to use ArrayField like this:

yield ArrayField::new('links')
    ->setFormTypeOption('allow_add', false)
    ->setFormTypeOption('allow_delete', false)
    ->setFormTypeOption('entry_type', UrlType:class)
    ->onlyOnForms();

But items are still displayed as <input type='text' ...>.

How can I change it to <a href='...'>?

1

There are 1 answers

1
ogash On

UrlType will just render an input and prepend the http://, i think in your case you should create a custom FormType with a form theme that renders as a link and add a listener on the form to change the entry_type in case of edit.

EDIT:

1 - Create a custom FormType:

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class YourCustomType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('your_field', TextType::class)
        ;
    }

    public function getParent()
    {
        return TextType::class;
    }
}

2 - create the form theme

{# templates/form/your_custom_theme.html.twig #}
{% block your_custom_widget %}
    <a href="{{ value }}">{{ value }}</a>
{% endblock %}

3 - Configure it in twig

twig:
    form_themes:
        - 'form/your_custom_theme.html.twig'

4 - configure it in easyadmin config

easy_admin:
    entities:
        YourEntity:
            class: App\Entity\YourEntity
            form:
                fields:
                    - { property: 'yourField', type: 'App\Form\Type\YourCustomType' }