You are here

public function DynamicEntityReferenceFieldDiffParser::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 EntityReferenceFieldDiffParser::build

See also

\Drupal\entity_share_diff\Plugin\DiffGenerator\CoreFieldDiffParser

File

modules/entity_share_diff/src/Plugin/DiffGenerator/DynamicEntityReferenceFieldDiffParser.php, line 25

Class

DynamicEntityReferenceFieldDiffParser
Plugin to diff entity reference fields.

Namespace

Drupal\entity_share_diff\Plugin\DiffGenerator

Code

public function build(FieldItemListInterface $field_items, array $remote_field_data = []) {
  $result = [];

  // Case of local entity:
  // Every item from $field_items is of type FieldItemInterface.
  if (!$this
    ->getRemote()) {
    foreach ($field_items as $field_key => $field_item) {
      if (!$field_item
        ->isEmpty()) {
        if ($field_item->entity) {
          $entity = $field_item->entity;
          $entity_type_id = $entity
            ->getEntityTypeId();
          $result[$field_key] = $entity_type_id . ': ' . $entity
            ->uuid();
        }
      }
    }
  }
  elseif (!empty($remote_field_data['data'])) {
    foreach ($remote_field_data['data'] as $field_key => $remote_item_data) {
      list($entity_type_id, ) = explode('--', $remote_item_data['type']);
      $result[$field_key] = $entity_type_id . ': ' . $remote_item_data['id'];
    }
  }
  return $result;
}