0

I would like to create content on another Drupal instance using JSON:API. Using the jsonapi_extras service, serializer.normalizer.entity.jsonapi_extras, entities can serialized but contain more node information than is required for remote location.

What I need to do is to prune this JSON object further to only include what is required, but haven't found an elegant way to do this with the EntityToJsonApi class. It seems like a workaround might be to request this JSON object from the HTTP API and limit included data with sparse fieldsets, but wanted to get some feedback on the best way to achieve this.

1

For consideration, an interesting way do this that was suggested to me by gabesullice, is to deny access to the fields. This also resulted a performance improvement in jsonapi 1.x, maybe 2.x as well.

/**
 * Implements hook_entity_field_access().
 */
function MYMODULE_entity_field_access($operation, $field_definition, $account, $items = NULL) {

  $request = \Drupal::request();
  if (strstr($request->getUri(), '/jsonapi/node/')) {
    $name = $field_definition->getName();
    switch ($name) {
      case 'field_blah':
      case 'uid':
      case 'moderation_state':
      case 'promote':
      case 'sticky':
      case 'unpublish':
      case 'review_date':
      case 'vid':
      case 'type':
      case 'revision_translation_affected':
      case 'revision_timestamp':
      case 'revision_log':
      case 'revision_id':
      case 'revision_uid':
      case 'langcode':
      case 'default_langcode':
        return AccessResult::forbidden();
    }
  }
  return AccessResult::neutral();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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