Search code examples
shopwareshopware6

Why Shopware admin keeps automatically fulfilling custom fields with the fallback language? I want to make it empty


I'm facing a problem. No matter how many times I clear up a custom field from product in English on admin, it always come back with the fallback language, in German. I want to make it empty in English and not having it pulling the value in German.

For example: Customer wants to fulfill shoes_description custom field. All products have a shoes_description information in de-DE, which is the default language, but NOT necessarily all products have the information in en-GB.

Before:

  • de-DE: "Schwarze Schuhe"
  • en-GB: "Schwarze Schuhe"

Then I update to:

  • de-DE: "Schwarze Schuhe"
  • en-GB: ""

When I save the product, that's what I get on the page reload on admin:

  • de-DE: "Schwarze Schuhe"
  • en-GB: "Schwarze Schuhe"

No... that's not how it supposed to work out. For the en-GB language I want the custom field to be empty "", not "Schwarze Schuhe", which is in German de-DE.

👉👉 How can I keep a custom field in the English language empty (""), and not "Schwarze Schuhe"?


Solution

  • As mentioned in your previous question there are two ways to access translated fields. The same rules apply to custom fields. If you empty the input of a custom field in the administration and hit save, the value for the corresponding property and language will become null (unless there's a default or min value like 0 for number fields).

    custom fields translations

    Then it's just a matter of using either the translated accessor or not, whether the property will be undefined or inherited from the fallback language.

    difference in using translated property or not

    However in the administration the value of the fallback language will be used for the value of the input field in any case. So you may empty the field, save the entity, the value will actually be null in the database, but the input's value will still show the inherited value of the fallback language. And once you save then, the inherited value will also become the value of the actually selected language. So you would have to clear the field every time before the save the product again.

    If you wanted to change the behavior in the administration, you'd have to override sw-custom-field-set-renderer and change all occurrences of [...].translated.customFields to [...].customFields.

    Honestly though, I'd avoid the trouble and tell the user to enter a single space character instead if they intend the field to be empty in a specific language. Then wherever it's used in the template add a whitespace trim modifier, followed by an empty check.

    {% if product.customFields.some_custom_field is defined and product.customFields.some_custom_field|trim is not empty %}
        ...
    {% endif %}