How to Use JSON API POST Request to Create Custom Entity with Metatag

Created on 10 July 2023, over 1 year ago

Problem/Motivation

Hello! I have two websites. I am trying to create a custom entity with a standard metatag field using JSON API POST request called from one site (the source) to the other (the endpoint). Both have the same "coh_event" node with the same exact structure. Everything works except I cannot get the metatags of the coh_event entity to be created using the POST request

Steps to reproduce

I have a custom entity named coh_event, with a standard metatag field named field_coh_metatag

From the source drupal site, I am calling POST on localclient.local to create a coh_event using the json api

$this->httpClient->request('POST', 'localclient.local/jsonapi/node/coh_event', [
'json' => $data, 'auth' => ... , 'headers' => ...]);

The $data is an array in json format. For readability, i've reproduced it in json format:

{
  "data": {
  "type": "node--coh_event",
  "attributes": {
    "field_coh_metatag": [
        {
            "tag": "meta",
            "attributes": {
                "name": "abstract",
                "content": "Test abstract"
            }
        }
     ], 
    "field_coh_body": {
        "value": "Test body",
        "format": "az_standard"
      },
     "field_coh_event_date": [
      {
          "value": "2023-07-12T21:00:00+00:00",
          "end_value": "2023-07-12T22:00:00+00:00",
          "timezone": ""
      }
      ...
}

Proposed resolution

I've tried installing patches (specifically Patch 172 and Patch 61) from here: https://www.drupal.org/project/metatag/issues/2945817 πŸ“Œ Support JSON API, REST, GraphQL and custom normalizations via new computed field Fixed
-> the focus of that issue seems to make Json API spit out the metatag in the correct format, but it doesn't say anything about POST request and creating entities with metatags

I've tried putting the metatag information in the actual "metatag" attribute in the json $data as well as in the "metatag_normalized" attribute (made available by the patches above) to no avail.

I've tried serializing like shown in this link: https://drupal.stackexchange.com/questions/230906/how-do-i-add-a-meta-ta...
-> I get this error: 500 Internal Server Error: The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "field_coh_metatag" field (field item class: Drupal\metatag\Plugin\Field\FieldType\MetatagFieldItem).
-> My code is:

    $data['attributes']['field_coh_metatag'] = array(
      0 => array('value' => serialize([
        'abstract' => 'test',
        'geo.region' => 'US-AZ',
        'content-language' => 'en',
      ]))
    );

TLDR

I want to know if it is possible to create a custom entity with a metatag field using a POST request via JSON API and if it is, what the right format for the body data should be to accomplish this. Much thanks in advance!!

πŸ’¬ Support request
Status

Active

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States kevinwlu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kevinwlu
  • πŸ‡ΊπŸ‡ΈUnited States kevinwlu

    Solution found:
    In the POST request json body, for the metatag field, enclose everything in "value" like this:

          "field_custom_metatags": {
            "value": {
              "abstract": "This is a page to test coh_event deployment. ",
              "content_language": "en",
              "geo_placename": "Tucson",
              "geo_position": "32.23298190321497; -110.95011213068365",
              "geo_region": "US-AZ",
              "icbm": "32.23298190321497, -110.95011213068365",
              "keywords": "test, coh deploy, college of humanities, event entity",
              "robots": "index, follow, noimageindex",
              "shortlink": "https://goo.gl/maps/qZGCcjVQxHzANEx19"
            }
          }
    
Production build 0.71.5 2024