Jump to content

When trying to use the TranslatorAwareType class, I get an error.


compsoul.dev

Recommended Posts

<?php

declare(strict_types=1);

namespace PrestaShop\Module\CompsoulCategories\Form;

use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use PrestaShopBundle\Form\Admin\Type\CategoryChoiceTreeType;
use Symfony\Component\Form\FormBuilderInterface;

class ConfigurationFormType extends TranslatorAwareType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('category_ids', CategoryChoiceTreeType::class, [
                'label' => $this->trans('Category choice', 'Modules.CompsoulCategories.Admin'),
            ]);
    }
}

 


Too few arguments to function PrestaShopBundle\Form\Admin\Type\TranslatorAwareType::__construct(), 0 passed in D:\wamp64\www\skate\vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php on line 89 and exactly 2 expected

 

obraz.thumb.png.3c7b0c03fb9dc40bd166a60cb1a08c08.png

Link to comment
Share on other sites

  • 2 weeks later...

I forgot what I did :( Probably clearing (deleting everything) from the folder helped: \var\cache
Now my code looks like this:

 

declare(strict_types=1);

namespace PrestaShop\Module\CompsoulCategories\Form;

use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use PrestaShopBundle\Form\Admin\Type\TranslatableType;
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
use PrestaShopBundle\Form\Admin\Type\CategoryChoiceTreeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class Form extends TranslatorAwareType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('category_heading', TranslatableType::class, [
                'label' => $this->trans('Section heading', 'Modules.CompsoulCategories.Admin'),
                'type' => TextType::class,
                'required' => false,
            ])
            ->add('category_content', TranslatableType::class, [
                'label' => $this->trans('Section description', 'Modules.CompsoulCategories.Admin'),
                'type' => FormattedTextareaType::class,
                'required' => false,
            ])
            ->add('category_ids', CategoryChoiceTreeType::class, [
                'label' => 'Category',
                'multiple' => true,
            ])
        ;
    }
}

 

Link to comment
Share on other sites

  • 3 months later...

You have the option to use 'class ConfigurationFormType extends AbstractType...' instead of 'class ConfigurationFormType extends TranslatorAwareType...'.

However, if you opt to use 'TranslatorAwareType', you must inject the translation service into your custom type:

you_module/config/services.yaml

services:
  _defaults:
    public: true 

  you_module.form.type.custom_form_type:
    class: PrestaShop\Module\Custom\Form\ConfigurationFormType
    parent: 'form.type.translatable.aware'
    public: true
    arguments:
      - "@router"
      - "@=service('prestashop.adapter.legacy.configuration').getBoolean('PS_REWRITING_SETTINGS')"
      - "@=service('prestashop.adapter.legacy.configuration').getBoolean('PS_FORCE_FRIENDLY_PRODUCT')"
      - '@prestashop.adapter.legacy.context'
      - '@PrestaShop\PrestaShop\Core\ConfigurationInterface'
    tags:
      - { name: form.type }

In your custom form class, retrieve it in the constructor method like this examle:

 

<?php

namespace YouModule\Custom\Form\Type;

use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use PrestaShopBundle\Translation\TranslatorInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;

class SeoEnhanceFieldType extends TranslatorAwareType
{

    /**
     * @var RouterInterface
     */
    private $router;

    /**
     * @var bool
     */
    private $friendlyUrlEnabled;

    /**
     * @var bool
     */
    private $forceFriendlyUrl;

    /**
     * @var LegacyContext
     */
    private $legacyContext;

    /**
     * @var ConfigurationInterface
     */
    private $configuration;

    /**
     * @param TranslatorInterface $translator
     * @param array $locales
     * @param RouterInterface $router
     * @param bool $friendlyUrlEnabled
     * @param bool $forceFriendlyUrl
     * @param LegacyContext $legacyContext
     * @param ConfigurationInterface $configuration
     */
    public function __construct(
        TranslatorInterface $translator,
        array $locales,
        RouterInterface $router,
        bool $friendlyUrlEnabled,
        bool $forceFriendlyUrl,
        LegacyContext $legacyContext,
        ConfigurationInterface $configuration
    ) {
        parent::__construct($translator, $locales);
        $this->router = $router;
        $this->friendlyUrlEnabled = $friendlyUrlEnabled;
        $this->forceFriendlyUrl = $forceFriendlyUrl;
        $this->legacyContext = $legacyContext;
        $this->configuration = $configuration;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('customField', TextType::class, [
                'label' => 'Custom Field',
                'data_class' => null,
            ]);
    }

	// your logique here

}

 

 

 

Edited by Maamria (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...