Fetch document field value in Structure Builder's S.view.options

1 replies
Last updated: Nov 1, 2021
I need to fetch a value in my Structurebuilder from a page. In this case, it’s which language the page has instead of having “en” as fixed, it should get the “language” value from that page. How would I go about doing it in this case? Is it possible to ad a GROQ query or something in
S.view.options
? The only value I can get from the `S.document()`seems to be the documentId, maybe somehow fetch the value dynamically using that ID?
export const getDefaultDocumentNode = (props) => {
  if (props.schemaType === "page") {
    return S.document().views([
      S.view.form().icon(FaEdit).title("Edit"),
      S.view
        .component(IframePreview)
        .options({ previewURL, selectedLanguage: "en" })
        .title("Preview")
        .icon(FaEye),
    ]);
  }
};
AI Update

Good news! You can absolutely access the document's data in your custom view component, including the language field. The key is that while getDefaultDocumentNode only receives the document ID, your custom view component (in this case IframePreview) receives much more detailed props, including the actual document data.

Solution: Access Document Data in Your Component

Your IframePreview component receives a document prop that contains the current document's data, including the displayed version. You can access the language field directly from there:

// IframePreview.js
export function IframePreview(props) {
  const { document, options } = props;
  
  // Access the language from the document's displayed version
  const language = document.displayed?.language || 'en';
  const previewURL = options.previewURL;
  
  return (
    <iframe 
      src={`${previewURL}?lang=${language}`}
      // ... other iframe props
    />
  );
}

Structure Builder Configuration

Your getDefaultDocumentNode setup stays simple - just pass the static options you need:

export const getDefaultDocumentNode = (props) => {
  if (props.schemaType === "page") {
    return S.document().views([
      S.view.form().icon(FaEdit).title("Edit"),
      S.view
        .component(IframePreview)
        .options({ previewURL }) // Just pass static config
        .title("Preview")
        .icon(FaEye),
    ]);
  }
};

Component Props Available

Your custom view component receives these useful props:

  • document.displayed - The current state of the document being edited (includes all fields like language)
  • document.draft - The draft version (if it exists)
  • document.published - The published version
  • documentId - The document's ID
  • schemaType - The schema type
  • options - Whatever you passed in .options()

The displayed version is particularly useful because it shows what the editor is currently looking at (draft if it exists, otherwise published).

Live Updates

The component will automatically re-render when the document changes, so if the editor changes the language field, your preview will update accordingly with the new language value.

This approach is much cleaner than trying to run GROQ queries in the Structure Builder configuration - you let Sanity handle the document data flow and just consume it in your component. The Component View documentation covers more about how custom document views work with document data in Sanity Studio.

Show original thread
1 reply
You can access information in the document inside of your
S.view.component
by using
document.displayed.<field>
. You could try passing that value to your 'selectedLanguage' option.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?