Score:0

create a child node via api

kr flag
Mat

Running Drupal 9.3, I have two content types, job and and Job detail. Every job has many job details.

Lets say i created a job with node ID of 5. I have a field in job detail called "field_job_id" that will take the node ID of the parent, in this case "5".

When creating a job detail node, i have tried inputting the node ID as a int, as a string and nothing works.

    "_links": {
    "type": {
      "href": "https://domain.com/rest/type/node/job_detail"
    }
  },
  "title": [
    {
      "value": filename
    }
  ],
  "field_job_id": [
    {
      "value": jobTitle
    }
  ],
  "field_document_type": [
    {
      "value": "PDF"
    }
  ],
  "field_author": [
    {
      "value": author
    }
  ],
  "field_remediation_cost": [
    {
      "value": "0"
    }
  ]
})
headers = {
  'Content-Type': 'application/hal+json',
  'Authorization': 'Basic HDHBdbdb83hcn8echw8fy389hfeuivbwe'
}

The node get posted but the field that should display the parent node is blank every time. Is there something i am doing wrong?

kr flag
Mat
ok...so it seems I need to add a "relationships" field to this but not quite sure how to do that.
Kevin avatar
in flag
Is job id an entity reference? Should it be target_id instead?
kr flag
Mat
@kevin Yes. it is a entity reference. I am very new to rest api so i will try that
kr flag
Mat
@Kevin I could not for the life of me get a relationship into a rest api. Do you have an example?
Score:1
kr flag
Mat

FINALLY figured it out. since I am dealing with a entity ref, that being "field_job_id" I had to add a field

  "field_job_id":[{"target_id":8}],

with 8 being the node of the job it is referencing

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.