EntityReferenceFieldDiffParser.php in Entity Share 8.3
File
modules/entity_share_diff/src/Plugin/DiffGenerator/EntityReferenceFieldDiffParser.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_diff\Plugin\DiffGenerator;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\entity_share\EntityShareUtility;
use Drupal\entity_share_diff\DiffGenerator\DiffGeneratorPluginBase;
class EntityReferenceFieldDiffParser extends DiffGeneratorPluginBase {
public function build(FieldItemListInterface $field_items, array $remote_field_data = []) {
$result = [];
if (!$this
->getRemote()) {
foreach ($field_items as $field_key => $field_item) {
if (!$field_item
->isEmpty()) {
if ($field_item->entity) {
$entity = $field_item->entity;
if ($this->entityParser
->referenceEmbeddable($entity
->getEntityTypeId()) && $this->entityParser
->validateNeedToProcess($entity
->uuid(), FALSE)) {
$result[$field_key] = $this->entityParser
->prepareLocalEntity($entity);
}
else {
$result[$field_key] = $entity
->label() . ' (' . $entity
->uuid() . ')';
}
}
}
}
}
elseif (!empty($remote_field_data['data'])) {
$detailed_response = $this->remoteManager
->jsonApiRequest($this
->getRemote(), 'GET', $remote_field_data['links']['related']['href']);
$entities_json = Json::decode((string) $detailed_response
->getBody());
if (!empty($entities_json['data'])) {
$data = EntityShareUtility::prepareData($entities_json['data']);
}
else {
$data = [];
}
foreach ($remote_field_data['data'] as $field_key => $remote_item_data) {
$uuid = $data[$field_key]['id'];
list($referenced_entity_type, ) = explode('--', $remote_item_data['type']);
if ($this->entityParser
->referenceEmbeddable($referenced_entity_type) && $this->entityParser
->validateNeedToProcess($uuid, TRUE)) {
$result[$field_key] = $this->entityParser
->prepareRemoteEntity($data[$field_key], $this
->getRemote());
}
elseif (isset($data[$field_key])) {
$public_title_key = $this->entityParser
->getPublicFieldName('title', $data[$field_key]);
$title = $data[$field_key]['attributes'][$public_title_key] ?? '';
$uuid = $remote_item_data['id'];
$result[$field_key] = "{$title} ({$uuid})";
}
}
}
return $result;
}
}