You are here

function amp_field_formatter_view in Accelerated Mobile Pages (AMP) 7

Implements hook_field_formatter_view().

File

./amp.module, line 1117

Code

function amp_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'amp_iframe':
      foreach ($items as $delta => $item) {
        $output = _text_sanitize($instance, $langcode, $item, 'value');
        $amp_content = _amp_convert_markup_to_amp($output);
        $element[$delta] = array(
          '#markup' => $output,
          '#iframe' => array(
            '#markup' => $amp_content['amp_markup'],
          ),
        );
      }
      amp_add_component_libraries($amp_content['amp_components']);
      break;
    case 'amp_text':
      foreach ($items as $delta => $item) {
        $output = _text_sanitize($instance, $langcode, $item, 'value');
        $amp_content = _amp_convert_markup_to_amp($output);
        $element[$delta] = array(
          '#markup' => $amp_content['amp_markup'],
        );
        if (isset($_GET['warnfix']) || variable_get('amp_library_warnings_display', false)) {
          $element[$delta]['#markup'] .= '<pre>' . $amp_content['amp_warnings'] . '</pre>';
        }
        amp_add_component_libraries($amp_content['amp_components']);
      }
      break;
    case 'amp_text_trimmed':
      foreach ($items as $delta => $item) {
        $output = _text_sanitize($instance, $langcode, $item, 'value');
        if ($display['type'] == 'amp_text_trimmed') {
          $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
        }
        $amp_content = _amp_convert_markup_to_amp($output);
        $element[$delta] = array(
          '#markup' => $amp_content['amp_markup'],
        );
        if (isset($_GET['warnfix']) || variable_get('amp_library_warnings_display', false)) {
          $element[$delta]['#markup'] .= '<pre>' . $amp_content['amp_warnings'] . '</pre>';
        }
        amp_add_component_libraries($amp_content['amp_components']);
      }
      break;
    case 'amp_summary_or_trimmed':
      foreach ($items as $delta => $item) {
        if (!empty($item['summary'])) {
          $output = _text_sanitize($instance, $langcode, $item, 'summary');
        }
        else {
          $output = _text_sanitize($instance, $langcode, $item, 'value');
          $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
        }
        $amp_content = _amp_convert_markup_to_amp($output);
        $element[$delta] = array(
          '#markup' => $amp_content['amp_markup'],
        );
        if (isset($_GET['warnfix']) || variable_get('amp_library_warnings_display', false)) {
          $element[$delta]['#markup'] .= '<pre>' . $amp_content['amp_warnings'] . '</pre>';
        }
        amp_add_component_libraries($amp_content['amp_components']);
      }
      break;
    case 'amp_image':

      // Check if the formatter involves a link.
      if ($display['settings']['image_link'] == 'content') {
        $uri = entity_uri($entity_type, $entity);
      }
      elseif ($display['settings']['image_link'] == 'file') {
        $link_file = TRUE;
      }
      foreach ($items as $delta => $item) {
        if (isset($link_file)) {
          $uri = array(
            'path' => file_create_url($item['uri']),
            'options' => array(),
          );
        }
        $element[$delta] = array(
          '#theme' => 'image_formatter',
          '#item' => $item,
          '#image_style' => $display['settings']['image_style'],
          '#path' => isset($uri) ? $uri : '',
        );
        $element[$delta]['#item']['attributes']['layout'] = $display['settings']['amp_layout'];
        if ($display['settings']['amp_layout'] == 'fixed-height') {
          $element[$delta]['#item']['attributes']['height'] = $display['settings']['amp_fixed_height'];
          $element[$delta]['#item']['attributes']['width'] = 'auto';
        }
      }
      break;
    case 'amp_video':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'amp_video',
          '#file' => (object) $item,
          '#video_attributes_array' => array(
            'height' => $display['settings']['amp_video_height'],
            'width' => $display['settings']['amp_video_width'],
          ),
        );
      }
      break;
  }
  return $element;
}