BoltTranslate / Translate

Provides translation for contenttypes.
Other
43 stars 37 forks source link

unexpected behaviour on adding new language to existing bolt installation #151

Closed virtualize closed 3 years ago

virtualize commented 7 years ago

A fatal error occurs, if you add the translate extension at running single language bolt installation. The database fields were set but the initial field values are empty. So if you try to switch to the new language in bolt, the system does not find an array in Line 139 – a fatal error occur. It only work, if you re-save the content in the given language and than switch to the new language.

https://github.com/BoltTranslate/Translate/blob/2d445c583e6aa97dfc34035c8d05944d86c0e43e/src/EventListener/StorageListener.php#L139

Here we should check if $localeData is actually an array and if not, do an early return.

$localeData = json_decode($subject[$localeSlug . 'data'], true);
if (!is_array($localeData)) {
  return;
}
lenvanessen commented 7 years ago

Hi @virtualize , I've run past your steps but can't reproduce the issue.

The posthydrate event is triggered after the save, so any translatable field is already json-encoded and stored in the database, no matter what language you're editing first there should be json stored and therefore an extra check should not be needed.

That said, i'd like to figure out why this is not the case for you, so we can dig up the real bug.

Could you provide me with:

virtualize commented 7 years ago

Hi @lenvanessen thanks for your fast reply. I've added the issue for a coworker of mine, so I will redirect your questions to her. @pakadesign could you please provide these files / answers?

jnvsor commented 6 years ago

We just ran into this issue ourselves.

The posthydrate event is triggered after the save

Unless I'm mistaken (and bolt is needlessly saving a content type every time you simply open the form) posthydrate is triggered after the load. (Hydration is putting database stuff into objects no?)

The problem occurs because line 136 only checks the value with isset, so it works fine if you have NULL. In some cases when adding languages the default value of $lang.'data' fields is an empty string, which results in a null when json_decoded and throws a fatal error when trying to foreach over it.

It should be checked both before and after json_decode, as is done in prehydrate (See line 98)

I'd say there are 2 bugs here:

  1. Adding new languages somehow adds data fields with empty string as the default value
  2. Empty strings aren't properly checked and cause the admin to go boom