You are here

public function FileFieldDiffParser::build in Entity Share 8.3

Builds an array of strings.

This method is responsible for transforming a FieldItemListInterface object into an array of strings. The resulted array of strings is then compared by the Diff component with another such array of strings and the result represents the difference between two entity fields.

Example of FieldItemListInterface built into an array of strings:

array(
  0 => "This is an example string",
  1 => "Field values or properties",
);

Parameters

\Drupal\Core\Field\FieldItemListInterface $field_items: Represents an entity field.

Return value

mixed An array of strings to be compared. If an empty array is returned it means that a field is either empty or no properties need to be compared for that field.

Overrides DiffGeneratorInterface::build

See also

\Drupal\entity_share_diff\Plugin\DiffGenerator\CoreFieldDiffParser

File

modules/entity_share_diff/src/Plugin/DiffGenerator/FileFieldDiffParser.php, line 29

Class

FileFieldDiffParser
Plugin to diff file fields.

Namespace

Drupal\entity_share_diff\Plugin\DiffGenerator

Code

public function build(FieldItemListInterface $field_items, array $remote_field_data = []) {
  $result = [];
  $fileManager = $this->entityTypeManager
    ->getStorage('file');

  // Every item from $field_items is of type FieldItemInterface.
  if (!$this
    ->getRemote()) {
    foreach ($field_items as $field_key => $field_item) {

      // Even though the local field is empty, remote data may be set.
      // So, get field values regardless.
      $values = $field_item
        ->getValue();
      if (!$field_item
        ->isEmpty()) {

        // Compare file names.
        if (isset($values['target_id'])) {

          /** @var \Drupal\file\Entity\File $file */
          $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();
          }
        }
      }

      // Compare additional (meta) fields.
      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'];
      }

      // Compare additional (meta) fields.
      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;
}