FileFieldDiffParser.php in Entity Share 8.3
File
modules/entity_share_diff/src/Plugin/DiffGenerator/FileFieldDiffParser.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;
use Drupal\file\Entity\File;
class FileFieldDiffParser extends DiffGeneratorPluginBase {
public function build(FieldItemListInterface $field_items, array $remote_field_data = []) {
$result = [];
$fileManager = $this->entityTypeManager
->getStorage('file');
if (!$this
->getRemote()) {
foreach ($field_items as $field_key => $field_item) {
$values = $field_item
->getValue();
if (!$field_item
->isEmpty()) {
if (isset($values['target_id'])) {
$file = $fileManager
->load($values['target_id']);
if ($file instanceof File) {
$label = (string) $this
->t('File name');
$result[$field_key][$label] = $file
->getFilename();
$label = (string) $this
->t('File size');
$result[$field_key][$label] = $file
->getSize();
}
}
}
foreach ($this
->getFieldMetaProperties() as $key => $label) {
if (isset($values[$key])) {
$result[$field_key][$label] = $values[$key];
}
}
}
}
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 (array_keys($remote_field_data['data']) as $field_key) {
if ($data[$field_key]['attributes']['filename']) {
$label = (string) $this
->t('File name');
$result[$field_key][$label] = $data[$field_key]['attributes']['filename'];
$label = (string) $this
->t('File size');
$result[$field_key][$label] = (string) $data[$field_key]['attributes']['filesize'];
}
foreach ($this
->getFieldMetaProperties() as $key => $label) {
if (isset($remote_field_data['data'][$field_key]['meta'][$key])) {
$result[$field_key][$label] = $remote_field_data['data'][$field_key]['meta'][$key];
}
}
}
}
return $result;
}
protected function getFieldMetaProperties() {
return [
'description' => (string) $this
->t('Description'),
];
}
}