public function LinkFormatter::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/link/lib/Drupal/link/Plugin/field/formatter/LinkFormatter.php, line 118
Contains \Drupal\link\Plugin\field\formatter\LinkFormatter.

Class

LinkFormatter
Plugin implementation of the 'link' formatter.

Namespace

Drupal\link\Plugin\field\formatter

Code

public function prepareView(array $entities, $langcode, array &$items) {
  $settings = $this
    ->getSettings();
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => &$item) {

      // Split out the link into the parts required for url(): path and options.
      $parsed = drupal_parse_url($item['url']);
      $item['path'] = $parsed['path'];
      $item['options'] = array(
        'query' => $parsed['query'],
        'fragment' => $parsed['fragment'],
        'attributes' => &$item['attributes'],
      );

      // Add optional 'rel' attribute to link options.
      if (!empty($settings['rel'])) {
        $item['options']['attributes']['rel'] = $settings['rel'];
      }

      // Add optional 'target' attribute to link options.
      if (!empty($settings['target'])) {
        $item['options']['attributes']['target'] = $settings['target'];
      }
    }
  }
}