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
E
Just noticed that
Related question: is it possible (or on the roadmap) to support
initialValuesupport launched for all `type`s ๐ very very cool
Related question: is it possible (or on the roadmap) to support
slug-like
options.sourcehandling for
initialValuefields? Iโd like to be able to, say, source a document field value for the
initialValueof an
objectwithin the parent document.
Apr 28, 2021, 4:12 PM
E
Interesting question - maybe
user T
knows the answer to thisApr 29, 2021, 6:25 PM
B
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
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
๐ )
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
E
Thanks for the responses ๐
Example use case: Iโd like to manage a blog article โheroโ section
Example use case: Iโd like to manage a blog article โheroโ section
blogPost.titleseparately from the SEO
blogPost.seoTitleto allow for slight differences. But Iโm thinking it could be cool for a user to enter a
blogPost.titleand it would prefill
seoTitlefield as a default.
Apr 29, 2021, 6:41 PM
B
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:
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
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 } }
I think handling it in the data layer makes the most sense, but either way should work
Apr 29, 2021, 6:49 PM
B
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:
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
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 } }
I think handling it in the data layer makes the most sense, but either way should work
Apr 29, 2021, 6:49 PM
E
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
E
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
B
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
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.