Can we add select dropdown of custom module in shopware 6 admin category module?

446 Views Asked by At

I have created one module Color

I want to add this module to shopware 6 default module which is Category

Is it possible to add dropdown list with multiple select of Color when we add/edit Category

I did try to do it with custom field option, but my color module has multiple fields which I can use in front side.

1

There are 1 best solutions below

0
dneustadt On

The most straight forward way to do this would indeed be using custom fields. As you already noted the custom field referring to an entity will only contain a single value, by default the id of the entity. You can use the id to resolve the referenced entity when the category is loaded.

Step 1: Create the custom field

$customFieldSetRepository = $container->get('custom_field_set.repository');
$customFieldSetRepository->create([
    [
        'name' => 'my_custom_field_set_color',
        'label' => ['en-GB' => 'Color'],
        'relations' => [[
            'entityName' => 'category',
        ]],
        'customFields' => [
            [
                'name' => 'my_custom_field_color',
                'type' => 'select',
                'config' => [
                    'label' => ['en-GB' => 'Color'],
                    'entity' => 'color',
                    'labelProperty' => 'name',
                    'customFieldType' => 'entity',
                    'componentName' => 'sw-entity-single-select',
                    'customFieldPosition' => 1,
                ],
            ],
        ]
    ],
], Context::createDefaultContext());

Step 2: Set up a subscriber to fetch the referenced entity from the id in the custom field when the category is loaded. To store your custom entity with the category entity you can use an extension.

class CategorySubscriber implements EventSubscriberInterface
{
    /**
     * in the service definition this would be the repository of your
     * custom entity, e.g. 'color.repository'
     */
    private EntityRepositoryInterface $repository;

    public function __construct(
        EntityRepositoryInterface $repository
    ) {
        $this->repository = $repository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            'sales_channel.' . CategoryEvents::CATEGORY_LOADED_EVENT => 'entityLoaded',
        ];
    }

    public function entityLoaded(EntityLoadedEvent $event): void
    {
        /** @var CategoryEntity $category */
        foreach ($event->getEntities() as $category) {
            $customFields = $category->getCustomFields();

            if (empty($customFields['my_custom_field_color'])) {
                continue;
            }

            $criteria = new Criteria([$customFields['my_custom_field_color']]);
            $entity = $this->repository->search($criteria, $event->getContext())->first();

            if (!$entity) {
                continue;
            }

            $category->addExtension('myCustomFieldColor', $entity);
        }
    }
}

Step 3: In the template you check for the extension and if it exists you can use the extension as an entry point to your custom entity.

{% if category.extensions.myCustomFieldColor is defined %}
    {{ category.extensions.myCustomFieldColor.hexCode }}
{% endif %}

Another way to this would be to use an EntityExtension to add a proper association from your custom entity to category. This will allow you to reference your entity when querying categories with the DAL, e.g. for filtering by color. From the point of performance this would also be preferable, but the implementation requires more steps. If you decide to go that route, I have an example repository where I add an association from a custom entity to the customer entity and manually add a field for it in the administration. Maybe this will be of help.