Commit 6f12e7e6 authored by attila.fekete's avatar attila.fekete
Browse files

Issue #2188503: added new formatter to support the new SoundCloud visual...

Issue #2188503: added new formatter to support the new SoundCloud visual player, added categorized settings for each player type, properly set player height
parent d141fd24
......@@ -17,7 +17,7 @@ function soundcloudfield_field_schema($field) {
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'sortable' => TRUE
'sortable' => TRUE,
),
),
);
......
......@@ -9,7 +9,11 @@
define('SOUNDCLOUDFIELD_DEFAULT_WIDTH', 100);
define('SOUNDCLOUDFIELD_DEFAULT_HEIGHT', 81);
define('SOUNDCLOUDFIELD_DEFAULT_SET_HEIGHT', 305);
define('SOUNDCLOUDFIELD_DEFAULT_HEIGHT_SETS', 305);
define('SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT', 166);
define('SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT_SETS', 450);
// Usable sizes for visual player: 300, 450, 600.
define('SOUNDCLOUDFIELD_DEFAULT_VISUAL_PLAYER_HEIGHT', 450);
/**
* Implements hook_field_info().
......@@ -22,12 +26,16 @@ function soundcloudfield_field_info() {
'instance_settings' => array(
'width' => SOUNDCLOUDFIELD_DEFAULT_WIDTH,
'height' => SOUNDCLOUDFIELD_DEFAULT_HEIGHT,
'set_height' => SOUNDCLOUDFIELD_DEFAULT_SET_HEIGHT,
'set_height' => SOUNDCLOUDFIELD_DEFAULT_HEIGHT_SETS,
'autoplay' => FALSE,
'showcomments' => FALSE,
'showplaycount' => FALSE,
'showartwork' => FALSE,
'color' => 'ff7700',
'html5_player_height' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT,
'html5_player_height_sets' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT_SETS,
// New visual player setting.
'visual_player_height' => SOUNDCLOUDFIELD_DEFAULT_VISUAL_PLAYER_HEIGHT,
),
'default_widget' => 'soundcloud_url',
'default_formatter' => 'soundcloud_default',
......@@ -79,12 +87,14 @@ function soundcloudfield_field_instance_settings_form($field, $instance) {
$form = array(
'#element_validate' => array('soundcloudfield_settings_form_validate'),
);
$form['soundcloudplayer'] = array(
'#type' => 'fieldset',
'#title' => t('SoundCloud settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['soundcloudplayer']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
......@@ -94,47 +104,102 @@ function soundcloudfield_field_instance_settings_form($field, $instance) {
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['soundcloudplayer']['height'] = array(
// Flash player-only settings.
$form['soundcloudplayer']['flash_player'] = array(
'#type' => 'fieldset',
'#title' => t('SoundCloud flash player-only settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['soundcloudplayer']['flash_player']['height'] = array(
'#type' => 'textfield',
'#title' => t('Height for tracks'),
'#size' => 4,
'#default_value' => empty($settings['soundcloudplayer']['height']) ? SOUNDCLOUDFIELD_DEFAULT_HEIGHT : $settings['soundcloudplayer']['height'],
'#default_value' => empty($settings['soundcloudplayer']['flash_player']['height']) ? SOUNDCLOUDFIELD_DEFAULT_HEIGHT : $settings['soundcloudplayer']['flash_player']['height'],
'#description' => t('Player height for tracks. Default is @height.', array('@height' => SOUNDCLOUDFIELD_DEFAULT_HEIGHT)),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['soundcloudplayer']['set_height'] = array(
$form['soundcloudplayer']['flash_player']['set_height'] = array(
'#type' => 'textfield',
'#title' => t('Height for sets'),
'#size' => 4,
'#default_value' => empty($settings['soundcloudplayer']['set_height']) ? SOUNDCLOUDFIELD_DEFAULT_SET_HEIGHT : $settings['soundcloudplayer']['set_height'],
'#description' => t('Player height for sets. Default is @height.', array('@height' => SOUNDCLOUDFIELD_DEFAULT_SET_HEIGHT)),
'#default_value' => empty($settings['soundcloudplayer']['flash_player']['set_height']) ? SOUNDCLOUDFIELD_DEFAULT_HEIGHT_SETS : $settings['soundcloudplayer']['flash_player']['set_height'],
'#description' => t('Player height for sets. Default is @height.', array('@height' => SOUNDCLOUDFIELD_DEFAULT_HEIGHT_SETS)),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['soundcloudplayer']['showcomments'] = array(
$form['soundcloudplayer']['flash_player']['showcomments'] = array(
'#type' => 'checkbox',
'#title' => t('Show comments'),
'#default_value' => empty($settings['soundcloudplayer']['showcomments']) ? FALSE : $settings['soundcloudplayer']['showcomments'],
'#description' => t('Show comments in player'),
'#default_value' => empty($settings['soundcloudplayer']['flash_player']['showcomments']) ? FALSE : $settings['soundcloudplayer']['flash_player']['showcomments'],
'#description' => t('Show comments by default in the player.'),
);
// HTML5 (classic) player-only settings.
$form['soundcloudplayer']['html5_player'] = array(
'#type' => 'fieldset',
'#title' => t('SoundCloud HTML5 player-only settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['soundcloudplayer']['html5_player']['html5_player_height'] = array(
'#type' => 'textfield',
'#title' => t('Height for tracks'),
'#size' => 4,
'#default_value' => empty($settings['soundcloudplayer']['html5_player']['html5_player_height']) ? SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT : $settings['soundcloudplayer']['html5_player']['html5_player_height'],
'#description' => t('Player height for tracks. Default is @height.', array('@height' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT)),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['soundcloudplayer']['html5_player']['html5_player_height_sets'] = array(
'#type' => 'textfield',
'#title' => t('Height for sets'),
'#size' => 4,
'#default_value' => empty($settings['soundcloudplayer']['html5_player']['html5_player_height_sets']) ? SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT_SETS : $settings['soundcloudplayer']['html5_player']['html5_player_height_sets'],
'#description' => t('Player height for sets. Default is @height.', array('@height' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT_SETS)),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
// Visual (new) player-only settings.
$form['soundcloudplayer']['visual_player'] = array(
'#type' => 'fieldset',
'#title' => t('SoundCloud HTML5 visual player-only settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['soundcloudplayer']['visual_player']['visual_player_height'] = array(
'#type' => 'select',
'#title' => t('Size of the visual player (height):'),
'#default_value' => empty($settings['soundcloudplayer']['visual_player']['visual_player_height']) ? SOUNDCLOUDFIELD_DEFAULT_VISUAL_PLAYER_HEIGHT : $settings['soundcloudplayer']['visual_player']['visual_player_height'],
'#options' => array(
300 => '300px',
450 => '450px',
600 => '600px',
),
'#description' => t('Height of the "new" HTML5 visual player.'),
);
// Common settings.
$form['soundcloudplayer']['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#default_value' => empty($settings['soundcloudplayer']['autoplay']) ? FALSE : $settings['soundcloudplayer']['autoplay'],
'#description' => t('Player autoplay'),
'#description' => t('Autoplay track or set.'),
);
$form['soundcloudplayer']['showplaycount'] = array(
'#type' => 'checkbox',
'#title' => t('Show play count'),
'#default_value' => empty($settings['soundcloudplayer']['showplaycount']) ? FALSE : $settings['soundcloudplayer']['showplaycount'],
'#description' => t('Show play count in player'),
'#description' => t('Show play count in player.'),
);
$form['soundcloudplayer']['showartwork'] = array(
'#type' => 'checkbox',
'#title' => t('Show artwork'),
'#default_value' => empty($settings['soundcloudplayer']['showartwork']) ? FALSE : $settings['soundcloudplayer']['showartwork'],
'#description' => t('Show artwork in player'),
'#description' => t('Show artwork in player. Has no effect when using the visual player.'),
);
$form['soundcloudplayer']['color'] = array(
'#type' => module_exists('jquery_colorpicker') ? 'jquery_colorpicker' : 'textfield',
......@@ -200,10 +265,16 @@ function soundcloudfield_field_formatter_info() {
'label' => t('Flash player'),
'field types' => array('soundcloud'),
),
'soundcloud_html5' => array(
'label' => t('HTML5 player'),
'field types' => array('soundcloud'),
'settings' => array(
// Give a default value for when the form is first loaded.
'player_type' => 'classic',
),
),
'soundcloud_link' => array(
'label' => t('Link'),
'field types' => array('soundcloud'),
......@@ -211,6 +282,49 @@ function soundcloudfield_field_formatter_info() {
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function soundcloudfield_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
// This gets the view_mode where our settings are stored.
$display = $instance['display'][$view_mode];
// This gets the actual settings.
$settings = $display['settings'];
// Initialize the element variable.
$element = array();
// Add select box, for HTML5 only.
if (strpos($display['type'], '_html5') !== FALSE) {
$element['player_type'] = array(
'#type' => 'select',
'#title' => t('HTML5 player type'),
'#description' => t('Select which HTML5 player to use.'),
'#default_value' => $settings['player_type'],
'#options' => array(
'classic' => 'Classic (old)',
'visual' => 'Visual Player (new)',
),
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function soundcloudfield_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if (strpos($display['type'], '_html5') !== FALSE) {
$summary = t('Use the ') . $settings['player_type'] . t(' HTML5 player.');
}
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
......@@ -226,15 +340,18 @@ function soundcloudfield_field_formatter_view($entity_type, $entity, $field, $in
watchdog('soundcloudfield', 'Instance settings array is missing. Falling back to default player settings.', NULL, WATCHDOG_WARNING);
}
$showcomments = ($settings['showcomments']) ? 'true' : 'false';
// 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']) {
// Flash player case.
case 'soundcloud_default':
// Get the 'showcomment' setting for the flash player.
$showcomments = ($settings['flash_player']['showcomments']) ? 'true' : 'false';
foreach ($items as $delta => $item) {
$output = '';
$encoded_url = urlencode($item['url']);
......@@ -242,15 +359,17 @@ function soundcloudfield_field_formatter_view($entity_type, $entity, $field, $in
$splitted_url = explode("/", $parsed_url['path']);
// If this is a track..
if ($splitted_url[2] != 'sets') {
$height = $settings['height'];
$height = $settings['flash_player']['height'];
$params = 'url=' . $encoded_url .
'&show_comments=' . $showcomments .
'&auto_play=' . $autoplay .
'&color=' . $color;
}
// Otherwise it's a set.
else {
$height = $settings['set_height'];
$height = $settings['flash_player']['set_height'];
$params = 'url=' . $encoded_url .
'&show_comments=' . $showcomments .
'&auto_play=' . $autoplay .
......@@ -265,14 +384,32 @@ function soundcloudfield_field_formatter_view($entity_type, $entity, $field, $in
$element[$delta] = array('#markup' => $output);
}
break;
// HTML5 (classic+visual) player case.
case 'soundcloud_html5':
$oembed_endpoint = 'http://soundcloud.com/oembed';
// Get HTML5 player-specific settings.
$html5_player_height = $settings['html5_player']['html5_player_height'];
$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']);
// Track or set?
$iframe_height = ($splitted_url[2] != 'sets') ? $html5_player_height : $html5_player_height_sets;
}
// Create the URL
$oembed_url = $oembed_endpoint . '?iframe=true&url=' . ($encoded_url);
......@@ -283,31 +420,40 @@ function soundcloudfield_field_formatter_view($entity_type, $entity, $field, $in
// Load in the oEmbed XML
$oembed = simplexml_load_string($soundcloud_curl_get);
// Replace player default settings with our settings
// 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 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);
}
$output = html_entity_decode($final_iframe); // '$oembed->html' for original embed
// 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'])));
......@@ -316,7 +462,7 @@ function soundcloudfield_field_formatter_view($entity_type, $entity, $field, $in
$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']));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment