Skip to content
Snippets Groups Projects
Commit 11653d49 authored by Kostia Bohach's avatar Kostia Bohach
Browse files

Issue #3404122: Coding standards

parent 251756e9
No related branches found
No related tags found
1 merge request!6Issue #3404122: Coding standards
<?php
/**
* @file
* Contains \Drupal\videojs\Plugin\Field\FieldFormatter\VideJsPlayerFormatter.
*/
namespace Drupal\videojs\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Link;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
/**
* Plugin implementation of the 'videojs_player' formatter.
......@@ -29,9 +24,10 @@ use Drupal\Core\Cache\Cache;
* }
* )
*/
class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* The current user.
*
......@@ -39,6 +35,13 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
*/
protected $currentUser;
/**
* The file URL generator.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
protected $fileUrlGenerator;
/**
* Constructs an VideoPlayerFormatter object.
*
......@@ -58,10 +61,13 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
* Any third party settings settings.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
* The file URL generator.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, FileUrlGeneratorInterface $file_url_generator) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->currentUser = $current_user;
$this->fileUrlGenerator = $file_url_generator;
}
/**
......@@ -77,6 +83,7 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('current_user'),
$container->get('file_url_generator')
);
}
......@@ -84,15 +91,15 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
return [
'width' => '854',
'height' => '480',
'controls' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'muted' => FALSE,
'preload' => 'none'
) + parent::defaultSettings();
'preload' => 'none',
] + parent::defaultSettings();
}
/**
......@@ -135,12 +142,12 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
'#title' => t('Preload'),
'#type' => 'select',
'#default_value' => $this->getSetting('preload'),
'#options' => array(
'none' =>'none',
'#options' => [
'none' => 'none',
'metadata' => 'metadata',
'auto' => 'auto'
),
'#description' => t('Hint to the browser about whether optimistic downloading of the video itself or its metadata is considered worthwhile.')
'auto' => 'auto',
],
'#description' => t('Hint to the browser about whether optimistic downloading of the video itself or its metadata is considered worthwhile.'),
];
return $element;
}
......@@ -149,22 +156,23 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$summary = [];
$summary[] = t('HTML5 Video (@widthx@height@controls@autoplay@loop@muted).', [
'@width' => $this->getSetting('width'),
'@height' => $this->getSetting('height'),
'@controls' => $this->getSetting('controls') ? t(', controls') : '' ,
'@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '' ,
'@loop' => $this->getSetting('loop') ? t(', looping') : '' ,
'@muted' => $this->getSetting('muted') ? t(', muted') : '',
'@controls' => $this->getSetting('controls') ? $this->t(', controls') : '',
'@autoplay' => $this->getSetting('autoplay') ? $this->t(', autoplaying') : '',
'@loop' => $this->getSetting('loop') ? $this->t(', looping') : '' ,
'@muted' => $this->getSetting('muted') ? $this->t(', muted') : '',
]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$elements = [];
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
......@@ -175,14 +183,14 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
// Collect cache tags to be added for each item in the field.
foreach ($files as $delta => $file) {
$video_uri = $file->getFileUri();
$elements[$delta] = array(
$elements[$delta] = [
'#theme' => 'videojs',
'#items' => array(Url::fromUri(file_create_url($video_uri))),
'#items' => [Url::fromUri($this->fileUrlGenerator->generateAbsoluteString($video_uri))],
'#player_attributes' => $this->getSettings(),
'#attached' => array(
'library' => array('videojs/videojs'),
),
);
'#attached' => [
'library' => ['videojs/videojs'],
],
];
}
return $elements;
}
......@@ -191,12 +199,10 @@ class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements Conta
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_form_display = \Drupal::service('entity_display.repository')->getFormDisplay($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
$widget = $entity_form_display->getRenderer($field_definition->getName());
$widget_id = $widget->getBaseId();
if(!$field_definition->isList()){
if (!$field_definition->isList()) {
return TRUE;
}
return FALSE;
}
}
<?php
/**
* @file
* Contains \Drupal\videojs\Plugin\Field\FieldFormatter\VideJsPlayerFormatterBase.
*/
namespace Drupal\videojs\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
/**
......@@ -22,4 +16,5 @@ abstract class VideoJsPlayerFormatterBase extends FileFormatterBase {
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
return parent::getEntitiesToView($items, $langcode);
}
}
<?php
/**
* @file
* Contains \Drupal\videojs\Plugin\Field\FieldFormatter\VideoJsPlayerListFormatter.
*/
namespace Drupal\videojs\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
use Drupal\videojs\Plugin\Field\FieldFormatter\VideoJsPlayerFormatter;
/**
* Plugin implementation of the 'videojs_player_list' formatter.
......@@ -30,14 +19,13 @@ use Drupal\videojs\Plugin\Field\FieldFormatter\VideoJsPlayerFormatter;
* }
* )
*/
class VideoJsPlayerListFormatter extends VideoJsPlayerFormatter implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$elements = [];
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
......@@ -46,19 +34,19 @@ class VideoJsPlayerListFormatter extends VideoJsPlayerFormatter implements Conta
}
// Collect cache tags to be added for each item in the field.
$video_items = array();
foreach ($files as $delta => $file) {
$video_items = [];
foreach ($files as $file) {
$video_uri = $file->getFileUri();
$video_items[] = Url::fromUri(file_create_url($video_uri));
$video_items[] = Url::fromUri($this->fileUrlGenerator->generateAbsoluteString($video_uri));
}
$elements[] = array(
$elements[] = [
'#theme' => 'videojs',
'#items' => $video_items,
'#player_attributes' => $this->getSettings(),
'#attached' => array(
'library' => array('videojs/videojs'),
),
);
'#attached' => [
'library' => ['videojs/videojs'],
],
];
return $elements;
}
......@@ -66,12 +54,10 @@ class VideoJsPlayerListFormatter extends VideoJsPlayerFormatter implements Conta
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_form_display = \Drupal::service('entity_display.repository')->getFormDisplay($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
$widget = $entity_form_display->getRenderer($field_definition->getName());
$widget_id = $widget->getBaseId();
if($field_definition->isList()){
if ($field_definition->isList()) {
return TRUE;
}
return FALSE;
}
}
<?php
/**
* @file
* Installation file for Video.js module.
......@@ -8,7 +9,7 @@
* Implements hook_requirements().
*/
function videojs_requirements($phase) {
$requirements = array();
$requirements = [];
$config = \Drupal::config('videojs.settings');
$directory = $config->get('videojs_directory');
......@@ -17,12 +18,19 @@ function videojs_requirements($phase) {
$videojs_version = videojs_get_version();
if ($videojs_version == NULL) {
$requirements['videojs']['value'] = t('Not found');
$requirements['videojs']['description'] = t('Missing the Video.js library. Please <a href="!url">download Video.js</a> and extract it into the %directory directory.', array('!url' => 'http://videojs.com', '%directory' => $directory));
$requirements['videojs']['description'] = t('Missing the Video.js library. Please <a href="!url">download Video.js</a> and extract it into the %directory directory.', [
'!url' => 'http://videojs.com',
'%directory' => $directory,
]);
$requirements['videojs']['severity'] = REQUIREMENT_ERROR;
}
elseif (version_compare($videojs_version, '5', '<')) {
$requirements['videojs']['value'] = t('Unsupported Video.js version @version', array('@version' => $videojs_version));
$requirements['videojs']['description'] = t('The installed version of the Video.js library is not supported. This version of the Video.js module only supports Video.js version 5. <a href="@videojs-url">Download Video.js version 5</a> and extract it into the %directory directory or <a href="@videojs-module-url">download Video.js module version 7.x-2.x</a> to use Video.js 3.', array('@videojs-url' => 'http://videojs.com', '%directory' => $directory, '@videojs-module-url' => 'http://drupal.org/project/videojs'));
$requirements['videojs']['value'] = t('Unsupported Video.js version @version', ['@version' => $videojs_version]);
$requirements['videojs']['description'] = t('The installed version of the Video.js library is not supported. This version of the Video.js module only supports Video.js version 5. <a href="@videojs-url">Download Video.js version 5</a> and extract it into the %directory directory or <a href="@videojs-module-url">download Video.js module version 7.x-2.x</a> to use Video.js 3.', [
'@videojs-url' => 'http://videojs.com',
'%directory' => $directory,
'@videojs-module-url' => 'http://drupal.org/project/videojs',
]);
$requirements['videojs']['severity'] = REQUIREMENT_ERROR;
}
else {
......
......@@ -5,45 +5,40 @@
* Exposes global functionality for video.js fields.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\Entity\File;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field\FieldConfigInterface;
/**
* Implements hook_theme().
*/
function videojs_theme() {
return array(
'videojs' => array(
'variables' => array('items' => NULL, 'player_attributes' => NULL)
),
);
return [
'videojs' => [
'variables' => ['items' => NULL, 'player_attributes' => NULL],
],
];
}
/**
* Return the version of Video.js installed.
*
* @param $path
* @param string|null $path
* The path to check for a Video.js installation. This can be a local path
* like sites/all/libraries/video-js or a remote path like
* http://mycdn.com/videojs. Do not add a trailing slash.
* Defaults to videojs_directory when using the local file path location
* or whatever location the Libraries API determines.
*
* @return
* @return mixed|null
* The version found or NULL if no version found.
*/
function videojs_get_version($path = NULL) {
$version = NULL;
$config = \Drupal::config('videojs.settings');
if (!isset($path)) {
$path = $config->get('videojs_directory');
};
// When admins specify a protocol-relative URL, add http because file_get_contents doesn't understand it.
// When admins specify a protocol-relative URL, add http because
// file_get_contents doesn't understand it.
if (strncmp('//', $path, 2) === 0) {
$path = 'http:' . $path;
}
......@@ -52,10 +47,10 @@ function videojs_get_version($path = NULL) {
// Now admins can also refer to directories like http://mycdn.com/videojs.
$contents = @file_get_contents($path . '/video.js', FALSE, NULL, 0, 400);
if (!empty($contents)) {
$matches = array();
$matches = [];
if (preg_match('/([\d.]{3,})/i', $contents, $matches)) {
$version = $matches[1];
}
}
return $version;
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment