5

I am using JSON API module to expose data using REST API. I want to alter its response. I am trying to altering it using hook_ENTITY_TYPE_load. If I change any existing property it shows the updated value. But if I try to add custom property it doesn't show the custom property in response. The two hooks I used are below:

function my_custom_entity_type_alter(array &$entity_types) {
  $entity_types['node']->set('flag_count', 10);
}

function my_custom_node_load($nodes) {
  foreach ($nodes as &$node) {
    $node->flag_count = 10;
  }
}

I need to know if there is any possible way to alter the json api response with custom properties?

3
  • Is flag_count a valid typed data property on the node? That might be removed by serialization.
    – mradcliffe
    Jan 2, 2017 at 13:42
  • Did you add your custom property as a custom (computed?) field of the node entity?
    – Eyal
    Jan 2, 2017 at 18:13
  • @mradcliffe yes I tried the hook according to Neograph734 answer. I guess you are right, JSON API is removing the property. Need to debug the code. Jan 3, 2017 at 5:43

3 Answers 3

4

Please 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.

4
  • Thanks hook_entity_base_field_info worked like a charm!! :) Jan 4, 2017 at 11:10
  • Hi e0ipso, did you actually manage to alter the output of jsonapi module creating a new normalizer? Jan 22, 2019 at 12:00
  • 3
    Things have changed quite a lot since this comment was written. Take a look at lullabot.com/articles/jsonapi-2
    – e0ipso
    Jan 23, 2019 at 12:43
  • Long story short, the comment above is referring to the fact that you cannot override field normalizers from JSON:API anymore. You will get an error about it because the module considers normalizers part of internal API. There is a dirty trick that JSON:API Extras uses to get around this for its purposes -- it places code in the JSON:API module namespace to dodge the check -- but this is not a recommended pattern because the internal API of JSON:API could change at any time.
    – GuyPaddock
    Jul 26, 2021 at 1:55
1

You can create your own custom normalizers to alter the JSON response.

Some good resources:

2
0

I suppose you should be using hook_entity_type_build rather than hook_entity_type_alter, which states:

Do not use this hook to add information to entity types, unless one of the following is true:

  • You are filling in default values.
  • You need to dynamically add information only in certain circumstances.
  • Your hook needs to run after hook_entity_type_build() implementations.

Use hook_entity_type_build() instead in all other cases.

And EntityTypeInterface states

Additional information can be provided by modules: hook_entity_type_build() can be implemented to define new properties, while hook_entity_type_alter() can be implemented to alter existing data and fill-in defaults.

You might want to try something like this instead:

function my_custom_entity_type_build(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  $entity_types['node']
    ->set('flag_count', 10);
}
1
  • Great. I can see the property in hook_ENTITY_TYPE_load but I guess JSON API removed this property. I need to debug some json api code itself. Jan 3, 2017 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.