Unlock seamless workflows and faster delivery with our latest releases – get the details

User seeks to lock array items in Sanity while keeping content editable

12 replies
Last updated: Aug 30, 2021
Hello,5th project on Sanity, loving the tool and the possibilities.
With the experience for the previous projects I now have a pretty good idea on how I like to setup my schemas and I’m trying to build a starter for our next projects.

Worked hard yersterday but didn’t manage to build what I want :
Is it possible to have a field which is an array when I’m building the content in the studio then “locking those array items” ultimately replacing in the studio the field with just the content of the array.

For instance I have my page document which has an array of modules that I can pick when creating the page then after I’m done setting it up, the user has a Home page with say Foo module (in that array), but just sees the home page which has a field foo module.

Sorry not native english speaker, hope what I’m aiming to do is understandable.
Aug 20, 2021, 9:58 AM
Hey User! Glad to hear you’re enjoying Sanity! To clarify, are you looking to make a field read-only after you’ve added the items you want to the array? Or are you looking to set up an Initial Value Template ?
Aug 20, 2021, 5:26 PM
Thanks for your answer basically I’d like the field to become read-only as in not being able to change the array items but still being able to modify the item content.Reading the Initial value template right now.
Aug 20, 2021, 5:28 PM
Got it: so you want to add the items to the array, then make it so that the objects cannot be added or removed, but can still have their content edited. Initial Value Templates are not what you’re looking for here, then. Let me think about how best to structure this!
Aug 20, 2021, 5:30 PM
Exactly ! And at the same time making it “invisible” for the content editor in the end (the fact that this field is an array) he would just see the array contents as fields.
Basically this would allow me to have a starter in which (in a simplified way) I can have a page document with a field Content which is an array of modules,
so i can use previously coded modules, maybe add a few more then create few pages document on the fly and then “lock it” .

So in the end the content editor just see page1 with module1 as content page2 with module2 as content and so on.
Aug 20, 2021, 5:35 PM
Exactly ! And at the same time making it “invisible” for the content editor in the end (the fact that this field is an array) he would just see the item.
Basically this would allow me to have a starter in which (in a simplified way) I can have a page document with a field Content which is an array of modules,
so i can use previously coded modules, maybe add a few more then create few pages document on the fly and then “lock it” .

So in the end the content editor just see page1 with module1 as content page2 with module2 as content and so on.
Aug 20, 2021, 5:35 PM
Thanks a lot for answering !
Aug 20, 2021, 5:39 PM
any thought
user M
? 🙂
Aug 26, 2021, 9:03 AM
I've played around with this, and unfortunately, this will be a pretty complex thing to tackle. Basically, you would have to build a document that uses the Document Actions API to generate an Initial Value Template on publish, based upon the modules you've added to the document. It's not impossible, but it will take some heavy lifting on your end.
Aug 27, 2021, 5:04 PM
I've played around with this, and unfortunately, this will be a pretty complex thing to tackle. Basic, you would have to build a document that uses the Document Actions API to generate an Initial Value Template on publish, based upon the modules you've added to the document. It's not impossible, but it will take some heavy lifting on your end.
Aug 27, 2021, 5:04 PM
Thanks a lot for the insights, I’m glad to head you didn’t find a solution in minutes since I’ve spent quite some time working on that already 🙂.Support is awesome as usual !
Thanks Sanity Team !
Aug 30, 2021, 2:46 PM
And I did manage to find a solution for the time being with conditional fields, release came on point 🙂 !
Aug 30, 2021, 2:47 PM
Awesome! Conditional fields coming in handy already!
Aug 30, 2021, 5:43 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?