0

I'm trying to create a node on a Drupal 8 site via REST. Passing all the values to create the fields work fine apart from the field that is an entity reference field. The field is an entity reference to Drupal users. Drupal creates the node, there is no validations errors, but the entity reference field has no value. I've tried changing 'target_id' to 'value', same thing, the node gets created without errors but no value is given to the user entity reference field.

Any ideas?

$serialized_entity = json_encode([
    'title' => array(
      'value' => 'some_title',
    ),
    'type' => array(
      'target_id' => 'node_type',
    ),
    '_links' => array(
      'type' => array(
        'href' => $base_url . '/rest/type/node/node_type',
      ),
    ),
    'entity_reference_field' => array(
      'target_id' => $entity_id, // This does not work. Using a valid uid.
    ),
    'normal_field' => array(
      'value' => 'some_data',
    ),
]);
2
  • So when you mean "uid" you mean like $entity_id = 5 right not like $entity_id = someUUID because "field_image": [{ "target_id": "5"}] does work. also are you using core rest aka POST /entity/node?_format=json ??
    – taggartJ
    Commented Jan 20, 2019 at 23:53
  • Sorry, I meant entity id (the value of $entity->id()). I am using the core REST but it's /node?_format=json Commented Jan 21, 2019 at 9:47

1 Answer 1

0

I managed to get working by changing this...

'entity_reference_field' => array(
  'target_id' => $entity_id,
),

to this...

'entity_reference_field' => array(
  array('target_id' => $entity_id),
),

I also had to disable using an entity reference view as the 'Reference type' on the field and just have the option as 'Default'. Not sure if this is a bug or if there's something wrong with my entity reference view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.