You are here

function fft_field_formatter_render in Field Formatter Template 8

Same name and namespace in other branches
  1. 8.2 fft.module \fft_field_formatter_render()

Render field formatter.

Parameters

object $entity: The entity object.

string $field_type: Field type.

$items: Field items.

array $settings: Settings array data.

bool $langcode: The language code.

Return value

array Return array builder data.

1 call to fft_field_formatter_render()
FFTFormatter::viewElements in src/Plugin/Field/FieldFormatter/FFTFormatter.php
Builds a renderable array for a field value.

File

./fft.module, line 127
Field formatter template.

Code

function fft_field_formatter_render($entity, $field_type, $items, array $settings, $langcode = FALSE) {
  $data = [];
  $element[0]['#markup'] = '';
  switch ($field_type) {
    case 'file':
      break;
    case 'image':
      foreach ($items as $delta => $item) {
        $image_uri = $item->entity
          ->getFileUri();
        $image['uri'] = $image_uri;
        $image['path'] = file_create_url($image_uri);
        foreach ([
          'width',
          'height',
          'alt',
        ] as $key) {
          $image["{$key}"] = $item->{$key};
        }
        for ($i = 1; $i <= 2; $i++) {
          if (!empty($settings["image_style_{$i}"])) {
            $image_styled = fft_field_styled_image_url($item, $settings["image_style_{$i}"]);
            $image["path_{$i}"] = $image_styled['path'];
            $image["width_{$i}"] = $image_styled['width'];
            $image["height_{$i}"] = $image_styled['height'];
          }
        }
        $data[] = $image;
      }
      break;
    case 'text':
    case 'text_long':
    case 'text_with_summary':
      foreach ($items as $delta => $item) {

        /** @var \Drupal\text\Plugin\Field\FieldType\TextWithSummaryItem $item */
        $data[] = [
          'text' => !empty($item->processed) ? $item->processed : '',
          'summary' => !empty($item->summary_processed) ? $item->summary_processed : '',
        ];
      }
      break;
    case 'entity_reference':
    case 'entity_reference_revisions':
      $data = $items;
      break;
    default:
      foreach ($items as $delta => $item) {
        $data[] = $item
          ->getValue();
      }
      break;
  }
  if (!empty($data)) {
    $template_file = fft_storage_dir() . "/" . $settings['template'];
    $settings_extras = drupal_parse_info_format($settings['settings']);
    $attached = [];
    foreach ([
      'js',
      'css',
    ] as $item) {
      if (isset($settings_extras[$item])) {
        foreach ((array) $settings_extras[$item] as $key => $value) {
          if (is_string($value)) {
            $attached[$item][] = fft_realpath($value, $template_file);
          }
          else {
            $attached[$item][] = $value;
          }
        }
      }
    }
    $settings_extras = $attached + $settings_extras;
    $output = fft_render($template_file, [
      'data' => $data,
      'entity' => $entity,
      'settings' => $settings_extras,
    ]);
    $element[0] = [
      '#markup' => $output,
      '#attached' => $attached,
    ];
  }
  return $element;
}