You are here

public function JsonapiHelper::updateRelationships in Entity Share 8.2

Same name and namespace in other branches
  1. 8 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper::updateRelationships()

Create or update the entity reference field values of an entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to update.

array $data: An array of data.

Overrides JsonapiHelperInterface::updateRelationships

1 call to JsonapiHelper::updateRelationships()
JsonapiHelper::importEntityListData in modules/entity_share_client/src/Service/JsonapiHelper.php
Use data from the JSON:API to import content.

File

modules/entity_share_client/src/Service/JsonapiHelper.php, line 295

Class

JsonapiHelper
Class JsonapiHelper.

Namespace

Drupal\entity_share_client\Service

Code

public function updateRelationships(ContentEntityInterface $entity, array $data) {
  if (isset($data['relationships'])) {
    $resource_type = $this->resourceTypeRepository
      ->get($entity
      ->getEntityTypeId(), $entity
      ->bundle());

    // Reference fields.
    foreach ($data['relationships'] as $field_name => $field_data) {
      $field_name = $resource_type
        ->getInternalName($field_name);
      $field = $entity
        ->get($field_name);
      if ($this
        ->relationshipHandleable($field)) {
        $field_values = [];

        // Check that the field has data.
        if ($field_data['data'] != NULL && isset($field_data['links']['related']['href'])) {
          $referenced_entities_response = $this->requestService
            ->request($this
            ->getHttpClient(), 'GET', $field_data['links']['related']['href']);
          $referenced_entities_json = Json::decode((string) $referenced_entities_response
            ->getBody());

          // $referenced_entities_json['data'] can be null in the case of
          // missing/deleted referenced entities.
          if (!isset($referenced_entities_json['errors']) && !is_null($referenced_entities_json['data'])) {
            $referenced_entities_ids = $this
              ->importEntityListData($referenced_entities_json['data']);
            $main_property = $field
              ->getItemDefinition()
              ->getMainPropertyName();

            // Remove the missing entities from the array to avoid key
            // mismatch.
            $prepared_data = [];
            foreach (EntityShareUtility::prepareData($field_data['data']) as $field_value_data) {
              if ($field_value_data['id'] !== 'missing') {
                $prepared_data[] = $field_value_data;
              }
            }

            // Add field metadatas.
            foreach ($prepared_data as $key => $field_value_data) {

              // When dealing with taxonomy term entities which has a
              // hierarchy, there is a virtual entity for the root. So
              // $referenced_entities_ids[$key] may not exist.
              // See https://www.drupal.org/node/2976856.
              if (isset($referenced_entities_ids[$key])) {
                $field_value = [
                  $main_property => $referenced_entities_ids[$key],
                ];
                if (isset($field_value_data['meta'])) {
                  $field_value += $field_value_data['meta'];
                }

                // Allow to alter the field value with an event.
                $event = new RelationshipFieldValueEvent($field, $field_value);
                $this->eventDispatcher
                  ->dispatch(RelationshipFieldValueEvent::EVENT_NAME, $event);
                $field_values[] = $event
                  ->getFieldValue();
              }
            }
          }
        }
        $entity
          ->set($field_name, $field_values);
      }
    }

    // Save the entity once all the references have been updated.
    $entity
      ->save();
  }
}