I have an Article content type with an image field and some tags via a relation. I’ve created a custom field for my need. Here is what I would like to do when I create a new article:
I upload a new image
My custom field detect there is a new image and do some processing
The result of the processing fill the tags
Do you know if there is a way know the data of other fields? Can we fill them programmatically?
At the moment, in the custom field props I only have access to its attributes and data.
I could do vanilla javascript but I’m looking for the strapi way of doing it.
It depends on how you created the custom field. If you added the custom field by adding/overriding files in the ./extensions/content-manager/admin/src directory, then you can use the useDataManager hook from strapi-plugin-content-manager/admin/src/hooks/useDataManager.
modifiedData is essentially the current state of the form. I’ve been using this method to access other fields from a specific field’s input component. However, I’m now trying to implement my custom field as a local plugin using the Field API. Using the above method has required me to keep overridden files up to date with new releases, when essentially all I was doing was importing a custom component and putting some case statement around which input component to use. Very labor-intensive and prone to error.
I have not been able to figure out a way to access other fields or the EditViewDataManager context. I need to do something similar to your case where my input component is dependent on data from another field.
Are you using one of these approaches for your custom field, and have you had any luck?
I use a new custom field (so not an overridden field), I tried your method with the useDataManager and as you said it doesn’t work. It seems it creates a completely new Context rather than retrieving the existing one. My React skills are limited to be able to understand everything.
Yeah that’s what I ran into as well. I’m assuming the EditView provider isn’t wrapping custom Field components, which is pretty disappointing. I’ll keep poking around and maybe we can submit a feature request or a PR at some point.
I notice that modifiedData does not load all data. For example relations of a nested component will not be populated, until user toggle open that section. How can we populate field in admin console programmatically?
I was looking for the same thing and found out that this solution works - at least for simple fields. This is just a PoC code.
It is a very late answer but - maybe it will help someone
const {
modifiedData
} = useCMEditViewDataManager()
useEffect(() => {
console.log(modifiedData)
// HACK: since Strapi does not expose a way to do this nicely, we need to resort to this stuff :/
if (modifiedData.stock === 111) {
let el = document.getElementById("unit")
if (el) {
var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set
nativeInputValueSetter.call(el, 'test')
var inputEvent = new Event('input', { bubbles: true})
el.dispatchEvent(inputEvent)
}
}
}, [modifiedData])
Very good point. I simply did not know about this and documentation on this is lacking. I did see it but I really though that it is for another purpose.