JSON:API FieldEnhancers – removing a resource object from a JSON:API response

I need to remove a resource object (in this case a paragraph) from a JSON:API response when it matches a specific condition. I do not want to delete it, I just want to exclude the object from the response.

Approach: I tried using a FieldEnhancer to access the resource object in "doUndoTransform()", check for the condition and skip/remove it before being displayed.

Problem: So far I only succeeded in altering the data of my resource object. I’m able to manipulate its content but I haven’t yet figured out how to exclude it from the output.

Question: Is there any way or probably an alternative (to the FieldEnhancer approach) to achieve my goal? I thought about extending the schema in "getOutputJsonSchema()" but due to a lack of documentation I’m missing the possible options.

Setup:

Article (node) | List (paragraph) | Items | Item (paragraph containing my custom FieldGroup with criteria) (see below)

My FieldEnhancer is attached to List looping over each Item in Items to retrieve the entity ID and the condition. The custom FieldGroup which holds the condition can be attached to any entity so using a FieldEnhancer on a specific resource is also pretty fast.

FieldEnhancer code example:

/**  * {@inheritdoc}  */ protected function doUndoTransform($data, Context $context): mixed {     $entityId = $this->getEntityId($context);     // if ok display object     if ($this->fieldGroupProcessor->isCondition($entityId)) {        return $data;     }      // else return null / skip / remove object }  public function getOutputJsonSchema(): array {     return [         'type' => 'object',         'properties' => [],     ]; } 

JSON:API resource object example:

{ type: "paragraph--quicklinks", id: "3eabd16d-92a5-468d-9968-ad7e24797340", links: {     self: {         href: "http://127.0.0.1:8088/jsonapi/paragraph/quicklinks/3eabd16d-92a5-468d-9968-ad7e24797340"     } }, attributes: {     heading: "Test" }, relationships: {     quicklinks_items: {         data: [{             type: "paragraph--quicklink_item",             id: "af04ef25-bee5-401d-b5f7-1bf742349dde",             meta: {                 target_revision_id: 230829,                 drupal_internal__target_id: 36089             }         },         {             type: "paragraph--quicklink_item",             id: "0831315b-f6bd-46a4-87bf-feff5c9473bc",             meta: {                 target_revision_id: 230831,                 drupal_internal__target_id: 36091             }         },         {             type: "paragraph--quicklink_item",             id: "53b2a7d2-4388-4e5f-8681-a91cbe0e07c7",             meta: {                 target_revision_id: 230833,                 drupal_internal__target_id: 36301             }         }],         links: {             related: {                 href: "http://127.0.0.1:8088/jsonapi/paragraph/quicklinks/3eabd16d-92a5-468d-9968-ad7e24797340/quicklinks_items"             },             self: {                 href: "http://127.0.0.1:8088/jsonapi/paragraph/quicklinks/3eabd16d-92a5-468d-9968-ad7e24797340/relationships/quicklinks_items"             }         }     } } 

},

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

JSON:API FieldEnhancers – removing a resource object from a JSON:API response

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.