You are here

public function LinkIconFormatter::prepareView in Link Icon 8

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 operates on multiple entities. The $entities_items parameter is an array 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 directly altering the items.

Parameters

\Drupal\Core\Field\FieldItemListInterface[] $entities_items: An array with the field values from the multiple entities being rendered.

Overrides FormatterBase::prepareView

File

src/Plugin/Field/FieldFormatter/LinkIconFormatter.php, line 74

Class

LinkIconFormatter
Plugin implementation of the 'linkicon' formatter.

Namespace

Drupal\linkicon\Plugin\Field\FieldFormatter

Code

public function prepareView(array $entities_items) {

  // @todo drop 'predefined' for the new integer: LINKICON_PREDEFINED.
  if ($this
    ->getFieldSetting('title') == LinkIconManagerInterface::LINKICON_PREDEFINED || $this
    ->getFieldSetting('title') == 'predefined') {
    $settings = $this
      ->getFieldSettings();
    if (!empty($settings['title_predefined'])) {
      $titles = $this->linkIconManager
        ->extractAllowedValues($settings['title_predefined']);
      $tooltips = $this->linkIconManager
        ->extractAllowedValues($settings['title_predefined'], TRUE);
      foreach ($entities_items as $items) {
        $new_values = [];
        foreach ($items as $item) {
          $values = $item
            ->getValue();
          $title = isset($values['title']) ? $values['title'] : 'nix';
          $new_values['display_title'] = isset($titles[$title]) ? $titles[$title] : '';
          if (isset($tooltips[$title]) && $tooltips[$title]) {
            $new_values['tooltip'] = $tooltips[$title];
          }
          $merged_values = array_merge($values, $new_values);
          $item
            ->setValue($merged_values);
        }
      }
    }
  }
}