You are here

public function ImageFieldBuilder::build in Diff 8

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

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 FieldDiffBuilderInterface::build

See also

\Drupal\diff\Plugin\diff\Field\TextFieldBuilder

File

src/Plugin/diff/Field/ImageFieldBuilder.php, line 25

Class

ImageFieldBuilder
Plugin to diff image fields.

Namespace

Drupal\diff\Plugin\diff\Field

Code

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

  // Every item from $field_items is of type FieldItemInterface.
  foreach ($field_items as $field_key => $field_item) {
    if (!$field_item
      ->isEmpty()) {
      $values = $field_item
        ->getValue();

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

        /** @var \Drupal\file\Entity\File $image */
        $image = $fileManager
          ->load($values['target_id']);
        $result[$field_key][] = $this
          ->t('Image: @image', [
          '@image' => $image
            ->getFilename(),
        ]);
      }

      // Compare Alt fields.
      if ($this->configuration['compare_alt_field']) {
        if (isset($values['alt'])) {
          $result[$field_key][] = $this
            ->t('Alt: @alt', [
            '@alt' => $values['alt'],
          ]);
        }
      }

      // Compare Title fields.
      if ($this->configuration['compare_title_field']) {
        if (!empty($values['title'])) {
          $result[$field_key][] = $this
            ->t('Title: @title', [
            '@title' => $values['title'],
          ]);
        }
      }

      // Compare file id.
      if ($this->configuration['show_id']) {
        if (isset($values['target_id'])) {
          $result[$field_key][] = $this
            ->t('File ID: @fid', [
            '@fid' => $values['target_id'],
          ]);
        }
      }
      $separator = $this->configuration['property_separator'] == 'nl' ? "\n" : $this->configuration['property_separator'];
      $result[$field_key] = implode($separator, $result[$field_key]);

      // EXPERIMENTAL: Attach thumbnail image data.
      if ($this->configuration['show_thumbnail']) {
        if (isset($values['target_id'])) {
          $storage = $this->entityTypeManager
            ->getStorage('entity_form_display');
          $display = $storage
            ->load($field_items
            ->getFieldDefinition()
            ->getTargetEntityTypeId() . '.' . $field_items
            ->getEntity()
            ->bundle() . '.default');
          if ($image_field = $display
            ->getComponent($field_item
            ->getFieldDefinition()
            ->getName())) {
            $image = $fileManager
              ->load($values['target_id']);
            $image_style[$field_key]['#thumbnail'] = array(
              '#theme' => 'image_style',
              '#uri' => $image
                ->getFileUri(),
              '#style_name' => $image_field['settings']['preview_image_style'],
            );
            $result = array_merge($result, $image_style);
          }
        }
      }
    }
  }
  return $result;
}