Search code examples
javascriptreactjssanity

How can i add a field for e.g. an alt text to every image in an array in Sanity?


i am trying to add a string object to every image in an array in sanity, but i am not successfull altough my solution makes pretty much sense to me:

{
      name: 'images',
      title: 'Images',
      type: 'array',
      of: [
        {
          name: 'image',
          type: 'image',
          title: 'Image',
          options: {
            hotspot: true,
          },
          fields: [
            {
              name: 'alt',
              type: 'string',
              title: 'Alternative text',
            },
          ],
        },
      ],
    },

This is what it looks like in studio

All what is displayed in sanity studio is the field to upload an image, can somebody help me with what is wrong here?


Solution

  • Use an object type:

    {
      name: 'images',
      title: 'Images',
      type: 'array',
      of: [
        {
          type: 'object',
          name: 'person',
          fields: [
            {
              name: 'alt',
              type: 'string',
              title: 'Alternative text',
            },
            {
              name: 'image',
              type: 'image',
              title: 'Image',
              options: {
                hotspot: true,
              },
            },
          ]
        }
      ]
    }