Commit d96850e3 authored by Attila Fekete's avatar Attila Fekete
Browse files

Added field type, field formatters, field widget. Attempt to get this to work.

parent 35d917c8
# Schema for the configuration files of the soundcloudfield module.
field.soundcloud.settings:
type: mapping
label: 'SoundCloud field settings'
field.soundcloud.instance_settings:
type: mapping
label: 'SoundCloud field settings'
name: 'SoundCloud field'
name: SoundCloud field
type: module
description: 'Field for SoundCloud tracks and sets.'
core: 8.x
type: module
package: SoundCloud
version: VERSION
dependencies:
- field
package: 'SoundCloud'
<?php
/**
* @file
* Install, update and uninstall functions for the SoundCloud field module.
*
*
* @author Attila Fekete - http://drupal.org/user/762986
*/
/**
* Implements hook_field_schema().
*/
function soundcloudfield_field_schema($field) {
return array(
'columns' => array(
'url' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'sortable' => TRUE,
),
),
);
}
/**
* Update field instances to be in sync with the new instance settings.
*/
function soundcloudfield_update_7000() {
$instances = field_info_instances();
foreach ($instances as $entity_type => $entities) {
foreach ($entities as $bundle => $fields) {
foreach ($fields as $field_name => $instance) {
if (in_array($instance['widget']['type'], array('soundcloud_url'))) {
$changed = FALSE;
if (!empty($instance['settings']['soundcloudplayer'])) {
if (empty($instance['settings']['soundcloudplayer']['flash_player']) || !isset($instance['settings']['soundcloudplayer']['flash_player']['height']) || !isset($instance['settings']['soundcloudplayer']['flash_player']['height_sets']) || !isset($instance['settings']['soundcloudplayer']['flash_player']['showcomments'])) {
$instance['settings']['soundcloudplayer']['flash_player'] = array(
'height' => SOUNDCLOUDFIELD_DEFAULT_HEIGHT,
'height_sets' => SOUNDCLOUDFIELD_DEFAULT_HEIGHT_SETS,
'showcomments' => 0,
);
$changed = TRUE;
}
if (empty($instance['settings']['soundcloudplayer']['html5_player']) || !isset($instance['settings']['soundcloudplayer']['html5_player']['html5_player_height']) || !isset($instance['settings']['soundcloudplayer']['html5_player']['html5_player_height_sets'])) {
$instance['settings']['soundcloudplayer']['html5_player'] = array(
'html5_player_height' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT,
'html5_player_height_sets' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT_SETS,
);
$changed = TRUE;
}
if (empty($instance['settings']['soundcloudplayer']['visual_player']) || !isset($instance['settings']['soundcloudplayer']['visual_player']['visual_player_height'])) {
$instance['settings']['soundcloudplayer']['visual_player'] = array(
'visual_player_height' => SOUNDCLOUDFIELD_DEFAULT_VISUAL_PLAYER_HEIGHT,
);
$changed = TRUE;
}
}
if ($changed) {
field_update_instance($instance);
}
}
}
}
}
field_cache_clear();
drupal_set_message(t('Field instances of type SoundCloud have been updated.'));
}
This diff is collapsed.
<?php
/**
* @file
* Contains Drupal\soundcloudfield\Plugin\Field\FieldFormatter\SoundCloudDefaultFormatter.
*/
namespace Drupal\soundcloudfield\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'soundcloud_default' formatter.
*
* @FieldFormatter(
* id = "soundcloud_default",
* module = "soundcloudfield",
* label = @Translation("Default (HTML5 player)"),
* field_types = {
* "soundcloud"
* }
* )
*/
class SoundCloudDefaultFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'soundcloud_player_type' => 'classic',
'soundcloud_player_width' => SOUNDCLOUDFIELD_DEFAULT_WIDTH,
'soundcloud_player_height' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT,
'soundcloud_player_height_sets' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT_SETS,
'soundcloud_player_visual_height' => SOUNDCLOUDFIELD_DEFAULT_VISUAL_PLAYER_HEIGHT,
'soundcloud_player_autoplay' => '',
'soundcloud_player_color' => 'ff7700',
'soundcloud_player_hiderelated' => '',
'soundcloud_player_showartwork' => '',
'soundcloud_player_showcomments' => TRUE,
'soundcloud_player_showplaycount' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['soundcloud_player_type'] = array(
'#title' => t('HTML5 player type'),
'#description' => t('Select which HTML5 player to use.'),
'#type' => 'select',
'#default_value' => $this->getSetting('soundcloud_player_type'),
'#options' => array(
'classic' => 'Classic',
'visual' => 'Visual Player (new)',
),
);
$elements['soundcloud_player_width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#size' => 4,
'#default_value' => $this->getSetting('soundcloud_player_width'),
'#description' => t('Player width in percent. Default is @width.', array('@width' => SOUNDCLOUDFIELD_DEFAULT_WIDTH)),
);
$elements['soundcloud_player_height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#size' => 4,
'#default_value' => $this->getSetting('soundcloud_player_height'),
'#states' => array(
'visible' => array(
':input[name*="soundcloud_player_type"]' => array('value' => 'classic'),
),
),
);
$elements['soundcloud_player_height_sets'] = array(
'#type' => 'textfield',
'#title' => t('Height for sets'),
'#size' => 4,
'#default_value' => $this->getSetting('soundcloud_player_height_sets'),
'#states' => array(
'visible' => array(
':input[name*="soundcloud_player_type"]' => array('value' => 'classic'),
),
),
);
$elements['soundcloud_player_visual_height'] = array(
'#type' => 'select',
'#title' => t('Height of the visual player'),
'#size' => 4,
'#default_value' => $this->getSetting('soundcloud_player_visual_height'),
'#options' => array(
300 => '300px',
450 => '450px',
600 => '600px',
),
'#states' => array(
'visible' => array(
':input[name*="soundcloud_player_type"]' => array('value' => 'visual'),
),
),
);
$elements['soundcloud_player_autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Play audio automatically when loaded (autoplay).'),
'#default_value' => $this->getSetting('soundcloud_player_autoplay'),
);
$elements['soundcloud_player_color'] = array(
'#type' => 'textfield',
'#title' => t('Player color.'),
'#default_value' => $this->getSetting('soundcloud_player_color'),
'#description' => t('Player color in hexadecimal format. Default is ff7700. Turn on the jQuery Colorpicker module if available.'),
);
$elements['soundcloud_player_hiderelated'] = array(
'#type' => 'checkbox',
'#title' => t('Hide raleted tracks.'),
'#default_value' => $this->getSetting('soundcloud_player_hiderelated'),
);
$elements['soundcloud_player_showartwork'] = array(
'#type' => 'checkbox',
'#title' => t('Show artwork.'),
'#default_value' => $this->getSetting('soundcloud_player_showartwork'),
);
$elements['soundcloud_player_showcomments'] = array(
'#type' => 'checkbox',
'#title' => t('Show comments.'),
'#default_value' => $this->getSetting('soundcloud_player_showcomments'),
);
$elements['soundcloud_player_showplaycount'] = array(
'#type' => 'checkbox',
'#title' => t('Show play count.'),
'#default_value' => $this->getSetting('soundcloud_player_showplaycount'),
);
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$settings = $this->getSettings();
$summary[] = t('Displays the SoundCloud player.');
return $summary;
}
/**
* {@inheritdoc}
*/
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 = 'http://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['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&url=' . ($encoded_url);
// 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,
// 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 = t('The SoundCloud content at !url is not available, or it is set to private.', array('!url' => l($item['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;
}
protected function renderEmbedCode($track_id, $width, $height, $autoplay) {
return [
'#type' => 'html_tag',
'#tag' => 'iframe',
'#attributes' => [
'width' => $width,
'height' => $height,
'frameborder' => '0',
'allowfullscreen' => 'allowfullscreen',
'src' => sprintf('https://w.soundcloud.com/player/%s?autoplay=%s', $track_id, $autoplay),
],
];
}
}
<?php
/**
* @file
* Contains Drupal\soundcloudfield\Plugin\Field\FieldFormatter\SoundCloudLinkFormatter.
*/
namespace Drupal\soundcloudfield\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Plugin implementation of the 'soundcloud_link' formatter.
*
* @FieldFormatter(
* id = "soundcloud_link",
* module = "soundcloudfield",
* label = @Translation("Link to SoundCloud URI"),
* field_types = {
* "soundcloud"
* }
* )
*/
class SoundCloudLinkFormatter extends FormatterBase {
// investigate: extends UriLinkFormatter
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'trim_length' => '80',
'rel' => '',
'target' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['trim_length'] = array(
'#type' => 'number',
'#title' => t('Trim link text length'),
'#field_suffix' => t('characters'),
'#default_value' => $this->getSetting('trim_length'),
'#min' => 1,
'#description' => t('Leave blank to allow unlimited link text lengths.'),
);
$elements['rel'] = array(
'#type' => 'checkbox',
'#title' => t('Add rel="nofollow" to links'),
'#return_value' => 'nofollow',
'#default_value' => $this->getSetting('rel'),
);
$elements['target'] = array(
'#type' => 'checkbox',
'#title' => t('Open link in new window'),
'#return_value' => '_blank',
'#default_value' => $this->getSetting('target'),
);
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$settings = $this->getSettings();
$summary[] = t('Displays the SoundCloud link.');
if (!empty($settings['trim_length'])) {
$summary[] = t('Link text trimmed to @limit characters', array('@limit' => $settings['trim_length']));
}
else {
$summary[] = t('Link text not trimmed');
}
if (!empty($settings['rel'])) {
$summary[] = t('Add rel="@rel"', array('@rel' => $settings['rel']));
}
if (!empty($settings['target'])) {
$summary[] = t('Open link in new window');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$settings = $this->getSettings();
foreach ($items as $delta => $item) {
if (!$item->isEmpty()) {
$link_title = $item->url;
// Trim the link text to the desired length.
if (!empty($settings['trim_length'])) {
$link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
}
$elements[$delta] = array(
'#type' => 'link',
'#url' => Url::fromUri($item->url),
'#title' => $link_title,
'#options' => array(),
);
if (!empty($item->_attributes)) {
$elements[$delta]['#options'] += array('attributes' => array());
$elements[$delta]['#options']['attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
}
}
return $elements;
}
}
<?php
/**
* @file
* Contains Drupal\soundcloudfield\Plugin\Field\FieldType\SoundCloudItem.
*/
namespace Drupal\soundcloudfield\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition; // investigate
/**
* Plugin implementation of the 'soundcloud' field type.
*
* @FieldType(
* id = "soundcloud",
* label = @Translation("SoundCloud"),
* module = "soundcloud",
* description = @Translation("Stores a SoundCloud URL string."),
* default_widget = "soundcloud_url",
* default_formatter = "soundcloud_default",
* )
*/
class SoundCloudItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['url'] = DataDefinition::create('uri')
->setLabel(t('URL'));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'url' => array(
'description' => 'The URL of the SoundCloud link.',
'type' => 'varchar',
'length' => 2048,
'not null' => FALSE,
),
// 'indexes' => array(
// 'url' => array('url'),
// ),
),
);
}
/**
* {@inheritdoc}
*/
public function instanceSettingsForm(array $form, array &$form_state) {
$element = array();