Discussion on using `initialValue` support for all `type`s and possibility of supporting `slug`-like `options.source` handling for `initialValue` fields.

8 replies
Last updated: Apr 29, 2021
Just noticed that
initialValue
support launched for all `type`s ๐ŸŽ‰ very very cool
Related question: is it possible (or on the roadmap) to support
slug
-like
options.source
handling for
initialValue
fields? Iโ€™d like to be able to, say, source a document field value for the
initialValue
of an
object
within the parent document.
Apr 28, 2021, 4:12 PM
Interesting question - maybe
user T
knows the answer to this
Apr 29, 2021, 6:25 PM
Super interesting question! I'm going to ask some folks on the dev team. Theoretically a bunch of things are possible, since you can give a function as an initialValue, but I don't know if there's any first-party support for grabbing a field off the document (like slug's options or preview's
select
...)
I'm working on expanding the initial value docs, so i'll explore this a bit, and ask around (either way, it's totally a use case I can see being helpful and i'll put it into some folks ears
๐Ÿ˜„ )
Apr 29, 2021, 6:36 PM
Thanks for the responses ๐Ÿ™‚
Example use case: Iโ€™d like to manage a blog article โ€œheroโ€ section
blogPost.title
separately from the SEO
blogPost.seoTitle
to allow for slight differences. But Iโ€™m thinking it could be cool for a user to enter a
blogPost.title
and it would prefill
seoTitle
field as a default.
Apr 29, 2021, 6:41 PM
Ahhh, that might not end up working out with initial values. Initial Values only come into play upon a new document creation
We can get that done in a couple other ways, though!

You could manage this with a conditional in your query. I'd have to play a bit to get the exact query but it would look something like this:

*[_type == "blogpost"] {
   ...,
   // GROQ conditional if seoTitle is blank, then set it to title
   !defined(seoTitle) => {
      "seoTitle":  title     
   }
}
You could also create a custom input with a button to generate the text (like the slug input would do).

I think handling it in the data layer makes the most sense, but either way should work
Apr 29, 2021, 6:49 PM
Ahhh, that might not end up working out with initial values. Initial Values only come into play upon a new document creation
We can get that done in a couple other ways, though!

You could manage this with a conditional in your query. I'd have to play a bit to get the exact query but it would look something like this:

*[_type == "blogpost"] {
   ...,
   // GROQ conditional if seoTitle is blank, then set it to title
   !defined(seoTitle) => {
      "seoTitle":  title     
   }
}
You could also create a custom input with a button to generate the text (like the slug input would do).

I think handling it in the data layer makes the most sense, but either way should work
Apr 29, 2021, 6:49 PM
You could also create a custom input with a button to generate the text (like the slug input would do).
Yeah was thinking I might just go this route ๐Ÿ˜› thanks for the thoughts!
Apr 29, 2021, 6:50 PM
You could also create a custom input with a button to generate the text (like the slug input would do).
Yeah was thinking I might just go this route ๐Ÿ˜› thanks for the thoughts!
Apr 29, 2021, 6:50 PM
You might also look at a Custom Document Action: https://www.sanity.io/docs/document-actions#362c883e4421
On publish -> check if seoTitle exists -> if not, set it to title value
Apr 29, 2021, 6:52 PM

Sanityโ€“ build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?