🇪🇸Spain @mrjey

Sevilla
Account created on 4 November 2020, over 4 years ago
#

Recent comments

🇪🇸Spain mrjey Sevilla

In case you want to keep the incoming format, you just has to modify the patch:

    /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = null, array $context = []): mixed {
    if ($format == 'api_json' && $class == 'Drupal\custom_field\Plugin\DataType\CustomFieldEntityReference') {
      return ['id' => $data['id'], 'entity' => \Drupal::service('entity.repository')->loadEntityByUuid('node', $data['id'])];
    }
    return parent::denormalize($data, $class, $format, $context);
  }

And the payload in JSON:API request would be:

"field_custom": [
{
"my_custom_field_entity_reference": {"id: "2a5033b4-9105-4fae-973d-d4255c0ee834"},
"other_custom_field_entity_reference": {"id: "e010a93b-ffd2-4f3d-ae68-f67ed8bb3fa7"}
}
],

🇪🇸Spain mrjey Sevilla

You can patch the file custom_field/src/Normalizer/EntityReferenceNormalizer.php adding the following function:

    /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = null, array $context = []): mixed {

    if ($format == 'api_json' && $class == 'Drupal\custom_field\Plugin\DataType\CustomFieldEntityReference') {
        return ['id' => $data, 'entity' => \Drupal::service('entity.repository')->loadEntityByUuid('node', $data)];
    }

    return parent::denormalize($data, $class, $format, $context);
  }  

When invokin JSON:API just set the field with the UUID, i.e:

"field_custom": [
{
"my_custom_field_entity_reference": "2a5033b4-9105-4fae-973d-d4255c0ee834",
"other_custom_field_entity_reference": "e010a93b-ffd2-4f3d-ae68-f67ed8bb3fa7"
}
],

Production build 0.71.5 2024