Closed (fixed)
Project:
JSON:API
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
4 Jan 2017 at 09:00 UTC
Updated:
5 Feb 2019 at 06:54 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
e0ipsoPlease see how `jsonapi_entity_base_field_info` adds a computed property to the `file` entities. That should do.
If you don't want to create a *virtual* field, you can add a normalizer that only acts for that specific content type as a service. You'll need to use a higher priority than `serializer.normalizer.entity.jsonapi` (set to 21). Your custom normalizer can inherit from `Drupal\jsonapi\Normalizer\ContentEntityNormalizer` and add your custom stuff in `getFields`.
Comment #3
wim leersIt's the Flag module's responsibility to provide a normalizer (which would work both fore
restandjsonapi).Comment #5
danberk4 commentedI'm trying to implement e0ipso's solution by adding a normalizer, but i keep getting the error below. Any suggestions?
Recoverable fatal error: Argument 1 passed to Drupal\my_api\Normalizer\EventNodeEntityNormalizer::__construct() must be an instance of Drupal\jsonapi\LinkManager\LinkManager, instance of Drupal\Core\Entity\EntityManager given,
My code
Comment #6
danberk4 commentedFixed my issue. Make sure you are returning the following arguments in your normalizer service
arguments: ['@jsonapi.link_manager', '@jsonapi.resource_type.repository', '@entity_type.manager']
Comment #7
e0ipsoThanks for taking the time to report back to the issue queue.
Comment #8
jorisInsign commentedHi @e0ipso,
I found your solution with hook_entity_base_field_info().
It work great for a single value.
But I can't figure how to make it work for multi-values...
Here is my tests files
my_module.module :
and my MyFields.php :
But at the end, I only got first value for the second field... :
Can you point me my errors ? or if you have a guide somewhere for this purpose ? I can't find any documentation/tutorial about this...
Thanks !
Comment #9
brownrl commentedThe computed field solution in the end for our app covered 100% of what we needed. What we do here is take all the file or image fields and make computed fields to expose direct urls for download or display. With some clever code you can get to the point where you just disable the file endpoint. The normalizer route also works but if you need to go that far we agreed here that it would just be best to make a custom json/resource.
To get the multi value thing to work I had to play with the "cardinality" of the computed field.
Then the system says "Ok, this can spit out more than one value."
Comment #10
e0ipsoIt's great to hear that the recommendations are proving to be useful.
One friendly reminder, the recommendation is to use JSON RPC methods (using the Drupal module) instead of custom endpoints.
Comment #11
ash2303 commentedI have been trying to add custom data (like edit link) in JSON API response as suggested in #5. As parent is returning object of type EntityNormalizerValue and it is not possible to add field/custom value to it. Any help is most welcome. I am attaching my code for ContentEntityNormalizer.
Comment #12
jiisuominen commentedHey!
I'm trying to add user's roles directly to the User entity, because of simplicity in our app, but also because I'm having some issues with roles -relationships..
I'm getting some mixed results, and can't find straightforward documentation for this use case.
NULL values to be exact:
So I'm guessing there's some normalizing happening that I cannot figure out. I can see the values with debugger in PHPSTORM.
Anyways, here's my field definition:
The configuration of this field was/is pretty much a guessing game for me, so there's probably something wrong here, but I just can't figure out correct combo for computed field. I started with only setComputed -value in the base field definition and added line by line to see some different results. Nulls are all I get with these..
And here's my method for the values:
I pretty much copied my code from this article.
Cheers,
Janne
Comment #13
jiisuominen commentedJust came to say that one needs to check that all enhancers are off when you try to develop custom stuff. So yeah, we had JSON field enhancer turned on for the computed field and that nulled all the values. Makes sense.
Cheers,
Janne
Comment #14
ash2303 commentedI was able to add computed field. But I was specifically looking to alter jsonapi response.
Thanks,
Ashish