You are here

function audiofield_field_formatter_view in AudioField 7

Implements hook_field_formatter_view().

TODO: Implement playlists to group audios hold in multiple valued fields.

File

./audio.field.inc, line 481
Implement an audio field, based on the file module's file field.

Code

function audiofield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  if ($display['type'] == 'audiofield_player') {

    // Get a list of all the players.
    $audio_players = audiofield_players();

    // Load the user so we can check permissions.
    global $user;

    // Loop over each file and format the player.
    foreach ($items as $delta => $item) {

      // Get file info.
      $fileinfo = pathinfo($item['uri']);

      // Determine the player for this item (default to HTML5).
      $selected_player = isset($display['settings']['audiofield_audioplayer_' . $fileinfo['extension']]) ? $display['settings']['audiofield_audioplayer_' . $fileinfo['extension']] : 'html5';
      $selected_player = $audio_players[$selected_player];

      // Set options for the item.
      $options = array(
        'entity_type' => $entity_type,
        'entity' => $entity,
        'field' => $field,
        'instance' => $instance,
        'langcode' => $langcode,
        'item' => $item,
        'display' => $display,
      );

      // Set the path to the audio player.
      $player_path = '';
      if (isset($selected_player['path'])) {
        $player_path = base_path() . $selected_player['path'];
      }

      // Render the player.
      $elements[$delta] = array(
        'player' => array(
          '#markup' => call_user_func($selected_player['callback'], $player_path, $item['uri'], $options),
        ),
      );

      // Display the file description if one is available.
      if (isset($item['description']) && !empty($item['description'])) {
        $elements[$delta]['description'] = array(
          '#type' => 'container',
          '#attributes' => array(
            'class' => 'description',
          ),
          '#children' => $item['description'],
        );
      }

      // Display download link if access granted.
      if ($display['settings']['download_link'] == 1 && (user_access('download all audio files') || $user->uid == $item['uid'] && user_access('download own audio files'))) {
        $elements[$delta]['download'] = array(
          '#type' => 'container',
          '#attributes' => array(
            'class' => array(
              'audio-download',
            ),
          ),
          '#children' => t('<strong>Download</strong>: !file_link', array(
            '!file_link' => theme('file_link', array(
              'file' => (object) $item,
            )),
          )),
        );
      }

      // Display file details.
      if ($display['settings']['display_file_details'] == 1) {

        // GetID3/ffprobe details.
        $audio_details = audiofield_details_formatter($item['uri'], $display['settings']['audiofield_detail']);
        $elements[$delta]['file_details'] = array(
          '#type' => 'container',
          '#attributes' => array(
            'class' => array(
              'audio-details',
            ),
          ),
        );
        if (isset($audio_details['img']['attributes']['src'])) {
          $elements[$delta]['file_details'][] = array(
            '#type' => 'container',
            '#attributes' => array(
              'class' => array(
                'audiofield_img',
              ),
            ),
            array(
              '#theme' => 'html_tag',
              '#tag' => 'img',
              '#attributes' => $audio_details['img']['attributes'],
            ),
          );
        }
        $elements[$delta]['file_details'][] = array(
          '#theme' => 'item_list',
          '#items' => $audio_details['list'],
          '#title' => NULL,
          '#type' => 'ul',
          '#attributes' => array(
            'class' => array(
              'tips',
              'audiofield_detail',
            ),
          ),
        );
      }
    }
  }
  return $elements;
}