I can't find any documentation for creating custom field enhancers.
I was looking at creating one that would change the photo reference to the url of the photo. I don't see any clear documentation and have been attempting to create something like this just by looking at the source code but it's proving challenging.
Some documented examples/tutorials/guidelines for creating these in your own module would be helpful!
Comments
Comment #2
e0ipso@kier.heyl very much agreed! I don't have the time to write the documentation. Is that something you'd want to give a try?
Maybe you can paste the text here and I can move to the official documentation later.
Comment #3
kier.heyl commentedI don't know if I have the expertise to document it myself. Here's my first crack at adding a url to the data variables for my image field. It'd not working at all though. I feel like I'm fundamentally not understanding something.
I tried doing a flat string replacement prior to this but it had really weird results where various fields inside the data were just the first letter of that string. Here I tried adding the url field and it didn't seem to add at all.
Comment #4
kier.heyl commentedSo after swapping a couple of things around I ended up with this:
I don't know why it works when applying the new field to the meta sub array and not to the base. I'm also not sure why doTransform and doUndoTransform seem to be backwards but it works. Any insights as to what I'm missing here?
Comment #5
ricky.bertram commented@kier.heyl
See: https://jsonapi.org/format/#document-resource-identifier-objects - the data structure must adhere to the jsonapi specification
Comment #6
jeffreysmattsonIs there documentation for field enhancers? Really trying to figure out how to apply one created by the JSON:API Extras module. It doesn't document where the enhancers are or how to apply them. Is it all code... is it in the GUI?
Comment #7
jeffreysmattsonHopefully saving some others time and frustration here. "Field Enhancers" are located here:
admin/config/services/jsonapi/resource_types
They are on a page called "Resource Overrides"... click on a "Overwrite" button, then click on advanced, the on the left of a drop down you will see the word "Enhancer". This is the only location where the word "enhancer" is used.
Comment #8
woldtwerk commentedWhat do I need to do to write my own enhancer in a custom module? Specifically how do I register them?
my plugin was not picked up because of some weird php cache issue ...
Comment #9
ptmkenny commented@woldtwerk Custom field enhancers should be saved as
/my_module/src/Plugin/jsonapi/FieldEnhancer/MyFieldEnhancer.php.After that, rebuilding the Drupal cache (drush cr) should cause the field enhancer to be picked up automatically.
Comment #10
anybodyComment #11
ndewhurstI successfully set up an enhancer just now by referencing the other ones in code and reading this issue. I just wanted to second the observation in #4 that
doTransformanddoUndoTransformseem to be named the opposite of what they do.doUndoTransformis where you will want to modify the data for API output. I didn't dig around too much to figure out why, but I guess it has to do with the more general intent ofShaper\DataAdaptor\DataAdaptorTransformerTraitfrome0ipso/shaper, wheredoTransformis meant to transform "incoming" data, anddoUndoTransformis meant to perform the inverse transformation on "outgoing" data. Our use cases in this issue are all for outgoing data. At least that's my interpretation.