You are here

public function BlazyTextFormatter::viewElements in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/BlazyTextFormatter.php \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyTextFormatter::viewElements()

Implements hook_field_formatter_view().

Overrides FormatterBase::viewElements

File

src/Plugin/Field/FieldFormatter/BlazyTextFormatter.php, line 53

Class

BlazyTextFormatter
Plugin implementation of the 'Blazy Text' formatter to have a grid of texts.

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter

Code

public function viewElements($items, $entity) {
  $settings = $this
    ->buildSettings();
  $settings['_grid'] = !empty($settings['style']) && !empty($settings['grid']);

  // Hand over to default formatter if not multiple as it is for grid only.
  // @todo figure out to disable formatter like isApplicable() at D8 in the
  // first place, and remove this.
  if (!$this->isMultiple || !$settings['_grid']) {
    $types = field_info_field_types($this->fieldDefinition['type']);
    $display['type'] = isset($types['default_formatter']) ? $types['default_formatter'] : '';
    if ($fallback = text_field_formatter_view($this->entityType, $entity, $this->fieldDefinition, $this->fieldInstance, $this->langcode, $items, $display)) {
      return $fallback;
    }
  }
  $settings['vanilla'] = TRUE;
  $settings['namespace'] = $settings['item_id'] = 'blazy';

  // Build the settings.
  $build = [
    'settings' => $settings,
  ];

  // Modifies settings before building elements.
  $this->formatter
    ->preBuildElements($build, $items, $entity);

  // The ProcessedText element already handles cache context & tag bubbling.
  // @see \Drupal\filter\Element\ProcessedText::preRenderText()
  foreach ($items as $item) {
    $content = _text_sanitize($this->fieldInstance, $settings['langcode'], $item, 'value');
    $build[] = [
      '#markup' => $content,
    ];
    unset($content);
  }

  // Pass to manager for easy updates to all Blazy formatters.
  return $this->formatter
    ->build($build);
}