Score:1

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

de flag

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"
            }
        }
    }
}

},

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.