public function TestFieldEmptyFormatter::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/TestFieldEmptyFormatter.php, line 35
Contains \Drupal\field_test\Plugin\field\formatter\TestFieldEmptyFormatter.

Class

TestFieldEmptyFormatter
Plugin implementation of the 'field_empty_test' formatter.

Namespace

Drupal\field_test\Plugin\field\formatter

Code

public function prepareView(array $entities, $langcode, array &$items) {
  foreach ($items as $id => $entity_items) {
    if (empty($entity_items)) {

      // For fields with no value, just add the configured "empty" value.
      $items[$id][0]['value'] = $this
        ->getSetting('test_empty_string');
    }
  }
}