5

For a new fashion project we want to define a new product image attribute (color_swatch or pattern). We can't use the standard color swatch functionality, because for fashion products the color swatch is mostly product specific. The definition of the new attribute(s) is exactly like in M1, and can be done in backend or in code. So far so good.

It would be nice to define image sizes via view.xml, so that we have a standard resize of these new images that is generated automatically. Unfortunately, the view.xml is validated against an XSD validation, which restricts the type options for the different images (see vendor/magento/framework/Config/etc/view.xsd). I tried to extend the default validation using DI, it might work (kind of) but it feels really hacky and it's absolutely neither flexible nor portable. I really thought this would be an easy task (in M1 it was very straightforward and we used it in nearly every project), but I couldn't find a solution that feels proper.

Has anyone already tackled a similar task? How did you solve it? I could maybe skip this view.xml step altogether, or rewrite the whole thing, but I feel there should be a way to get around it. Does the system have a standard way to achieve what I'm trying to do?

1

2 Answers 2

2

Yes I agree that this XSD is too strict and we have added this as an item to be fixed in core.

In the meantime you can re-configure DI to use a custom “SchemaLocator” class where you can configure it to use an alternative XSD file which redefines original “imageType” complex type. Here is a quick example how to do that “correctly” in the given circumstances.

In DI:

<type name="Magento\Framework\Config\View">
    <arguments>
        <argument name="schemaLocator" xsi:type="object”>Your\Extension\Config\SchemaLocator</argument>
    </arguments>
</type>

In "Your\Extension\Config\SchemaLocator":

/**
 * @param \Magento\Framework\Config\Dom\UrnResolver $urnResolver
 */
public function __construct(UrnResolver $urnResolver)
{
    $this->schema = $urnResolver->getRealPath('urn:magento:module:Your_Extension/etc/view.xsd');
}

In "Your_Extension/etc/view.xsd”:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:redefine schemaLocation="urn:magento:framework:Config/etc/view.xsd">
        <xs:complexType name="imageType">
            ...
        </xs:complexType>
    </xs:redefine>
</xs:schema>
3
  • That's exactly how I tried to solve it. But with this approach I can only install my image attributes in one module, since only one DI config "wins"... It might work for project-specific modules, but it wouldn't work for 3rd party modules.
    – fmrng
    Commented Jul 2, 2016 at 10:41
  • 1
    Note it should be urn:magento:module:Your_Extension:etc/view.xsd, thanks this works well.
    – jzahedieh
    Commented Dec 12, 2016 at 17:33
  • Hi Gordon, I've been at this for a while now trying to decipher the XSD. My extension's view.xsd is being used but I don't want to copy and change it a bit, I want to extend (or redefine) the XSD. I try to allow a new element at the same level as width/height/constrain/frame etc. but I don't think I can reference the image element since it's a local complexType? This is the first time I'm doing something with an XSD so I hope you have some pointers. Thanks! Commented Apr 25, 2018 at 0:06
0

Not sure when it happened, but if anyone is running into this now, the xsd validation of image types has been updated to include any string matching <string>_<string> and not just the builtin types. Fudging the xsd validation is no longer required, certainly as of 2.3.5 (and probably alot earlier)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.