createFromEntityReferenceField example

$value['title'] = 'My alter ego';
          break;

        case 'image2':
          $value['alt'] = 'Adorable llama';
          $value['title'] = 'My spirit animal 😍';
          break;
      }
      return $value;
    }$entity_property_names);
    $resource_object = ResourceObject::createFromEntity($this->referencingResourceType, $this->referencer);
    $relationship = Relationship::createFromEntityReferenceField($resource_object$resource_object->getField($field_name));
    // Normalize.     $actual = $this->normalizer->normalize($relationship, 'api_json');
    // Assert.     assert($actual instanceof CacheableNormalization);
    $this->assertEquals($expected$actual->getNormalization());
  }

  /** * Data provider for testNormalize. */
  public function normalizeProvider() {
    
    // restrictions.     if ($field instanceof FieldItemListInterface) {
      $field_access_result = $field->access('view', $context['account'], TRUE);
      if (!$field_access_result->isAllowed()) {
        return new CacheableOmission(CacheableMetadata::createFromObject($field_access_result));
      }
      if ($field instanceof EntityReferenceFieldItemListInterface) {
        // Build the relationship object based on the entity reference and         // normalize that object instead.         assert(!empty($context['resource_object']) && $context['resource_object'] instanceof ResourceObject);
        $resource_object = $context['resource_object'];
        $relationship = Relationship::createFromEntityReferenceField($resource_object$field);
        $normalized_field = $this->serializer->normalize($relationship$format$context);
      }
      else {
        $normalized_field = $this->serializer->normalize($field$format$context);
      }
      assert($normalized_field instanceof CacheableNormalization);
      return $normalized_field->withCacheableDependency(CacheableMetadata::createFromObject($field_access_result));
    }
    else {
      // @todo Replace this workaround after https://www.drupal.org/node/3043245       // or remove the need for this in https://www.drupal.org/node/2942975.

  public function getRelationship(ResourceType $resource_type, FieldableEntityInterface $entity$related, Request $request$response_code = 200) {
    /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */
    $field_list = $entity->get($resource_type->getInternalName($related));
    // Access will have already been checked by the RelationshipRouteAccessCheck     // service, so we don't need to call ::getAccessCheckedResourceObject().     $resource_object = ResourceObject::createFromEntity($resource_type$entity);
    $relationship = Relationship::createFromEntityReferenceField($resource_object$field_list);
    $response = $this->buildWrappedResponse($relationship$request$this->getIncludes($request$resource_object)$response_code);
    // Add the host entity as a cacheable dependency.     if ($response instanceof CacheableResponseInterface) {
      $response->addCacheableDependency($entity);
    }
    return $response;
  }

  /** * Adds a relationship to a to-many relationship. * * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * The base JSON:API resource type for the request to be served. * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The requested entity. * @param string $related * The related field name. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * * @return \Drupal\jsonapi\ResourceResponse * The response. * * @throws \Drupal\jsonapi\Exception\EntityAccessDeniedHttpException * Thrown when the current user is not allowed to PATCH the selected * field(s). * @throws \Symfony\Component\HttpKernel\Exception\ConflictHttpException * Thrown when POSTing to a "to-one" relationship. * @throws \Drupal\Core\Entity\EntityStorageException * Thrown when the underlying entity cannot be saved. * @throws \Drupal\jsonapi\Exception\UnprocessableHttpEntityException * Thrown when the updated entity does not pass validation. */
Home | Imprint | This part of the site doesn't use cookies.