🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Adding images to the blockContent schema in Sanity.io

30 replies
Last updated: Apr 4, 2023
Hello, I have a question about the blockContent-schema + PortableText/React plugin. I build small sites for small businesses, often creative people who wants to custom their’ content a lot. As of now I use the portable text module as often as I can, as they can add/remove content as they wish, without me having to edit the studio. A little like other cmses like hubspot where the rich text-module allows the client to add pretty much anything.BUT, I’m wondering about images. Is there any addon or alternative to the blockContent that allows for the user to add images? Or can you add some functionality to the blockContent so they can add images into it? Thanks in advance!
☀️
Apr 4, 2023, 9:37 AM
Hey User, did you check https://www.sanity.io/docs/portable-text-editor-configuration ? there is an image example:

export default {
  name: 'content',
  type: 'array',
  title: 'Content',
  of: [
    {
      type: 'block'
    },
    {
      type: 'image'
    }
  ]
}

Apr 4, 2023, 9:43 AM
rofl. Thank you!!!
Apr 4, 2023, 9:44 AM
Sorry to ask again, but where do I place this snippet?
Apr 4, 2023, 9:50 AM
In your schema file
Apr 4, 2023, 9:54 AM
I’ve added it like this, but the image-icon doesn’t appear
Apr 4, 2023, 9:55 AM
The text editor looks like this in the studio:
Apr 4, 2023, 9:57 AM
looks like it is one level too high up
Apr 4, 2023, 9:58 AM
The blue brackets above contains the type: ‘block’
Apr 4, 2023, 9:59 AM
So I can’t put image in there, can I?
Apr 4, 2023, 9:59 AM
no, it has to go in the content array
Apr 4, 2023, 10:00 AM
are you running the studio locally?
Apr 4, 2023, 10:00 AM
Yeah, for now yrs
Apr 4, 2023, 10:00 AM
yes*
Apr 4, 2023, 10:00 AM
can you please share the top part of your code
Apr 4, 2023, 10:01 AM
just close the
marks
attribute
Apr 4, 2023, 10:01 AM
close = collapse
Apr 4, 2023, 10:01 AM
I just tried it on my side and it worked with this code:


export default {
  name: 'chapter',
  type: 'document',
  title: 'Chapter',
  fields: [
    {
      title: 'Title',
      name: 'title',
      type: 'array',
      of: [
        {
          type: 'block',
          styles: [{title: 'Normal', value: 'normal'}],
          lists: [],
          marks: {
            // Only allow these decorators
            decorators: [{title: 'Strong', value: 'strong'}],
            annotations: [],
          },
        },
        {type: 'image'},
      ],
    },
  ],
}

Apr 4, 2023, 10:05 AM
make sure:• to use the latest version of sanity
• that there are no errors on console
• try to add a new content type just for testing / debugging
Apr 4, 2023, 10:05 AM
Hmm, let me update
Apr 4, 2023, 10:05 AM
Now all I get is this error..sigh…
Apr 4, 2023, 10:14 AM
maybe the best would be to create a new project with the latest version and then step by step apply your changes. which version where you on before? 2.x?
Apr 4, 2023, 10:16 AM
I’m not sure. But I started this project by creating a react app, and then installing and initializing Sanity.. I had this problem before too, but then I think I added a sanity.json-file and it worked.
Apr 4, 2023, 10:17 AM
okay, then I would try that again 🙂
Apr 4, 2023, 10:17 AM
So I just added a file, and the studio started, but still no image
Apr 4, 2023, 10:19 AM
And you are 100% sure that
Introtext
uses the content type / schema file you have changed?
Apr 4, 2023, 10:21 AM
to check that, you can change the marks / annotations. If they do not change, then sth is wrong
Apr 4, 2023, 10:21 AM
Uff.. I had added the content in the introtext section to be an array of blocks. So, by changing it to type blockContent fixed it. Thank you!
Apr 4, 2023, 10:25 AM
World record in patience there. 👑
Apr 4, 2023, 10:25 AM
Glad it worked! 🙂
Apr 4, 2023, 10:26 AM
Hey again
user B
, what do you use to render your images? I get “_Unknown block type “image”, specify a component for it in the
components.types
prop_” in the console. I’ve tried a few different ones but can’t seem to make it work
Apr 4, 2023, 8:17 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?