You are here

public function TextFieldBuilder::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/TextFieldBuilder.php, line 27

Class

TextFieldBuilder
Plugin to diff text fields.

Namespace

Drupal\diff\Plugin\diff\Field

Code

public function build(FieldItemListInterface $field_items) {
  $result = array();

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

    // Compare text formats.
    if ($this->configuration['compare_format'] == 1) {
      if (isset($values['format'])) {
        $controller = $this->entityTypeManager
          ->getStorage('filter_format');
        $format = $controller
          ->load($values['format']);

        // The format loaded successfully.
        $label = $this
          ->t('Format');
        if ($format != NULL) {
          $result[$field_key][] = $label . ": " . $format
            ->label();
        }
        else {
          $result[$field_key][] = $label . ": " . $this
            ->t('Missing format @format', array(
            '@format' => $values[$field_key],
          ));
        }
      }
    }

    // Compare field values.
    if (isset($values['value'])) {
      $value_only = TRUE;

      // Check if summary or text format are included in the diff.
      if ($this->configuration['compare_format'] == 1) {
        $value_only = FALSE;
      }
      $label = $this
        ->t('Value');
      if ($value_only) {

        // Don't display 'value' label.
        $result[$field_key][] = $values['value'];
      }
      else {
        $result[$field_key][] = $label . ":\n" . $values['value'];
      }
    }
  }
  return $result;
}