akeneo / pim-community-dev

[Community Development Repository] The open source Product Information Management (PIM)
http://www.akeneo.com
Other
952 stars 514 forks source link

Custom attribute with custom fields #9999

Open pfazzi opened 5 years ago

pfazzi commented 5 years ago

I'm implementing a custom attribute type. It has a custom field i'd like to store in its properties. Class Akeneo\Pim\Structure\Component\Updater\AttributeUpdater in it's method validateDataType has an hardcoded list of allowed fields, so in order to add a field i have to refedine that method. Is there a way to avoid it?

Doodoune commented 5 years ago

Actually, when you want to add a custom attribute type, you need to override AttributeUpdater and redefine this method. I don't see another way to do that natively.

pfazzi commented 5 years ago

Yes, i know but that is dangerous because if i create 2 independent bundles that add 2 different attribute types, i have to redefine this method 2 times but eventually they will result with an incompatible setup because both the bundles redefine that method and i have to choose one of them.

Actualy i solved using a Compiler Pass that extends the definition of the pim_catalog.updater.attribute service adding the property unit, that is the custom field of my attribute:

final class CompilerPass implements CompilerPassInterface
{
    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     */
    public function process(ContainerBuilder $container): void
    {
        $definition = $container->getDefinition('pim_catalog.updater.attribute');

        $properties = $definition->getArgument(4);

        $definition->setArgument(4, array_merge($properties, ['unit']));
    }
}

This way if i need another custom attribute it will not be in conflict, in addition i donìt need to redefine the method. Do you see any drawbacks in this solution?

Anyway I think this AttributeUpdater service should be modified in order to be more open for extension.

Doodoune commented 5 years ago

Glad to see that you found a solution.

I've raised a technical improvement ticket to our Product Team (internal reference is TIP-1166). We will keep you updated when it will be prioritized.

pfazzi commented 5 years ago

Thanks! Do you see any drawbacks in this solution?