Commit ff3ca1ba authored by guilhermevp's avatar guilhermevp Committed by akoepke
Browse files

Issue #3168341 by ao5357, guilhermevp, Lukey, sadashiv, Pooja Ganjage, Suresh...

Issue #3168341 by ao5357, guilhermevp, Lukey, sadashiv, Pooja Ganjage, Suresh Prabhu Parkala: Drupal 9 Readiness
parent 32117f27
......@@ -2,7 +2,7 @@ name: SoundCloud Field
type: module
description: 'Field for SoundCloud tracks and sets.'
core: 8.x
package: SoundCloud
version: VERSION
dependencies:
- drupal:field
......@@ -3,7 +3,7 @@
/**
* @file
*
* @author Attila Fekete - http://drupal.org/user/762986
* Contains soundcloudfield.module.
*/
define('SOUNDCLOUDFIELD_DEFAULT_WIDTH', 100);
......
......@@ -80,7 +80,7 @@ class SoundCloudDefaultFormatter extends FormatterBase implements ContainerFacto
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
return [
'soundcloud_player_type' => 'classic',
'soundcloud_player_width' => SOUNDCLOUDFIELD_DEFAULT_WIDTH,
'soundcloud_player_height' => SOUNDCLOUDFIELD_DEFAULT_HTML5_PLAYER_HEIGHT,
......@@ -92,7 +92,7 @@ class SoundCloudDefaultFormatter extends FormatterBase implements ContainerFacto
'soundcloud_player_showartwork' => '',
'soundcloud_player_showcomments' => TRUE,
'soundcloud_player_showplaycount' => '',
) + parent::defaultSettings();
] + parent::defaultSettings();
}
/**
......@@ -101,102 +101,102 @@ class SoundCloudDefaultFormatter extends FormatterBase implements ContainerFacto
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['soundcloud_player_type'] = array(
$elements['soundcloud_player_type'] = [
'#title' => $this->t('HTML5 player type'),
'#description' => $this->t('Select which HTML5 player to use.'),
'#type' => 'select',
'#default_value' => $this->getSetting('soundcloud_player_type'),
'#options' => array(
'classic' => 'Classic',
'visual' => 'Visual Player (new)',
),
);
'#options' => [
'classic' => t('Classic'),
'visual' => t('Visual Player (new)'),
],
];
$elements['soundcloud_player_width'] = array(
$elements['soundcloud_player_width'] = [
'#type' => 'textfield',
'#title' => $this->t('Width'),
'#size' => 4,
'#default_value' => $this->getSetting('soundcloud_player_width'),
'#description' => $this->t('Player width in percent. Default is @width.', array('@width' => SOUNDCLOUDFIELD_DEFAULT_WIDTH)),
);
'#description' => $this->t('Player width in percent. Default is @width.', ['@width' => SOUNDCLOUDFIELD_DEFAULT_WIDTH]),
];
$elements['soundcloud_player_height'] = array(
$elements['soundcloud_player_height'] = [
'#type' => 'textfield',
'#title' => $this->t('Height'),
'#size' => 4,
'#default_value' => $this->getSetting('soundcloud_player_height'),
'#states' => array(
'visible' => array(
':input[name*="soundcloud_player_type"]' => array('value' => 'classic'),
),
),
);
'#states' => [
'visible' => [
':input[name*="soundcloud_player_type"]' => ['value' => 'classic'],
],
],
];
$elements['soundcloud_player_height_sets'] = array(
$elements['soundcloud_player_height_sets'] = [
'#type' => 'textfield',
'#title' => $this->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'),
),
),
);
'#states' => [
'visible' => [
':input[name*="soundcloud_player_type"]' => ['value' => 'classic'],
],
],
];
$elements['soundcloud_player_visual_height'] = array(
$elements['soundcloud_player_visual_height'] = [
'#type' => 'select',
'#title' => $this->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'),
),
),
);
'#options' => [
300 => t('300px'),
450 => t('450px'),
600 => t('600px'),
],
'#states' => [
'visible' => [
':input[name*="soundcloud_player_type"]' => ['value' => 'visual'],
],
],
];
$elements['soundcloud_player_autoplay'] = array(
$elements['soundcloud_player_autoplay'] = [
'#type' => 'checkbox',
'#title' => $this->t('Play audio automatically when loaded (autoplay).'),
'#default_value' => $this->getSetting('soundcloud_player_autoplay'),
);
];
$elements['soundcloud_player_color'] = array(
$elements['soundcloud_player_color'] = [
'#type' => 'textfield',
'#title' => $this->t('Player color.'),
'#default_value' => $this->getSetting('soundcloud_player_color'),
'#description' => $this->t('Player color in hexadecimal format. Default is ff7700. Turn on the jQuery Colorpicker module if available.'),
);
];
$elements['soundcloud_player_hiderelated'] = array(
$elements['soundcloud_player_hiderelated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Hide related tracks.'),
'#default_value' => $this->getSetting('soundcloud_player_hiderelated'),
);
];
$elements['soundcloud_player_showartwork'] = array(
$elements['soundcloud_player_showartwork'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show artwork.'),
'#default_value' => $this->getSetting('soundcloud_player_showartwork'),
);
];
$elements['soundcloud_player_showcomments'] = array(
$elements['soundcloud_player_showcomments'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show comments.'),
'#default_value' => $this->getSetting('soundcloud_player_showcomments'),
);
];
$elements['soundcloud_player_showplaycount'] = array(
$elements['soundcloud_player_showplaycount'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show play count.'),
'#default_value' => $this->getSetting('soundcloud_player_showplaycount'),
);
];
return $elements;
}
......@@ -205,11 +205,8 @@ class SoundCloudDefaultFormatter extends FormatterBase implements ContainerFacto
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$settings = $this->getSettings();
$summary = [];
$summary[] = $this->t('Displays the SoundCloud player.');
return $summary;
}
......@@ -217,7 +214,7 @@ class SoundCloudDefaultFormatter extends FormatterBase implements ContainerFacto
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$elements = [];
$settings = $this->getSettings();
// Get the "common" settings.
......@@ -319,17 +316,17 @@ class SoundCloudDefaultFormatter extends FormatterBase implements ContainerFacto
unset($item->_attributes);
// Render each element as markup.
$elements[$delta] = array(
$elements[$delta] = [
'#markup' => $output,
'#allowed_tags' => ['iframe'],
);
];
}
return $elements;
}
/**
* Get data by url used httpClient.
* Get data from url using httpClient.
*/
public function fetchSoundCloudData($url) {
try {
......@@ -345,19 +342,4 @@ class SoundCloudDefaultFormatter extends FormatterBase implements ContainerFacto
return $data;
}
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),
],
];
}
}
......@@ -21,17 +21,17 @@ use Drupal\Core\Url;
* )
*/
class SoundCloudLinkFormatter extends FormatterBase {
// investigate: extends UriLinkFormatter
// @todo Investigate using extends UriLinkFormatter.
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
return [
'trim_length' => '80',
'rel' => '',
'target' => '',
) + parent::defaultSettings();
] + parent::defaultSettings();
}
/**
......@@ -40,28 +40,28 @@ class SoundCloudLinkFormatter extends FormatterBase {
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['trim_length'] = array(
$elements['trim_length'] = [
'#type' => 'number',
'#title' => $this->t('Trim link text length'),
'#field_suffix' => $this->t('characters'),
'#default_value' => $this->getSetting('trim_length'),
'#min' => 1,
'#description' => $this->t('Leave blank to allow unlimited link text lengths.'),
);
];
$elements['rel'] = array(
$elements['rel'] = [
'#type' => 'checkbox',
'#title' => $this->t('Add rel="nofollow" to links'),
'#return_value' => 'nofollow',
'#default_value' => $this->getSetting('rel'),
);
];
$elements['target'] = array(
$elements['target'] = [
'#type' => 'checkbox',
'#title' => $this->t('Open link in new window'),
'#return_value' => '_blank',
'#default_value' => $this->getSetting('target'),
);
];
return $elements;
}
......@@ -70,20 +70,20 @@ class SoundCloudLinkFormatter extends FormatterBase {
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$summary = [];
$settings = $this->getSettings();
$summary[] = $this->t('Displays the SoundCloud link.');
if (!empty($settings['trim_length'])) {
$summary[] = $this->t('Link text trimmed to @limit characters', array('@limit' => $settings['trim_length']));
$summary[] = $this->t('Link text trimmed to @limit characters', ['@limit' => $settings['trim_length']]);
}
else {
$summary[] = $this->t('Link text not trimmed');
}
if (!empty($settings['rel'])) {
$summary[] = $this->t('Add rel="@rel"', array('@rel' => $settings['rel']));
$summary[] = $this->t('Add rel="@rel"', ['@rel' => $settings['rel']]);
}
if (!empty($settings['target'])) {
$summary[] = $this->t('Open link in new window');
......@@ -96,7 +96,7 @@ class SoundCloudLinkFormatter extends FormatterBase {
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$elements = [];
$settings = $this->getSettings();
foreach ($items as $delta => $item) {
......@@ -108,15 +108,15 @@ class SoundCloudLinkFormatter extends FormatterBase {
$link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
}
$elements[$delta] = array(
$elements[$delta] = [
'#type' => 'link',
'#url' => Url::fromUri($item->url),
'#title' => $link_title,
'#options' => array(),
);
'#options' => [],
];
if (!empty($item->_attributes)) {
$elements[$delta]['#options'] += array('attributes' => array());
$elements[$delta]['#options'] += ['attributes' => []];
$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.
......
......@@ -2,10 +2,8 @@
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;
/**
......
......@@ -35,26 +35,23 @@ class SoundCloudItem extends FieldItemBase {
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'url' => array(
return [
'columns' => [
'url' => [
'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();
$element = [];
return $element;
}
......@@ -75,16 +72,15 @@ class SoundCloudItem extends FieldItemBase {
$constraints = parent::getConstraints();
$max_length = 512;
$constraints[] = $constraint_manager->create('ComplexData', array(
'url' => array(
'Length' => array(
$constraints[] = $constraint_manager->create('ComplexData', [
'url' => [
'Length' => [
'max' => $max_length,
'maxMessage' => $this->t('%name: the SoundCloud URL may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length)),
)
),
));
'maxMessage' => $this->t('%name: the SoundCloud URL may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length]),
],
],
]);
return $constraints;
}
}
......@@ -3,7 +3,6 @@
namespace Drupal\soundcloudfield\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
......@@ -25,9 +24,6 @@ class SoundCloudWidget extends WidgetBase {
* {@inheritdoc}
*/
public static function defaultSettings() {
// todo: investigate
// $settings = parent::defaultSettings();
return [
'url' => '',
'placeholder_url' => '',
......@@ -41,24 +37,24 @@ class SoundCloudWidget extends WidgetBase {
$item = $items[$delta];
// Build the element render array.
$element['url'] = array(
'#type' => 'url', // investigate, other types? (textfield)
$element['url'] = [
'#type' => 'url',
'#title' => $this->t('SoundCloud URL'),
'#placeholder' => $this->getSetting('placeholder_url'), // investigate
'#placeholder' => $this->getSetting('placeholder_url'),
'#default_value' => isset($item->url) ? $item->url : NULL,
'#element_validate' => [[get_called_class(), 'validateSoundCloudUriElement']],
'#maxlength' => 2048,
'#required' => $element['#required'],
);
];
if (empty($element['url']['#description'])) {
$element['url']['#description'] = $this->t('Enter the SoundCloud URL. A valid example: https://soundcloud.com/archives-5/purl-form-is-emptiness.');
}
if ($this->fieldDefinition->getFieldStorageDefinition()->getCardinality() == 1) {
$element += array(
$element += [
'#type' => 'fieldset',
);
];
}
return $element;
......@@ -70,12 +66,12 @@ class SoundCloudWidget extends WidgetBase {
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['placeholder_url'] = array(
$elements['placeholder_url'] = [
'#type' => 'textfield',
'#title' => $this->t('Placeholder for URL'),
'#default_value' => $this->getSetting('placeholder_url'),
'#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
);
];
return $elements;
}
......@@ -84,7 +80,7 @@ class SoundCloudWidget extends WidgetBase {
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$summary = [];
$placeholder_url = $this->getSetting('placeholder_url');
if (empty($placeholder_url)) {
......@@ -92,7 +88,7 @@ class SoundCloudWidget extends WidgetBase {
}
else {
if (!empty($placeholder_url)) {
$summary[] = $this->t('URL placeholder: @placeholder_url', array('@placeholder_url' => $placeholder_url));
$summary[] = $this->t('URL placeholder: @placeholder_url', ['@placeholder_url' => $placeholder_url]);
}
}
......
......@@ -8,13 +8,13 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\Display\EntityDisplayRepositoryInterface;
/**
* Test case for custom time field
* Test case for custom SoundCloud field.
*
* @group soundcloudfield
*/
class SoundCloudWidgetValidationTest extends WebTestBase {
/**
* Modules to install
* Modules to install.
*
* @var array
*/
......@@ -22,7 +22,7 @@ class SoundCloudWidgetValidationTest extends WebTestBase {
/**
* Random fieldName
* Random fieldName.
*/
protected $fieldName;
......@@ -40,19 +40,22 @@ class SoundCloudWidgetValidationTest extends WebTestBase {
*/
protected $field;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser([
'view test entity',
'administer entity_test content',
'link to any page',
]));
'view test entity',
'administer entity_test content',
'link to any page',
]));
}
/**
* Test an a valid SoundCloud URL
* Test for valid SoundCloud URL.
*/
function testSoundCloudUrlValid() {
public function testSoundCloudUrlValid() {
$field = $this->getField();
// Display creation form.
......@@ -72,9 +75,9 @@ class SoundCloudWidgetValidationTest extends WebTestBase {
}
/**
* Test an invalid URLs or strings
* Test for invalid URLs or strings.
*/
function testSoundCloudUrlInvalid() {
public function testSoundCloudUrlInvalid() {
$field = $this->getField();
// Display creation form.
......@@ -138,50 +141,51 @@ class SoundCloudWidgetValidationTest extends WebTestBase {
*/
private function getField() {
$this->fieldName = mb_strtolower($this->randomMachineName());
//db field
// DB field.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'soundcloud',
'cardinality' => 1,
]);
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'soundcloud',
'cardinality' => 1,
]);
$this->fieldStorage->save();
//field type
// Field type.
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'settings' => [
'title' => DRUPAL_DISABLED,
],
]);
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'settings' => [
'title' => DRUPAL_DISABLED,
],
]);