4

I want to patch an attribute when a certain node is present.

In my concrete example below I would to change the lowercaseUrls attribute for the commerce provider, but only when the commerce provider node is present (otherwise Sitecore throws an error).

The example snippet:

<linkManager >
  <providers>
    <!-- Sitecore Commerce SXA adds commerce as default, patch it as well if it exists-->
    <add name="commerce">
       <patch:attribute name="lowercaseUrls">true</patch:attribute>
    </add>
  </providers>
</linkManager>

Is it possible to only apply the patch when a commerce named provider is present?

1 Answer 1

2

The only which I know is to use 2 config files and a bit of hacking.

First config file must be loaded BEFORE your commerce link provider is defined, which unfortunately means you must put it not in the Include folder but ideally inside the same folder where commerce link provider is configured:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <linkManager>
        <providers>
            <add name="commerce" type="Wrong.Type">
            </add>
        </providers>
    </linkManager>
  </sitecore>
</configuration>

This config file define commerce link provider with Wrong.Type on purpose. It will allow us to remove the provider later if it's not updated with the correct settings.

The second config file must be added after the default config with commerce link provider, so in Include/zzz or any folder which works for you best:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <linkManager>
        <providers>
            <add name="commerce">
                <patch:attribute name="lowercaseUrls">true</patch:attribute>
            </add>

            <add type="Wrong.Type">
                <patch:delete />
            </add>
        </providers>
    </linkManager>
  </sitecore>
</configuration>

It contains a commerce provider with <patch:attribute>' forlowercaseUrlsattribute andelement for any provider with type equal toWrong.Type`.

So how does it work now?

  1. New provider with name="commerce" type="Wrong.Type" lowercaseUrls="true" attributes is defined.
  2. If there is now a proper config with proper attributes for commerce provider, Sitecore will merge based on the name of the providers and will replace the Wrong.Type with proper type of the provider.
  3. And the second custom config first sets lowercaseUrls url to true and then deletes any provider with Wrong.Type type. If proper config doesn't exist, provider defined in 1. will be removed. Otherwise nothing will be removed cause proper config changed type of the provider 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.