Skip to content
Snippets Groups Projects
Commit 4ec045d1 authored by Joshua Sedler's avatar Joshua Sedler :cartwheel_tone2: Committed by Julian Pustkuchen
Browse files

Issue #3521021: Add parent calls, adjust templates and fix formatter settings

parent 3ea70888
No related branches found
No related tags found
1 merge request!3Issue #3521021: Add parent calls, adjust templates and fix formatter settings
Pipeline #481924 passed with warnings
...@@ -6,9 +6,12 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; ...@@ -6,9 +6,12 @@ use Drupal\Core\StringTranslation\StringTranslationTrait;
/** /**
* Shared trait for Vidstack Player video formatters. * Shared trait for Vidstack Player video formatters.
*
* NOTE, this trait does not portray ALL shared settings. Notably:
* 'controls', 'autoplay' and 'loop' are shared, but not part of this trait.
* This is because they get partially inherited by some formatters parent class.
*/ */
trait VidstackPlayerFormatterTrait { trait VidstackPlayerFormatterTrait {
use StringTranslationTrait; use StringTranslationTrait;
/** /**
...@@ -20,60 +23,67 @@ trait VidstackPlayerFormatterTrait { ...@@ -20,60 +23,67 @@ trait VidstackPlayerFormatterTrait {
* @return array * @return array
* The shared settings to reduce duplicate code. * The shared settings to reduce duplicate code.
*/ */
public function getSharedSettings(array $form) { protected function getSharedSettings(array $form) {
$form['autoplay'] = [ // @see Trait class docblock.
'#title' => $this->t('Autoplay'),
'#type' => 'checkbox',
'#description' => $this->t('Play the video immediately upon page load. This is generally advised against on UX grounds. It is also disabled by default in some browsers.'),
'#default_value' => $this->getSetting('autoplay'),
];
$form['loop'] = [
'#title' => $this->t('Loop'),
'#type' => 'checkbox',
'#description' => $this->t('Loop the video.'),
'#default_value' => $this->getSetting('loop'),
];
$form['muted'] = [ $form['muted'] = [
'#title' => $this->t('Muted'), '#title' => $this->t('Muted'),
'#type' => 'checkbox', '#type' => 'checkbox',
'#description' => $this->t('Start the video muted.'), '#description' => $this->t('Start the video muted.'),
'#default_value' => $this->getSetting('muted'), '#default_value' => $this->getSetting('muted'),
]; ];
$form['controls'] = [
'#title' => $this->t('Default controls'),
'#type' => 'checkbox',
'#description' => $this->t('Show the default controls of the video (e.g. from the browser itself or a provider like YouTube).'),
'#default_value' => $this->getSetting('controls'),
];
$form['load'] = [ $form['load'] = [
'#title' => $this->t('Load strategy'), '#title' => $this->t('Load strategy'),
'#type' => 'select', '#type' => 'select',
'#options' => [ '#options' => $this->getLoadStrategyOptions(),
'eager' => $this->t('Load immediately'),
'idle' => $this->t('Load after the page is loaded'),
'visible' => $this->t('Load after becoming visible'),
'play' => $this->t('Load after hitting play'),
],
'#description' => $this->t('The load strategy that will be used to determine when the video content should start loading.'), '#description' => $this->t('The load strategy that will be used to determine when the video content should start loading.'),
'#default_value' => $this->getSetting('load'), '#default_value' => $this->getSetting('load'),
]; ];
return $form; return $form;
} }
/**
* Returns the load strategy form options.
*
* @return array
* The load strategy options.
*/
protected function getLoadStrategyOptions() {
return [
'eager' => $this->t('Load immediately'),
'idle' => $this->t('Load after the page is loaded'),
'visible' => $this->t('Load after becoming visible'),
'play' => $this->t('Load after hitting play'),
];
}
/** /**
* Returns the default settings which are shared between formatters. * Returns the default settings which are shared between formatters.
* *
* @return array * @return array
* The shared default settings to reduce duplicate code. * The shared default settings to reduce duplicate code.
*/ */
public static function getSharedDefaultSettings() { protected static function getSharedDefaultSettings() {
return [ return [
'autoplay' => FALSE,
'loop' => FALSE,
'muted' => FALSE, 'muted' => FALSE,
'controls' => FALSE,
'load' => 'visible', 'load' => 'visible',
]; ];
} }
/**
* Returns the settings summary shared between formatters.
*
* @return array
* The shared settings summary.
*/
protected function getSharedSettingsSummary() {
$summary = [];
$summary[] = $this->t('Muted: %muted', [
'%muted' => $this->getSetting('muted') ? $this->t('yes') : $this->t('no'),
]);
$summary[] = $this->t('Load strategy: %load', [
'%load' => $this->getLoadStrategyOptions()[$this->getSetting('load')],
]);
return $summary;
}
} }
...@@ -25,7 +25,6 @@ use Drupal\media\Plugin\media\Source\OEmbedInterface; ...@@ -25,7 +25,6 @@ use Drupal\media\Plugin\media\Source\OEmbedInterface;
* ) * )
*/ */
class VidstackPlayerRemoteVideoFormatter extends FormatterBase { class VidstackPlayerRemoteVideoFormatter extends FormatterBase {
use VidstackPlayerFormatterTrait; use VidstackPlayerFormatterTrait;
/** /**
...@@ -80,20 +79,17 @@ class VidstackPlayerRemoteVideoFormatter extends FormatterBase { ...@@ -80,20 +79,17 @@ class VidstackPlayerRemoteVideoFormatter extends FormatterBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function settingsSummary() { public function settingsSummary() {
$loadStrategies = [ $summary = parent::settingsSummary();
'eager' => $this->t('Load immediately'), $summary[] = $this->t('Playback controls: %controls', [
'idle' => $this->t('Load after page-load'), '%controls' => $this->getSetting('controls') ? $this->t('visible') : $this->t('hidden'),
'visible' => $this->t('Load after visible'), ]);
'play' => $this->t('Load after play'), $summary[] = $this->t('Autoplay: %autoplay', [
]; '%autoplay' => $this->getSetting('autoplay') ? $this->t('yes') : $this->t('no'),
$summary = []; ]);
$summary[] = $this->t('Autoplay: <strong>@autoplay</strong><br>Loop: <strong>@loop</strong><br>Muted: <strong>@muted</strong><br>Default controls: <strong>@controls</strong><br>Load strategy: <strong>@load</strong>', [ $summary[] = $this->t('Loop: %loop', [
'@autoplay' => $this->getSetting('autoplay') ? $this->t('Yes') : $this->t('No'), '%loop' => $this->getSetting('loop') ? $this->t('yes') : $this->t('no'),
'@loop' => $this->getSetting('loop') ? $this->t('Yes') : $this->t('No'),
'@muted' => $this->getSetting('muted') ? $this->t('Yes') : $this->t('No'),
'@controls' => $this->getSetting('controls') ? $this->t('Yes') : $this->t('No'),
'@load' => $loadStrategies[$this->getSetting('load')],
]); ]);
$summary += $this->getSharedSettingsSummary();
return $summary; return $summary;
} }
...@@ -101,14 +97,36 @@ class VidstackPlayerRemoteVideoFormatter extends FormatterBase { ...@@ -101,14 +97,36 @@ class VidstackPlayerRemoteVideoFormatter extends FormatterBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function defaultSettings() { public static function defaultSettings() {
return self::getSharedDefaultSettings(); $settings = parent::defaultSettings();
$settings['controls'] = FALSE;
$settings['autoplay'] = FALSE;
$settings['loop'] = FALSE;
$settings += self::getSharedDefaultSettings();
return $settings;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function settingsForm(array $form, FormStateInterface $form_state) { public function settingsForm(array $form, FormStateInterface $form_state) {
return $this->getSharedSettings($form); $form = parent::settingsForm($form, $form_state);
$form['controls'] = [
'#title' => $this->t('Show playback controls'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('controls'),
];
$form['autoplay'] = [
'#title' => $this->t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('autoplay'),
];
$form['loop'] = [
'#title' => $this->t('Loop'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('loop'),
];
$form += $this->getSharedSettings($form);
return $form;
} }
/** /**
......
...@@ -24,7 +24,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -24,7 +24,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* ) * )
*/ */
class VidstackPlayerVideoFormatter extends FileMediaFormatterBase implements FileMediaFormatterInterface { class VidstackPlayerVideoFormatter extends FileMediaFormatterBase implements FileMediaFormatterInterface {
use VidstackPlayerFormatterTrait; use VidstackPlayerFormatterTrait;
/** /**
...@@ -86,38 +85,21 @@ class VidstackPlayerVideoFormatter extends FileMediaFormatterBase implements Fil ...@@ -86,38 +85,21 @@ class VidstackPlayerVideoFormatter extends FileMediaFormatterBase implements Fil
* {@inheritdoc} * {@inheritdoc}
*/ */
public function settingsSummary() { public function settingsSummary() {
$loadStrategies = [ $summary = parent::settingsSummary();
'eager' => $this->t('Load immediately'), $summary += $this->getSharedSettingsSummary();
'idle' => $this->t('Load after page-load'), $posterValue = match($this->getSetting('poster')) {
'visible' => $this->t('Load after visible'), 'none' => $this->t('None (Show first frame)'),
'play' => $this->t('Load after play'), 'custom' => $this->t('Custom URL'),
]; default => $this->t('Field "%name" (%style)', [
$summary = []; '%name' => $this->getSetting('poster'),
$posterValue = ''; '%style' => $this->getSetting('posterStyle') === '' ? $this->t('None (original image)') : image_style_options(TRUE)[$this->getSetting('posterStyle')],
switch ($this->getSetting('poster')) { ]),
case 'none': };
$posterValue = $this->t('None (Show first frame)'); $summary[] = $this->t('Poster image: %poster', [
break; '%poster' => $posterValue,
]);
case 'custom': $summary[] = $this->t('Poster load strategy: %load', [
$posterValue = $this->t('Custom URL'); '%load' => $this->getLoadStrategyOptions()[$this->getSetting('posterLoad')],
break;
default:
$posterValue = $this->t('Field "@name" (@style)', [
'@name' => $this->getSetting('poster'),
'@style' => $this->getSetting('posterStyle') === '' ? $this->t('None (original image)') : image_style_options(TRUE)[$this->getSetting('posterStyle')],
]);
break;
}
$summary[] = $this->t('Autoplay: <strong>@autoplay</strong><br>Loop: <strong>@loop</strong><br>Muted: <strong>@muted</strong><br>Default controls: <strong>@controls</strong><br>Load strategy: <strong>@load</strong><br>Poster image: <strong>@poster</strong><br>Poster load strategy: <strong>@posterLoad</strong>', [
'@autoplay' => $this->getSetting('autoplay') ? $this->t('Yes') : $this->t('No'),
'@loop' => $this->getSetting('loop') ? $this->t('Yes') : $this->t('No'),
'@muted' => $this->getSetting('muted') ? $this->t('Yes') : $this->t('No'),
'@controls' => $this->getSetting('controls') ? $this->t('Yes') : $this->t('No'),
'@load' => $loadStrategies[$this->getSetting('load')],
'@poster' => $posterValue,
'@posterLoad' => $loadStrategies[$this->getSetting('posterLoad')],
]); ]);
return $summary; return $summary;
} }
...@@ -126,19 +108,21 @@ class VidstackPlayerVideoFormatter extends FileMediaFormatterBase implements Fil ...@@ -126,19 +108,21 @@ class VidstackPlayerVideoFormatter extends FileMediaFormatterBase implements Fil
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function defaultSettings() { public static function defaultSettings() {
return self::getSharedDefaultSettings() + [ $settings = parent::defaultSettings();
'poster' => 'none', $settings += self::getSharedDefaultSettings();
'posterUrl' => '', $settings['poster'] = 'none';
'posterLoad' => 'visible', $settings['posterUrl'] = '';
'posterStyle' => '', $settings['posterLoad'] = 'visible';
]; $settings['posterStyle'] = '';
return $settings;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function settingsForm(array $form, FormStateInterface $form_state) { public function settingsForm(array $form, FormStateInterface $form_state) {
$form = $this->getSharedSettings($form); $form = parent::settingsForm($form, $form_state);
$form += $this->getSharedSettings($form);
$form['poster'] = [ $form['poster'] = [
'#title' => $this->t('Poster image'), '#title' => $this->t('Poster image'),
......
<iframe {{ attributes }} allowfullscreen></iframe> {#
NOTE: We are using "progressive enhancement" here, as documented:
"The target can be any element but if it’s a <audio>, <video>, or
<iframe> element it will be replaced and enhanced (i.e., progressive
enhancement)."
@see https://vidstack.io/docs/player/getting-started/installation/javascript/?provider=youtube&styling=default-layout&install=cdn
#}
<iframe {{ attributes }}></iframe>
<video {{ attributes }} playsinline> {#
NOTE: We are using "progressive enhancement" here, as documented:
"The target can be any element but if it’s a <audio>, <video>, or
<iframe> element it will be replaced and enhanced (i.e., progressive
enhancement)."
@see https://vidstack.io/docs/player/getting-started/installation/javascript/?provider=video&styling=default-layout&install=cdn
#}
<video {{ attributes }}>
{% for file in files %} {% for file in files %}
<source {{ file.source_attributes }} /> <source {{ file.source_attributes }} />
{% endfor %} {% endfor %}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment