1

I have an Entry field layout where only a few Plain Text fields are translatable. Users can edit these Entries from front-end, and I'm looking for the most efficient way to let them save the translations as Draft from a single submit.

I know that I can force the site language where the Entry is saved to using this to the form:

{{ input('hidden', 'siteId', site.id) }}

But this is saving the whole form to this site, not a specific field.

Is there any way to save fields independently to a specific site?

The only solution I found is to:

  • Duplicate the translated fields to non-translated fields
  • Add all of them on the front-end and hide them in the CP
  • On save, add an Event in a custom module to store their respective values to the correct site

This works, but doesn't sound simple and clean at all.

1 Answer 1

2

You're not going to be able to pull this off with the stock elements/save-element action by itself.

In addition to the workarounds you mention, you could consider handling the form submission with JavaScript, POSTing the form (with a different site ID and values for the translatable fields) once for each site, in a seamless manner.

A final option, which is possibly the cleanest (albeit not necessarily the simplest) is to forego the core elements/save-element action altogether, by implementing the necessary logic in a custom controller action in a custom module.

FWIW, the recent launch of Craft Generator has made scaffolding both custom modules and individual system components (e.g., controllers) a bit simpler.

2
  • Thank you for your feedback. As there's no native solution for this, I like the custom module option, which seems cleaner and at least safer than the JS solution. Good point for no need to create useless fields, by filling the values within the custom module.
    – Romain P.
    Dec 28, 2022 at 8:33
  • Would you mind adding an example of how to loop through locales and save the matches to fields? My Entries are saved as Draft, and it's a bit challenging within the EVENT_BEFORE_SAVE_ELEMENT: I don't find out how to apply the translations to a Draft which doesn't already exists.
    – Romain P.
    Dec 28, 2022 at 8:37

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.