public function SoundCloudDefaultFormatter::viewElements in SoundCloud field 8
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ SoundCloudDefaultFormatter.php, line 164
Class
- SoundCloudDefaultFormatter
- Plugin implementation of the 'soundcloud_default' formatter.
Namespace
Drupal\soundcloudfield\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$settings = $this
->getSettings();
// Get the "common" settings.
$width = $this
->getSetting('soundcloud_player_width');
$autoplay = $this
->getSetting('soundcloud_player_autoplay') ? 'true' : 'false';
$showcomments = $this
->getSetting('soundcloud_player_showcomments') ? 'true' : 'false';
$showplaycount = $this
->getSetting('soundcloud_player_showplaycount') ? 'true' : 'false';
$showartwork = $this
->getSetting('soundcloud_player_showartwork') ? 'true' : 'false';
$color = $this
->getSetting('soundcloud_player_color') ? $this
->getSetting('soundcloud_player_color') : 'ff7700';
//
$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 = $this
->getSetting('soundcloud_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['soundcloud_player_visual_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 = Json::decode($soundcloud_curl_get);
// 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="' . $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 comments?
if (preg_match('/show_comments=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/show_comments=(true|false)/', 'show_comments=' . $showcomments, $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&show_comments=' . $showcomments . '">', $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 = $this
->t('The SoundCloud content at <a href=":url">:url</a> is not available, or it is set to private.', [
':url' => $item->url,
]);
}
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item_attributes = $item->_attributes;
unset($item->_attributes);
// Render each element as markup.
$elements[$delta] = array(
'#markup' => $output,
'#allowed_tags' => [
'iframe',
],
);
// $elements[$delta] = array(
// '#markup' => $item->value,
// '#markup' => $item->processed,
// );
}
return $elements;
}