You are here

public function DynamicEntityReferenceFieldBuilder::build in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/diff/Field/DynamicEntityReferenceFieldBuilder.php \Drupal\dynamic_entity_reference\Plugin\diff\Field\DynamicEntityReferenceFieldBuilder::build()

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

See also

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

File

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

Class

DynamicEntityReferenceFieldBuilder
Plugin to diff dynamic entity reference fields.

Namespace

Drupal\dynamic_entity_reference\Plugin\diff\Field

Code

public function build(FieldItemListInterface $field_items) {
  $result = [];
  foreach ($field_items as $field_key => $field_item) {
    if (!$field_item
      ->isEmpty()) {
      if ($field_item->entity) {

        /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
        $entity = $field_item->entity;
        if ($this->configuration['compare_entity_reference'] == COMPARE_ENTITY_REFERENCE_LABEL) {
          $result[$field_key][] = $this
            ->t('@id (@type)', [
            '@id' => $entity
              ->label(),
            '@type' => $entity
              ->getEntityType()
              ->getLabel(),
          ]);
        }
        else {
          $result[$field_key][] = $this
            ->t('Entity ID: @id (@type)', [
            '@id' => $entity
              ->id(),
            '@type' => $entity
              ->getEntityType()
              ->getLabel(),
          ]);
        }
      }
    }
  }
  return $result;
}