Jump to content

ColorPickerType in custom form


Recommended Posts

Hello, 

I'm using Prestashop 8.1.3 and i'm developing a module to add a custom field in category page. I'v created a function tu add a custom field :
 

<?php

declare(strict_types=1);

namespace PrestaShop\Module\BsllExtension\Form\Modifier;

use PrestaShop\Module\BsllExtension\Entity\CustomCategory;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId;
use PrestaShopBundle\Form\FormBuilderModifier;
use PrestaShopBundle\Form\Admin\Type\ColorPickerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Translation\TranslatorInterface;

final class CategoryFormModifier
{
    private $translator;
    private $formBuilderModifier;

    public function __construct(
        TranslatorInterface $translator,
        FormBuilderModifier $formBuilderModifier
    ) {
        $this->translator = $translator;
        $this->formBuilderModifier = $formBuilderModifier;
    }

    public function modify(
        ?CategoryId $categoryId,
        FormBuilderInterface $categoryFormBuilder
    ): void {
        $idValue = $categoryId ? $categoryId->getValue() : null;
        $customCategory = new CustomCategory($idValue);
        $this->modifyDescriptionTab($customCategory, $categoryFormBuilder);
    }

    private function modifyDescriptionTab(
        CustomCategory $customCategory,
        FormBuilderInterface $categoryFormBuilder
    ): void {
        $categoryFormBuilder->add(
            'bsll_custom_color',
            ColorPickerType::class,
            [
                'label' => 'Custom Color',
                'required' => false,
            ]
        );
    }
}

My custom field is added well, however it is displayed like a classic text field and not like the color selector which is presented in the documentation. I didn't find any exemple except the one in the documentation (https://devdocs.prestashop-project.org/8/development/components/form/types-reference/color-picker-type/) so I'm hopping someone would have an idea to help me. 

The custom fiels should be display like this

 

Thank you.

WhatIHave.png

whatIWant.png

Link to comment
Share on other sites

  • 2 months later...

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...