public function TestFieldPrepareViewFormatter::prepareView

Allows formatters to load information for field values being displayed.

This should be used when a formatter needs to load additional information from the database in order to render a field, for example a reference field that displays properties of the referenced entities such as name or type.

This method is called after the field type's implementation of hook_field_prepare_view().

This method operates on multiple entities. The $entities and $items parameters are arrays keyed by entity ID. For performance reasons, information for all involved entities should be loaded in a single query where possible.

Changes or additions to field values are done by alterings the $items parameter by reference.

Parameters

array $entities: Array of entities being displayed, keyed by entity ID.

string $langcode: The language the field values are to be shown in. If no language is provided the current language is used.

array $items: Array of field values for the entities, keyed by entity ID.

Overrides FormatterBase::prepareView

File

drupal/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/formatter/TestFieldPrepareViewFormatter.php, line 59
Contains \Drupal\field_test\Plugin\field\formatter\TestFieldPrepareViewFormatter.

Class

TestFieldPrepareViewFormatter
Plugin implementation of the 'field_test_with_prepare_view' formatter.

Namespace

Drupal\field_test\Plugin\field\formatter

Code

public function prepareView(array $entities, $langcode, array &$items) {
  foreach ($items as $id => $item) {
    foreach ($item as $delta => $value) {

      // Don't add anything on empty values.
      if ($value) {
        $items[$id][$delta]['additional_formatter_value'] = $value['value'] + 1;
      }
    }
  }
}