1

Trying to create node via JsonAPi. It contains a term field and text field. Managed to post text field without any issues but struggle to post values to term field. Anyone guide me to post values to term fields. format shared below

"data": {
    "type": "node--verification",
    "attributes": {
        "title": "My custom title"

       }
    "relationships": {
        "field_executing_agency": {
            "data":"BRO",
            "target_id": "1324"
        }

    }
}

1 Answer 1

0

In JSON:API, taxonomy term fields are handled like other entity reference fields.

Posting the data below will set the entity reference field field_entity_reference_taxonomy_term to an existing taxonomy term.

"data": {
    "type": "node--node-type",
    "attributes": {
        "title": "My custom title"
       }
    "relationships": {
        "field_entity_reference_taxonomy_term": {
            "type":"taxonomy-term-type-machine-name",
            "id": "taxonomy-term-uuid"
        }

    }
}

If you want to create a new taxonomy term and set the value of field_entity_reference_taxonomy_term to that, then you have two steps:

  1. POST the new taxonomy term.
  2. Update the node to set the value returned from the server when you POSTed the taxonomy term (the server returns the UUID in the response when it creates a new entity/term).

On a standard installation of Drupal, there's a nice UI for automatically creating taxonomy terms on the node edit page, but with decoupled Drupal, you have to build this yourself.

2
  • Thanks, Patrick. can you please tell me whether this field_entity_reference_taxonomy_term means field name and how to get that term type machine name
    – srini
    Mar 24, 2022 at 13:04
  • The field name under relationships is the machine name of the entity reference field. One easy way to find the name is to go to Structure / Content Types / My content type / Manage Fields and then click the entity reference field. The field machine name will be in the URL. An even easier way to get the name is to install the JSONAPI Extras or JSONAPI Explorer modules, but then you have another module. Explorer let’s you see the structure, and Extras let’s you rename stuff and disable unneeded items. Mar 24, 2022 at 13:53

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.