Auto-filling fields in Sanity schemas and the complexity of implementing it.
38 replies
Last updated: Feb 1, 2022
N
Help, I would like to know if I can auto-fill another field in the same schema, for example, from title to subtitle:
is it possible?
{ fields: [ { name: 'title', type: 'string', title: 'Title', }, { name: 'subtitle', title: 'SubTitle (Recommended)', type: 'string', }, ] }
Jan 31, 2022, 7:22 PM
N
Oh!, I will look at it š looks like it could do what I need.
Jan 31, 2022, 8:15 PM
N
thanks.
Jan 31, 2022, 8:15 PM
N
user M
by the way, do you know if it could work on sibling schemas? Letās say, I have one schema called postwhich has a title property and then the post also have a field called
metawhich it also contains a title, so my idea is to extract the title from post into the metaā¦ Is that possible with this approach?
Jan 31, 2022, 8:24 PM
N
I think that I could do it with
isUniquetaking a lookā¦
Jan 31, 2022, 8:25 PM
If they're exactly the same I wouldn't set a separate field in your schema for it. I'd just handle setting the meta title using the post title on your frontend.
Jan 31, 2022, 8:26 PM
N
No, they have some differences, for example:
TITLE | SITENAME
Jan 31, 2022, 8:28 PM
N
No, they have some differences, for example:
TITLE | SITENAME
Jan 31, 2022, 8:28 PM
If that's the case I'd use a slug field again. If the built in slugify function doesn't achieve what you need for that field you can override it by providing a function to your schema's
options.slugify. Pretty handy!
Jan 31, 2022, 8:30 PM
N
Nice!
Jan 31, 2022, 8:45 PM
N
user M
sorry for bothering you again š, but do you know how to make it reactive? I mean listening to the title to automatically fill the slug type?Jan 31, 2022, 8:46 PM
N
is that possible?
Jan 31, 2022, 8:46 PM
N
One thing that we donāt want is to press the button to generate the slug, and if possible, also hide the button.
Jan 31, 2022, 8:48 PM
It is! But you would need to create a custom document action that updates the
slugfield before publishing. Similar to this action .
Jan 31, 2022, 8:53 PM
Let me know if you're having trouble getting it set up and I can walk you through it.
Jan 31, 2022, 9:07 PM
Let me know if you're having trouble getting it set up and I can walk you through it.
Jan 31, 2022, 9:07 PM
N
š¬ looks a bit complicated to me, and I think it goes away of what I need, the idea is that when I type something in a title field the meta title also gets filled with the same data, itās basically fill to inputs using only the first one, but with document actions, it looks like I will not get it because it requires user interaction on the button.
Jan 31, 2022, 9:17 PM
N
Similar: <https://www.section.io/engineering-education/data-binding-with-angular /two-way-binding.gif>
Jan 31, 2022, 9:20 PM
N
without the button
Jan 31, 2022, 9:21 PM
As I said, a document action it the only way you can automatically set a field based off of the input in another field. It won't require any other input from an editor besides publishing the document.
Jan 31, 2022, 9:24 PM
N
Thanks š
Jan 31, 2022, 9:47 PM
M
You are overcomplicating things. If you want to have title and metaTitle, where metaTitle by default should use value from title but with the option for the author to overwrite it just add a description to the field: āIf left empty the title will be usedā.
And then in your app just check simple condition metaTitle || title. Thatās it job done. Simple solutions are the best
And then in your app just check simple condition metaTitle || title. Thatās it job done. Simple solutions are the best
Jan 31, 2022, 10:02 PM
V
user H
Speaking as someone who overthinks everything, the benefit of a simple solution is you can always overcomplicate it later šJan 31, 2022, 10:23 PM
N
user H
I would love to say this to the client, it will not want to have not an overcomplicated solution of course, but something simple, the SEO section with its fields are at the end of the post creation page, they donāt want to fill almost the same thing twice. I know that another thing is to hidethat section of SEO and handle it from the client side, but that will need to be discussed with the client.
Jan 31, 2022, 10:27 PM
M
Client/business is not always right and developers are not only to implement solutions but be a voice of sense. They do not understand technology, we do
Jan 31, 2022, 10:29 PM
M
Imagine a situation when you fill the metaTitle automatically based on title. But then the business wants to overwrite it to sometjing different. Imagine now they changed the title again. Should the metatitle overwrite automatically again? How would you mark which filed was changed and which not?
Jan 31, 2022, 10:30 PM
N
I agree with this, sadly š
Jan 31, 2022, 10:30 PM
M
Imagine a situation when you fill the metaTitle automatically based on title. But then the business wants to overwrite it to sometjing different. Imagine now they changed the title again. Should the metatitle overwrite automatically again? How would you mark which filed was changed and which not?
Jan 31, 2022, 10:30 PM
M
If metaTitle is empty it means the title will be used instead/ If there is a value it means it was overwritten and that value will be used. Simple, cheap, what else could you possible want?
Jan 31, 2022, 10:32 PM
N
As a developer I could make it possible, and I think everyone here also knows how to do it, but I am new in Sanity and I donāt know too much about it, so I expected it would be a bit straightforward.I appreciate your input by the way, donāt get me wrong. I will talk with the client tomorrow about this requirement.
Jan 31, 2022, 10:35 PM
N
If metaTitle is empty it means the title will be used insteadThe thing is that the client wants to see this in our cms, in the client will be okay, but he wants it on the cms.
Jan 31, 2022, 10:36 PM
M
sure thing, the point is it doesnāt matter if thatās possible or not, itās too complex, and the complexity doesnāt bring any value, quite opposite it just makes authoring complex and unclear
Jan 31, 2022, 10:37 PM
To be fair,
user M
, I use this method and it's one of the most complicated pieces for developers new to Sanity. It really IS a lot of code just for one thing. I've also used a listener, too.Jan 31, 2022, 11:42 PM
To be fair,
user M
, I use this method and it's one of the most complicated pieces for developers new to Sanity. It really IS a lot of code just for one thing. I've also used a listener, too.Jan 31, 2022, 11:42 PM
user L
, yep, it's up there with custom input components in terms of difficulty grasping for new Sanity folks. Hence, my offer to help set it up!Jan 31, 2022, 11:49 PM
N
user M
thank you for your help, I appreciated, I finally followed your suggestions and itās working now.The thing is that I didnāt know how Sanity works, I read more the docs and finally using custom components made it.user H
thanks for your input.Feb 1, 2022, 12:08 PM
N
user M
thank you for your help, I appreciated, I finally followed your suggestions and itās working now.The thing is that I didnāt know how Sanity works, I read more the docs and finally using custom components made it.import React from 'react'; import {FormField} from '@sanity/base/components'; import {TextInput} from '@sanity/ui'; import { useId } from '@reach/auto-id'; import PatchEvent, {set, unset} from '@sanity/form-builder/PatchEvent'; import {withDocument} from 'part:@sanity/form-builder'; const MetaTitleTextField = React.forwardRef((props, ref) => { const inputId = useId(); const handleChange = React.useCallback((event) => { const inputValue = event.currentTarget.value; props.onChange(PatchEvent.from(inputValue ? set(inputValue) : unset())); }, [props.onChange]); const customValue = props.value || `${props.document.title} | COMPANY TITLE`; return ( <FormField title={props.type.title} description={props.type.description} __unstable_markers={props.markers} __unstable_presence={props.presence} compareValue={props.compareValue} inputId={inputId} > <TextInput id={inputId} onChange={handleChange} value={customValue} readOnly={props.readOnly} placeholder={props.placeholder} onFocus={props.onFocus} onBlur={props.onBlur} ref={ref} /> </FormField> ); }); export default withDocument(MetaTitleTextField);
user H
thanks for your input.Feb 1, 2022, 12:08 PM
user T
for sure, and as user L
said, it's hard to tell what's going on there. Glad you got it working!Feb 1, 2022, 3:54 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.