I have a View with a REST export display (image attached) that has some indexed field values from Search API.

The highlighted portion, authors, is a Paragraph field. Instead of the entity ID pointing to the Paragraph, how can I output a JSON object per delta with the field value?

Note, I am not using JSON:API (and cannot). Also, this is a Search API DB (and eventually Solr) based index, so 'Entity' mode cannot be used (and thus, no custom Normalizer).

What can I hook into, or what class can I create, to change how the Paragraph is being included in the result output? I thought about using a custom field formatter, but that seems like a lot of work just to output JSON data (classes, theme hooks, twig etc).

Example output

  • Not sure if it also applies to indexed field values but with regular fields I have successfully extended FormatterBase to create FieldFormatter drupal.org/docs/8/creating-custom-modules/… – GiorgosK Apr 30 '19 at 14:48
  • But what about returning JSON only, not a render array? – Kevin Apr 30 '19 at 14:51
  • While selecting the fields in your search index can't you keep expanding through the referenced entities until you find your specific fields you want to index. (I'm guessing you're currently just indexing the entity reference field for your author and not the fields on that author entity) Then once you've got those fields indexed they should be available within views to show as values (Again assuming your view is based on the search index entity type) – Leigh Apr 30 '19 at 14:54
  • Right, so, there must be a way then to tell a specific field how to be included to a view. I think I am looking for a field formatter that can return a JSON structure, or something similar. – Kevin Apr 30 '19 at 14:58
  • Ah I see sorry you want more than just 1 field from the authors paragraph to be displayed in the authors item. Could a twig tpl file for your views field work? Might be abit messy but looks like twig has a json_encode option twig.symfony.com/doc/2.x/filters/json_encode.html – Leigh Apr 30 '19 at 15:06

I thought about using a custom field formatter, but that seems like a lot of work just to output JSON data

No Code Solution:

Install Views Field View

Create a new View, "User view" in this case (does not to be a REST view).

Go to your Rest View

  • Add field of "View", select the view you added above.
    • For contextual filter, use the replacement pattern for your author's field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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