Search code examples
twigsyliussymfony-4.3

Find a div in a block render in Symfony


I am doing a site with symfony 4. I start on this framework and I have a problem of design on my site. I've been looking for where it might come from, but I can not find the code I'd like to remove.

The part of the code I would like to remove is the following because the result is pretty ugly: enter image description here

enter image description here

I can not find this in my code:

in my controller :

/**
 * @return Response
 */
public function newLetterAction(Request $request): Response
{
    $form = $this->createForm(CustomerNewsletterType::class, new Customer());
    $form->handleRequest($request);

    $facebook = $this->manager->getRepository(ExternalUrl::class)->findOneByCode('facebook');
    $instagram = $this->manager->getRepository(ExternalUrl::class)->findOneByCode('instagram');

    return $this->templatingEngine->renderResponse('@SyliusShop/Homepage/_newsletter.html.twig', [
        'facebook' => $facebook,
        'instagram' => $instagram,
        'form' => $form->createView(),
        'rova_refonte' => (in_array($this->container->get('request_stack')->getMasterRequest()->attributes->get('_route'),["sylius_shop_homepage"]) ? true : false)
    ]);
}

in my formType :

    class CustomerNewsletterType extends AbstractResourceType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', EmailType::class, [
                'label' => 'app.ui.newsletter',
                'attr' => [
                    'placeholder' => 'app.ui.email'
                ]
            ])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix(): string
    {
        return 'app_customer_newsletter';
    }
}

in my twig:

{{ render(controller('app.controller.shop_homepage:newLetterAction')) }}

if anyone could tell me how to find the code, it would help me a lot.

Thank you


Solution

  • Everything is done under the hood when you call $form->createView().
    To sum up, every type of field into the form has a base rendering using twig blocks (so does the form itself), which can be overriden. This is what is called the form theme, there's a base one which is usually this one in the twig-bridge.

    You can create new themes, extend the existing one, or event create just what you need for a specific form (hint : the getBlockPrefix function in your form type is used for this).

    You can find all the documentation about the form rendering here :
    https://symfony.com/doc/current/form/form_customization.html

    Most functions described in this documentation are in fact calling twig blocks of form themes, and you can find the documentation about this here :
    https://symfony.com/doc/current/form/form_themes.html

    Keep in mind: Removing such a class / div might break existing CSS, error rendering or everything done in javascript targeting this class.