function soundcloudfield_field_formatter_view in SoundCloud field 7
Implements hook_field_formatter_view().
File
- ./
soundcloudfield.module, line 298 - @author Attila Fekete - http://drupal.org/user/762986
Code
function soundcloudfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// if player custom settings array is not available, use default settings
if (!empty($instance['settings']['soundcloudplayer'])) {
$settings = $instance['settings']['soundcloudplayer'];
}
else {
$settings = $instance['settings'];
watchdog('soundcloudfield', 'Instance settings array is missing. Falling back to default player settings.', NULL, WATCHDOG_WARNING);
}
// Get the "common" settings.
$autoplay = $settings['autoplay'] ? 'true' : 'false';
$showplaycount = $settings['showplaycount'] ? 'true' : 'false';
$showartwork = $settings['showartwork'] ? 'true' : 'false';
$color = $settings['color'];
switch ($display['type']) {
// HTML5 (classic or visual) player case.
case 'soundcloud_html5':
$oembed_endpoint = 'https://soundcloud.com/oembed';
// Get HTML5 player-specific settings.
$html5_player_height = empty($settings['html5_player']['html5_player_height']) ? SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT : $settings['html5_player']['html5_player_height'];
$html5_player_height_sets = empty($settings['html5_player']['html5_player_height_sets']) ? SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT_SETS : $settings['html5_player']['html5_player_height_sets'];
$visual_player = $display['settings']['player_type'] == 'visual' ? 'true' : 'false';
foreach ($items as $delta => $item) {
$output = '';
$encoded_url = urlencode($item['url']);
// Set the proper height for this item.
// - classic player: track default is 166px, set default is 450px.
// - visual player: player height it's the same for tracks and sets.
if ($visual_player == 'true') {
$iframe_height = $settings['visual_player']['visual_player_height'];
}
else {
$parsed_url = parse_url($item['url']);
$splitted_url = explode("/", $parsed_url['path']);
// An artist page or a set or a track?
$iframe_height = !isset($splitted_url[2]) || $splitted_url[2] == 'sets' ? $html5_player_height_sets : $html5_player_height;
}
// Create the URL
$oembed_url = $oembed_endpoint . '?iframe=true&format=json&url=' . $encoded_url;
// curl get
$soundcloud_curl_get = soundcloudfield_curl_get($oembed_url);
if ($soundcloud_curl_get != ' ') {
// Load in the oEmbed XML
$oembed = drupal_json_decode($soundcloud_curl_get);
// Replace player default settings with our settings,
// set player width and height first.
$final_iframe = preg_replace('/(width=)"([^"]+)"/', 'width="' . $settings['width'] . '%"', $oembed['html']);
$final_iframe = preg_replace('/(height=)"([^"]+)"/', 'height="' . $iframe_height . '"', $oembed['html']);
// Set autoplay.
if (preg_match('/auto_play=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/auto_play=(true|false)/', 'auto_play=' . $autoplay, $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&auto_play=' . $autoplay . '">', $final_iframe);
}
// Show playcount?
if (preg_match('/show_playcount=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/show_playcount=(true|false)/', 'show_playcount=' . $showplaycount, $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&show_playcount=' . $showplaycount . '">', $final_iframe);
}
// Show artwork?
if (preg_match('/show_artwork=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/show_artwork=(true|false)/', 'show_artwork=' . $showartwork, $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&show_artwork=' . $showartwork . '">', $final_iframe);
}
// Set player color.
if (preg_match('/color=([a-zA-Z0-9]{6})/', $final_iframe)) {
$final_iframe = preg_replace('/color=([a-zA-Z0-9]{6})/', 'color=' . $color, $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&color=' . $color . '">', $final_iframe);
}
// Set HTML5 player type based on formatter: classic/visual player.
if (preg_match('/visual=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/visual=(true|false)/', 'visual=' . $visual_player, $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&visual=' . $visual_player . '">', $final_iframe);
}
// Final output. Use '$oembed->html' for original embed code.
$output = html_entity_decode($final_iframe);
}
else {
$output = t('The SoundCloud content at !url is not available, or it is set to private.', array(
'!url' => l($item['url'], $item['url']),
));
}
$element[$delta] = array(
'#markup' => $output,
);
}
break;
// Link formatter.
case 'soundcloud_link':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => l($item['url'], $item['url']),
);
}
break;
}
return $element;
}