Index: mediafield_display.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/mediafield_display/mediafield_display.module,v retrieving revision 1.2.2.3 diff -u -r1.2.2.3 mediafield_display.module --- mediafield_display.module 27 Sep 2007 05:11:26 -0000 1.2.2.3 +++ mediafield_display.module 13 Nov 2007 04:38:40 -0000 @@ -10,7 +10,7 @@ if (file_exists($path .'1pixelout.swf')) { $formatters['1pixelout'] = array( 'label' => t('1 Pixel Out player'), - 'field types' => array('file_audio'), + 'field types' => array('file_audio', 'link'), ); $formatters['1pixelout_plus'] = array( 'label' => t('1 Pixel Out player plus download link'), @@ -20,7 +20,7 @@ if (file_exists($path .'button.swf')) { $formatters['button'] = array( 'label' => t('Button player'), - 'field types' => array('file_audio'), + 'field types' => array('file_audio', 'link'), ); $formatters['button_plus'] = array( 'label' => t('Button player plus download link'), @@ -36,27 +36,93 @@ * Implementation of hook_field_formatter(). */ function mediafield_display_field_formatter($field, $item, $formatter) { + global $base_url; - if (!module_exists('audiofield')) { - return t('Audio Field module is required to display audio files.'); - } - require_once(drupal_get_path('module', 'audiofield') .'/multimediafile.inc'); + switch ($field['type']) { + case 'file_audio': + require_once(drupal_get_path('module', 'audiofield') .'/multimediafile.inc'); + if (!isset($item['fid']) || empty($item['fid'])) { + return ''; + } + $file = _field_file_load($item['fid']); + $file_url = check_url($base_url .'/'. $file['filepath']); + $file_title = check_plain($file['description']); + $attributes = array(); + $query = $fragment = NULL; + break; + case 'link': + if (empty($item['url']) && ($field['url'] != 'optional' || empty($item['title']))) { + return ''; + } + + $attributes = array(); + $item['attributes'] = unserialize($item['attributes']); + // Add attributes defined at the widget level + if (is_array($item['attributes'])) { + foreach($item['attributes'] as $attribute => $attbvalue) { + if (isset($item['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') { + $attributes[$attribute] = $attbvalue; + } + } + } + // Add attributes defined at the field level + if (is_array($field['attributes'])) { + foreach($field['attributes'] as $attribute => $attbvalue) { + if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user') { + $attributes[$attribute] = $attbvalue; + } + } + } + + // Replace URL tokens + if (module_exists('token') && $field['enable_tokens']) { + $item['url'] = token_replace($item['url'], 'node', $node); + } + + $type = link_validate_url($item['url']); + $file_url = $url = link_cleanup_url($item['url']); + + // Separate out the anchor if any + if (strpos($url, '#') !== FALSE) { + $fragment = substr($url, strpos($url, '#') + 1); + $url = substr($url, 0, strpos($url, '#')); + } + // Separate out the query string if any + if (strpos($url, '?') !== FALSE) { + $query = substr($url, strpos($url, '?') + 1); + $url = substr($url, 0, strpos($url, '?')); + } + + // Build the title + if (strlen(trim($item['title'])) || ($field['title'] == 'value' && strlen(trim($field['title_value'])))) { + // Use the title defined at the field level + if ($field['title'] == 'value' && strlen(trim($field['title_value']))) { + $file_title = $field['title_value']; + } + // Use the title defined by the user at the widget level + else { + $file_title = $item['title']; + } + // Replace tokens + if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) { + $file_title = token_replace($file_title, 'node', $node); + } + + $file_title = check_plain($file_title); + } - if (!isset($item['fid']) || empty($item['fid'])) { - return ''; + break; } - $file = _field_file_load($item['fid']); switch ($formatter) { case '1pixelout': case 'button': - $output = theme('mediafield_display_'. $formatter, $file, $item, $field); + $output = theme('mediafield_display_'. $formatter, $file_title, $file_url, $item, $field); break; case '1pixelout_plus': case 'button_plus': - - $output = theme('mediafield_display_'. substr($formatter, 0, strlen($formatter) - 5), $file, $item, $field); - $output .= theme('audiofield', $file, $item, $field); + $output = theme('mediafield_display_'. substr($formatter, 0, strlen($formatter) - 5), $file_title, $file_url, $item, $field); + $output .= l(t('Download'), $file_url, $attributes, $query, $fragment); break; } @@ -66,11 +132,11 @@ /** * Theme a 1pixelout audio file. */ -function theme_mediafield_display_1pixelout($file, $item, $field) { +function theme_mediafield_display_1pixelout($file_title, $file_url, $item, $field) { global $base_url; $options = array(); - $options['soundFile'] = check_url($base_url .'/'. $file['filepath']); + $options['soundFile'] = $file_url; $url = $base_url .'/'. drupal_get_path('module', 'mediafield_display') .'/players/1pixelout.swf'; $flashvars = array(); @@ -95,12 +161,12 @@ /** * Theme a button audio file. */ -function theme_mediafield_display_button($file, $item, $field) { +function theme_mediafield_display_button($file_title, $file_url, $item, $field) { global $base_url; $options = array(); - $options['song_url'] = check_url($base_url .'/'. $file['filepath']); - $options['song_title'] = check_plain($file['description']); + $options['song_url'] = $file_url; + $options['song_title'] = check_plain($file_title); // str_replace() is to fix issue in Drupal 5.2. $url = $base_url .'/'. drupal_get_path('module', 'mediafield_display') .'/players/button.swf?'. str_replace('http%3A/%252F', 'http://', drupal_query_string_encode($options));