sanity-io / sanity-plugin-internationalized-array

A plugin to register array fields with a custom input component to store field values in multiple languages, queryable by using the language ID as an array `_key`.
MIT License
41 stars 10 forks source link

Internationalized Array String in objects not editable #37

Closed appfr3d closed 10 months ago

appfr3d commented 11 months ago

I have a document called Menu, and which among other fields has an array of a custom menuItem object. In the menuItem I have defined the title field as an internationalizedArrayString like so:

    defineField({
      name: 'title',
      title: 'Title',
      type: 'internationalizedArrayString',
    }),

When trying to create new or edit menuItems, it is not possible to add any translations.

image

In my defineConfig I have this:

    internationalizedArray({
      languages: [
          {id: 'nb', title: 'Norwegian (Bokmål)'},
          {id: 'nn', title: 'Norwegian (Nynorsk)'},
          {id: 'en', title: 'English'},
      ],
      defaultLanguages: ['nb'],
      fieldTypes: ['string', 'url', 'slug'],
      buttonAddAll: true,
      buttonLocations: ['field'],
    }),

Any obvious reason why this is happening?

selbekk commented 11 months ago

Looks like if you add an internationalizedArrayString field type to the document itself, it works as expected. This is because of this check, which doesn't check for nested arrays of objects.

It looks like the flattenSchema doesn't support iterating through arrays of objects like we do here.

bobinska-dev commented 11 months ago

Hello everyone, We are looking into this, but as a workaround, you can add a hidden internationalizedArrayString field at the document level, and then it works in the nested structure as well. Not the best solution but a quick fix for now.

selbekk commented 11 months ago

Yep, that's what we did. Hopefully we can avoid it in the future :-)

DmitriyMaltsevTakeda commented 8 months ago

I'm having the same problem.

In my case, there is the following document hierarchy: document (type Foo) -> array of objects (type Bar) -> array of objects (type Baz)

Objects of type Baz have several fields of type internationalizedArrayString, and they are not editable (same problem as OP had).

If I add a hidden field of type internationalizedArrayString to Foo as suggested by @selbekk and @bobinska-dev, it works. If I do the same for Bar only, the error still reproduces.

I briefly looked at the fix, and it seems that it can handle only the case with one level of nesting in root documents, but I may be wrong.

Is there any way to fix that without the hidden internationalizedArrayString field workaround?