Custom Attribute types

Does anyone know how I could add a custom attribute type? I tried to override one of the current form types but that wasn’t doing what I needed, so I feel that adding a separate attribute type (as in current types are text, textarea, date, datetime etc.) would solve my issue.

I can see a lot of documentation on using code to add attributes to products etc. but not how to make a custom attribute type.

Looks like the question has been asked before here and here but no responses.

I’ve asked same question some time ago but it looks like we have to figure this out by ourself.

I’m on some other project at the moment but I will try to make a plugin or something for new float attribute type soon.

Yeah, seems quiet here unfortunately (I also tried asking on the Slack channel). For me it is a CKEditor field I’d like to add as a type. I tried to override the textarea one but had all sorts of issues there too.

It looks like the Sylius code for attribute types is built around them being declared dynamically but I’m not sure how we could “inject” a custom one (I assume as a service) to be included.

Okay, since we are likely on our own here, here’s how I have gotten it to mostly work. What is left is likely just twig template overrides (getting the option in the “Create” attribute type list and output formatting).

  1. Copied both the Component and the AttributeBundle Form/Type files for a similar attribute type to a new AttributeType subdirectory in my local App\Form\Type directory. I named them CKEditorAttributeType and CKEditorAttributeTypeComponent rather than having them in separate directories.

  2. Added

        App\Form\Type\AttributeType\CKEditorAttributeTypeComponent:
            autowire: false
    

    entry to the service.yaml services:_instanceof section to avoid the component being autoloaded without my specific tags below.

  3. Added

    sylius.attribute_type.ckeditor:
        class: App\Form\Type\AttributeType\CKEditorAttributeTypeComponent
        tags:
            - { name: sylius.attribute.type, attribute_type: ckeditor, label: CKEditor, form_type: App\Form\Type\AttributeType\CKEditorAttributeType }
    

    to the services.yaml services section to specify the service tags.

  4. Modified the component and type files to suit.

As I mentioned, I haven’t finished yet, but I believe this is the core of it and the rest is overriding Twig templates.