You are here

function sarnia_field_formatter_view in Sarnia 7

Implements hook_field_formatter_view().

Uses sarnia_field_get_property() to fetch properties.

File

./sarnia.field_formatter.inc, line 293
Field formatter hook implementations.

Code

function sarnia_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $formatter = $display['type'];
  $settings = $display['settings'];
  $solr_property = $settings['solr_property'];

  // Get the value of the property we're formatting.
  $values = sarnia_field_get_property($entity, $field, $solr_property);

  // Build a render array for the values.
  $render_array = array();
  if ($formatter == 'sarnia_formatter_plain') {
    foreach ($values as $i => $value) {
      $render_array['#items'][$i] = $value;
      $render_array[$i] = array(
        '#markup' => check_plain($value),
      );
    }
  }
  elseif ($formatter == 'sarnia_formatter_date') {
    $format = $settings['date_format_predefined'] ? $settings['date_format_predefined'] : $settings['date_format_custom'];
    foreach ($values as $i => $value) {
      $raw = $settings['data_is_timestamp'] ? $value : strtotime($value);
      $formatted = date($format, $raw);
      $render_array['#items'][$i] = $raw;
      $render_array[$i] = array(
        '#markup' => $formatted,
      );
    }
  }
  elseif ($formatter == 'sarnia_formatter_number') {
    foreach ($values as $i => $value) {
      $formatted = $raw = (double) $value;
      if ($settings['separate_thousands']) {
        $formatted = number_format($raw);
      }
      $render_array['#items'][$i] = $raw;
      $render_array[$i] = array(
        '#markup' => $formatted,
      );
    }
  }
  elseif ($formatter == 'sarnia_formatter_multimedia' || $formatter == 'sarnia_formatter_image') {
    $path_values = $settings['path_solr_property'] ? sarnia_field_get_property($entity, $field, $settings['path_solr_property']) : array();
    $alt_values = $settings['alt_solr_property'] ? sarnia_field_get_property($entity, $field, $settings['alt_solr_property']) : array();
    $title_values = $settings['title_solr_property'] ? sarnia_field_get_property($entity, $field, $settings['title_solr_property']) : array();
    $player_settings = array(
      'controls' => TRUE,
      'width' => 300,
      'height' => 30,
      'download_link' => FALSE,
      'download_text' => t('Download'),
    );
    $player_js_settings = array(
      'opts' => array(
        'audioHeight' => 30,
        'audioWidth' => 300,
      ),
      'controls' => TRUE,
    );
    foreach ($values as $i => $value) {
      $file = new stdclass();
      $file->filename = $value;
      $file->filemime = file_get_mimetype($file->filename);
      $file->filepath = _sarnia_build_path($i, $values, $path_values, $settings['base_path']);
      $file->uri = url($file->filepath);
      $file->filesize = 0;

      // Select the file display based on the first half of the mimetype.
      $simple_mime = current(explode('/', $file->filemime));

      // Only provide media players if mediaelement is present.
      if (!module_exists('mediaelement') && ($simple_mime == 'audio' || $simple_mime == 'video')) {
        $simple_mime = 'default';
      }
      elseif ($simple_mime == 'video' && !in_array($file->filemime, array(
        'video/mp4',
        'video/webm',
        'video/ogg',
        'video/x-flv',
      ))) {
        $simple_mime = 'default';
      }
      switch ($simple_mime) {
        case 'audio':
        case 'video':
          $class = drupal_html_id('sarnia-media-player');
          $render_array['#items'][$i] = $file->uri;
          $render_array[$i] = array(
            '#theme' => $simple_mime == 'audio' ? 'mediaelement_audio' : 'mediaelement_video',
            '#attributes' => array(
              'class' => $class,
              'src' => $file->uri,
              'type' => $file->filemime,
            ),
            '#settings' => $player_settings,
            '#attached' => array(
              'library' => array(
                array(
                  'mediaelement',
                  'mediaelement',
                ),
              ),
              'js' => array(
                array(
                  'type' => 'file',
                  'data' => drupal_get_path('module', 'mediaelement') . '/mediaelement.js',
                ),
                array(
                  'type' => 'setting',
                  'data' => array(
                    'mediaelement' => array(
                      ".{$class}" => $player_js_settings,
                    ),
                  ),
                ),
              ),
            ),
          );
          break;
        case 'image':
          $alt = trim(isset($alt_values[$i]) ? $alt_values[$i] : current($alt_values), '/');
          $title = isset($title_values[$i]) ? $title_values[$i] : current($title_values);
          $render_array['#items'][$i] = $file->uri;
          $render_array[$i] = array(
            '#theme' => 'image',
            '#path' => $file->uri,
            '#alt' => $alt,
            '#title' => $title,
          );
          break;
        default:
          $render_array['#items'][$i] = $file->uri;
          $render_array[$i] = array(
            '#theme' => 'file_link',
            '#file' => $file,
          );
      }
    }
  }
  elseif ($formatter == 'sarnia_formatter_text') {
    foreach ($values as $i => $value) {
      $raw = $value;
      $formatted = check_markup($raw, $settings['input_format']);
      $render_array['#items'][$i] = $raw;
      $render_array[$i] = array(
        '#markup' => $formatted,
      );
    }
  }
  elseif ($formatter == 'sarnia_formatter_count') {
    $count = count($values);
    $render_array['#items'][] = $count;
    $render_array[] = array(
      '#markup' => $count,
    );
  }
  elseif ($formatter == 'sarnia_formatter_multimedia_count') {

    // Use Drupal's file_icon_url() to divide files up by type. Count the number
    // of files of each type.
    $types = array();
    foreach ($values as $i => $value) {
      $file = new stdclass();
      $file->filename = $value;
      $file->filemime = file_get_mimetype($file->filename);
      $icon_path = file_icon_url($file);
      $types[$icon_path] = empty($types[$icon_path]) ? 1 : $types[$icon_path] + 1;
      $render_array['#items'][$i] = $icon_path;
    }

    // Translate file type icons to a common, human readable name.
    $type_name_map = array(
      'application-octet-stream.png' => t('unknown'),
      'application-pdf.png' => t('PDF'),
      'application-x-executable.png' => t('executable'),
      'audio-x-generic.png' => t('audio'),
      'image-x-generic.png' => t('image'),
      'package-x-generic.png' => t('zip'),
      'text-html.png' => t('HTML'),
      'text-plain.png' => t('plain text'),
      'text-x-generic.png' => t('document'),
      'text-x-script.png' => t('script'),
      'video-x-generic.png' => t('video'),
      'x-office-document.png' => t('Office document'),
      'x-office-presentation.png' => t('Powerpoint'),
      'x-office-spreadsheet.png' => t('spreadsheet'),
    );

    // Make a renderable list of the file types in this field.
    foreach ($types as $icon_path => $count) {
      $icon_filename = substr($icon_path, strrpos($icon_path, '/') + 1);
      $icon_type = isset($type_name_map[$icon_filename]) ? $type_name_map[$icon_filename] : t('unknown');
      $text = format_plural($count, '1 !type file', '@count !type files', array(
        '!type' => $icon_type,
      ));
      $render_array['#items'][] = $text;
      $render_array[] = array(
        array(
          '#theme' => 'image',
          '#path' => $icon_path,
          '#alt' => '',
          '#access' => $settings['show_icon'],
        ),
        array(
          '#markup' => $text,
        ),
      );
    }
  }
  elseif ($formatter == 'sarnia_formatter_openlayers' && module_exists('openlayers')) {
    $map_features = array();
    $latitudes = sarnia_field_get_property($entity, $field, $settings['lat_solr_property']);
    $longitudes = sarnia_field_get_property($entity, $field, $settings['lon_solr_property']);
    foreach ($latitudes as $i => $lat) {
      if (isset($longitudes[$i])) {
        $lon = $longitudes[$i];

        // Render the coordinates as WKT.
        // @see http://en.wikipedia.org/wiki/Well-known_text
        $wkt = sprintf('POINT(%F %F)', check_plain($lon), check_plain($lat));
        $map_features[] = array(
          'wkt' => $wkt,
          'projection' => 'EPSG:4326',
          'displayProjection' => 'EPSG:4326',
        );
        $render_array['#items'][$i] = $wkt;
      }
    }
    $map = openlayers_map_load($settings['openlayers_map']);
    if (!empty($map->data['layers']['sarnia_formatter_layer'])) {
      if (empty($map_features)) {

        // Let the map preset handle empty values.
        $render_array[] = array(
          '#markup' => openlayers_render_map($map->data),
        );
      }
      else {
        $map = openlayers_build_map($map->data);
        $map['layers']['sarnia_formatter_layer']['features'] = $map_features;

        // If there were no errors, render the map.
        if (empty($map['errors'])) {
          $render_array[0] = array();
          $render_array[0]['#markup'] = theme('openlayers_map', array(
            'map' => $map,
            'map_name' => $settings['openlayers_map'],
          ));
          $render_array[0]['#attached']['js'][] = array(
            'type' => 'setting',
            'data' => array(
              'openlayers' => array(
                'maps' => array(
                  $map['id'] => $map,
                ),
              ),
            ),
          );
        }
      }
    }
  }
  return $render_array;
}