You are here

function theme_soundcloudfield_formatter_html5 in SoundCloud field 6

Theme function for 'html5' field formatter.

Parameters

$element:

Return value

string

1 string reference to 'theme_soundcloudfield_formatter_html5'
soundcloudfield_theme in ./soundcloudfield.module
Implementation of hook_theme().

File

./soundcloudfield.module, line 389
SoundCloud CCK field.

Code

function theme_soundcloudfield_formatter_html5($element) {
  $output = '';

  // $field = content_fields($element['#field_name'], $element['#type_name']);
  $oembed_endpoint = 'http://soundcloud.com/oembed';
  $field_info = soundcloudfield_get_content_node_field_instance_by_field_name($element['#field_name']);
  if ($field_info['widget_type'] == 'soundcloud_url') {
    $field_info['widget_settings'] = unserialize($field_info['widget_settings']);
    $field_info['display_settings'] = unserialize($field_info['display_settings']);
    $width = $field_info['widget_settings']['width'];
    $height = $field_info['widget_settings']['height'];
    $autoplay = $field_info['widget_settings']['autoplay'] ? 'true' : 'false';
    $showartwork = $field_info['widget_settings']['showartwork'] ? 'true' : 'false';
    $color = $field_info['widget_settings']['color'];
    $visual_player = $element['#formatter'] == 'visual' ? 'true' : 'false';
    $encoded_url = urlencode($element['#item']['embed']);
    $oembed_url = $oembed_endpoint . '?iframe=true&url=' . $encoded_url;
    $use_errors = libxml_use_internal_errors(TRUE);
    $oembed = simplexml_load_string(soundcloudfield_curl_get($oembed_url));
    if (!$oembed) {
      $output = t('The SoundCloud content at !url is not available, or it is set to private.', array(
        '!url' => l($element['#item']['embed'], $element['#item']['embed']),
      ));
    }
    else {

      // Replace player default settings with our settings,
      // set player width and height first.
      $final_iframe = preg_replace('/(width=)"([^"]+)"/', 'width="' . $width . '%"', $oembed->html);
      $final_iframe = preg_replace('/(height=)"([^"]+)"/', 'height="' . $height . '%"', $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);
      }

      // 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);
      }
      $output = html_entity_decode($final_iframe);

      // '$oembed->html' for original embed
    }

    // More info: http://php.net/manual/en/function.libxml-use-internal-errors.php
    libxml_clear_errors();
    libxml_use_internal_errors($use_errors);
    return $output;
  }
}