Using onChange and PatchEvent with custom-asset-sources in Sanity

4 replies
Last updated: Sep 19, 2022
Hello! πŸ‘‹
Is it possible when using
custom-asset-sources to be also able to update other parts of the document? It looks like that can be done using
onChange
&
PatchEvent
but I'm not seeing
onChange
passed to my plugin. Any thoughts?
AI Update

The short answer is that custom asset sources don't receive onChange or PatchEvent props - they work differently than custom input components.

How Custom Asset Sources Work

According to the Asset Source API documentation, custom asset sources are designed specifically for selecting and uploading assets. They receive a different set of props focused on asset selection:

Asset source selection component props:

  • onSelect(Asset[]) - Called when assets are selected. The Studio automatically handles uploading the asset(s)
  • onClose() - Called when the selection dialog is closed
  • selectionType - Either 'single' or 'multiple'
  • selectedAssets - Array of currently selected Sanity asset documents
  • assetType - Either 'file' or 'image'
  • dialogHeaderTitle - Optional header component

Why You Can't Update Other Document Fields

Custom asset sources are scoped to asset selection only. They don't have access to the document's form context or onChange handlers because they operate at a different level - they're specifically for browsing and selecting assets from external sources, not for modifying document fields.

Alternative Approaches

If you need to capture additional metadata (like a caption) when selecting an asset, you have a few options:

  1. Use the asset's built-in metadata fields - Sanity assets support fields like title, description, source, originalFilename, and custom metadata that can be set on the asset document itself.

  2. Custom input component instead - If you need to update multiple fields simultaneously, you should create a custom input component rather than a custom asset source. Custom input components do receive onChange and can use PatchEvent to update multiple fields, including using real-time safe patches with absolute paths.

  3. Post-selection handling - Handle the additional data collection after the asset is selected, perhaps through document actions or a custom form component that wraps the image field.

Example Reference

The community thread you found mentioned looking at the OG Image creator plugin as a reference. While this plugin shows asset source patterns, it's also limited to the onSelect interface for returning assets.

If you need to modify other parts of the document when an asset is selected, you'll likely need to rethink your approach - either by using custom input components that have full form access, or by handling the additional data separately from the asset selection flow.

Show original thread
4 replies
What exactly are you trying to do? πŸ™‚
I have a plugin that is following the Custom Asset Source steps to open a dialog with an existing image modify it, and re-save it. Along with modifying it, I want to be able to have a text input that captures a caption for that image. When a user is done, I'd like them to be able to save the modified image & the caption. Currently I'm only able to pass the image back via
onSelect
.
You should def look into the OG Image creator plugin
user B
created! There might be very useful things for you in there!
Thanks for pointing that out! I'll take a look!

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?