Commit 12c43587 authored by attila.fekete's avatar attila.fekete
Browse files

Issue #1547858 by edwardcuf: fixed issue when the track or set is set to private or not available

Issue #1549532 by scooteroo: if custom player settings are missing, fall back to default settings
parent 1c9e5829
......@@ -3,7 +3,5 @@ description = Field for SoundCloud tracks and sets.
core = 7.x
dependencies[] = field
package = SoundCloud
files[] = soundcloudfield.module
files[] = soundcloudfield.module
......@@ -40,8 +40,7 @@ function soundcloudfield_field_info() {
*
* Pseudo-hook.
*/
// function soundcloudfield_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
function soundcloudfield_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors) {
function soundcloudfield_field_validate($entity_type, $entity, $field, $instance, $langcode, &$items, &$errors) {
if ($field['type'] == 'soundcloud') {
foreach ($items as $delta => $item) {
if (!empty($item['url']) && !preg_match('@^http://soundcloud\.com/([^"\&]+)@i', $item['url'], $matches)) {
......@@ -80,67 +79,67 @@ function soundcloudfield_field_instance_settings_form($field, $instance) {
$form = array(
'#element_validate' => array('soundcloudfield_settings_form_validate'),
);
$form['player'] = array(
$form['soundcloudplayer'] = array(
'#type' => 'fieldset',
'#title' => t('SoundCloud settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['player']['width'] = array(
$form['soundcloudplayer']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#size' => 4,
'#default_value' => empty($settings['player']['width']) ? SOUNDCLOUDFIELD_DEFAULT_WIDTH : $settings['player']['width'],
'#default_value' => empty($settings['soundcloudplayer']['width']) ? SOUNDCLOUDFIELD_DEFAULT_WIDTH : $settings['soundcloudplayer']['width'],
'#description' => t('Player width in percent. Default is @width.', array('@width' => SOUNDCLOUDFIELD_DEFAULT_WIDTH)),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['player']['height'] = array(
$form['soundcloudplayer']['height'] = array(
'#type' => 'textfield',
'#title' => t('Height for tracks'),
'#size' => 4,
'#default_value' => empty($settings['player']['height']) ? SOUNDCLOUDFIELD_DEFAULT_HEIGHT : $settings['player']['height'],
'#default_value' => empty($settings['soundcloudplayer']['height']) ? SOUNDCLOUDFIELD_DEFAULT_HEIGHT : $settings['soundcloudplayer']['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['player']['set_height'] = array(
$form['soundcloudplayer']['set_height'] = array(
'#type' => 'textfield',
'#title' => t('Height for sets'),
'#size' => 4,
'#default_value' => empty($settings['player']['set_height']) ? SOUNDCLOUDFIELD_DEFAULT_SET_HEIGHT : $settings['player']['set_height'],
'#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)),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['player']['showcomments'] = array(
$form['soundcloudplayer']['showcomments'] = array(
'#type' => 'checkbox',
'#title' => t('Show comments'),
'#default_value' => empty($settings['player']['showcomments']) ? FALSE : $settings['player']['showcomments'],
'#default_value' => empty($settings['soundcloudplayer']['showcomments']) ? FALSE : $settings['soundcloudplayer']['showcomments'],
'#description' => t('Show comments in player'),
);
$form['player']['autoplay'] = array(
$form['soundcloudplayer']['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#default_value' => empty($settings['player']['autoplay']) ? FALSE : $settings['player']['autoplay'],
'#default_value' => empty($settings['soundcloudplayer']['autoplay']) ? FALSE : $settings['soundcloudplayer']['autoplay'],
'#description' => t('Player autoplay'),
);
$form['player']['showplaycount'] = array(
$form['soundcloudplayer']['showplaycount'] = array(
'#type' => 'checkbox',
'#title' => t('Show play count'),
'#default_value' => empty($settings['player']['showplaycount']) ? FALSE : $settings['player']['showplaycount'],
'#default_value' => empty($settings['soundcloudplayer']['showplaycount']) ? FALSE : $settings['soundcloudplayer']['showplaycount'],
'#description' => t('Show play count in player'),
);
$form['player']['showartwork'] = array(
$form['soundcloudplayer']['showartwork'] = array(
'#type' => 'checkbox',
'#title' => t('Show artwork'),
'#default_value' => empty($settings['player']['showartwork']) ? FALSE : $settings['player']['showartwork'],
'#default_value' => empty($settings['soundcloudplayer']['showartwork']) ? FALSE : $settings['soundcloudplayer']['showartwork'],
'#description' => t('Show artwork in player'),
);
$form['player']['color'] = array(
$form['soundcloudplayer']['color'] = array(
'#type' => module_exists('jquery_colorpicker') ? 'jquery_colorpicker' : 'textfield',
'#title' => t('Player color'),
'#default_value' => empty($settings['player']['color']) ? 'ff7700' : $settings['player']['color'],
'#default_value' => empty($settings['soundcloudplayer']['color']) ? 'ff7700' : $settings['soundcloudplayer']['color'],
'#required' => TRUE,
'#description' => t('Player color in hexadecimal format. Default is ff7700. Turn on the jQuery Colorpicker module if available.'),
);
......@@ -153,8 +152,8 @@ function soundcloudfield_field_instance_settings_form($field, $instance) {
* Validate the field settings form
*/
function soundcloudfield_settings_form_validate($element, &$form_state, $complete_form) {
if (!empty($form_state['values']['instance']['settings']['player']['width']) && ($form_state['values']['instance']['settings']['player']['width'] < 1 || $form_state['values']['instance']['settings']['player']['width'] > 100)) {
form_set_error('instance][settings][player][width', t('Player width must be a positive integer between 1-100'));
if (!empty($form_state['values']['instance']['settings']['soundcloudplayer']['width']) && ($form_state['values']['instance']['settings']['soundcloudplayer']['width'] < 1 || $form_state['values']['instance']['settings']['soundcloudplayer']['width'] > 100)) {
form_set_error('instance][settings][soundcloudplayer][width', t('Player width must be a positive integer between 1-100'));
}
}
......@@ -215,9 +214,17 @@ function soundcloudfield_field_formatter_info() {
/**
* Implements hook_field_formatter_view().
*/
function soundcloudfield_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
function soundcloudfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $instance['settings']['player'];
// 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);
}
$showcomments = ($settings['showcomments']) ? 'true' : 'false';
$autoplay = ($settings['autoplay']) ? 'true' : 'false';
......@@ -269,34 +276,42 @@ function soundcloudfield_field_formatter_view($obj_type, $object, $field, $insta
// Create the URL
$oembed_url = $oembed_endpoint . '?iframe=true&url=' . ($encoded_url);
// Load in the oEmbed XML
$oembed = simplexml_load_string(soundcloudfield_curl_get($oembed_url));
// Replace player default settings with our settings
$final_iframe = preg_replace('/(width=)"([^"]+)"/', 'width="' . $settings['width'] . '%"', $oembed->html);
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);
}
if (preg_match('/show_artwork=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/show_artwork=(true|false)/', 'show_artwork=' . $showartwork, $final_iframe);
// curl get
$soundcloud_curl_get = soundcloudfield_curl_get($oembed_url);
if ($soundcloud_curl_get != ' ') {
// Load in the oEmbed XML
$oembed = simplexml_load_string($soundcloud_curl_get);
// Replace player default settings with our settings
$final_iframe = preg_replace('/(width=)"([^"]+)"/', 'width="' . $settings['width'] . '%"', $oembed->html);
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);
}
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);
}
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
}
else {
$final_iframe = preg_replace('/">/', '&show_artwork=' . $showartwork . '">', $final_iframe);
}
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);
$output = t('The SoundCloud content at !url is not available, or it is set to private.', array('!url' => l($item['url'], $item['url'])));
}
else {
$final_iframe = preg_replace('/">/', '&color=' . $color . '">', $final_iframe);
}
$output = html_entity_decode($final_iframe); // '$oembed->html' for original embed
$element[$delta] = array('#markup' => $output);
}
......
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