You are here

function mediafront_field_formatter_view in MediaFront 7.2

Same name and namespace in other branches
  1. 7 includes/mediafront.field.inc \mediafront_field_formatter_view()

Implements hook_field_formatter_view().

File

includes/mediafront.field.inc, line 76

Code

function mediafront_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, &$items, $display, $type = 'mediafront_player') {
  $element = array();
  if ($display['type'] == $type) {
    $player_fields = array();

    // If this is a file, then we need just set the fields to the entity.
    if ($entity_type == 'file') {
      $player_fields = $entity;
    }
    else {

      // Set that the media was not found.
      $media_found = false;

      // Get the settings from this field first...
      if (!empty($instance['settings']['mediafront'])) {
        $options = $instance['settings']['mediafront'];
        $media_found |= $options['field_type'] == 'media';
        $player_fields[$instance['field_name']] = array(
          'type' => $options['field_type'],
          'field' => $instance['field_name'],
          'options' => $options,
        );
      }

      // Get the fields.
      $entity_fields = field_info_instances($instance['entity_type'], $instance['bundle']);

      // See if there is a views field defined for this display.
      $view = null;
      if (!empty($display['views_field'])) {
        $view = $display['views_field']->view;
      }
      foreach ($entity_fields as $name => $entity_field) {

        // Reset the options.
        $options = array();
        $include = TRUE;

        // If a views field is defined, use the options from it instead.
        if (!empty($view)) {
          $include = FALSE;
          foreach ($view->field as $view_field) {
            if ($view_field->field == $name && isset($view_field->options['mediafront'])) {
              $include = TRUE;
              $options = $view_field->options['mediafront'];
              break;
            }
          }
        }

        // If there aren't any options defined at this point, then use the field settings.
        if (!$options) {
          $options = !empty($entity_field['settings']['mediafront']) ? $entity_field['settings']['mediafront'] : array();
        }
        if ($include && !empty($options['field_type'])) {
          $is_media = $options['field_type'] == 'media';
          if (!$is_media || !$media_found) {
            $media_found |= $is_media;
            $player_fields[$entity_field['field_name']] = array(
              'type' => $options['field_type'],
              'field' => $entity_field['field_name'],
              'options' => $options,
            );
          }
        }
      }

      // If no media field is set, then just use this field.
      if (!$media_found && !empty($instance['field_name'])) {
        $player_fields[$instance['field_name']] = array(
          'type' => 'media',
          'field' => $instance['field_name'],
          'options' => array(
            'media_type' => 'media',
          ),
        );
      }
    }
    $settings = $display['settings'];

    // Use the first mediafront preset if none is defined.
    if (empty($settings['preset'])) {
      $temp = mediafront_preset_get_presets();
      $preset = array_shift($temp);
      $settings['preset'] = $preset['name'];
    }

    // If there was media found, but no items, we still want to render this player.
    if (!empty($instance['settings']['mediafront']['always_show_player']) && $media_found && empty($items)) {
      $items = array(
        0,
      );
    }
    $element[] = array(
      '#theme' => $type,
      '#entity' => $entity,
      '#preset' => $settings['preset'],
      '#fields' => $player_fields,
      '#type' => $display['type'],
    );
  }
  return $element;
}