From aebf64fe8ccd3f0ee874b591d8fc8aa98501194e Mon Sep 17 00:00:00 2001 From: abd bataineh Date: Wed, 4 Nov 2020 15:53:44 +0300 Subject: [PATCH] supported drupal 9 --- composer.json | 24 +--- .../src/Form/PresetFormBase.php | 18 +-- .../VideoEmbedPlayerFormatter.php | 21 +--- .../VideoEmbedThumbnailFormatter.php | 41 ++----- .../FieldFormatter/VideoPlayerFormatter.php | 23 +--- .../VideoPlayerListFormatter.php | 21 +--- .../FieldFormatter/VideoUrlFormatter.php | 108 ++++++++++++++++++ src/Plugin/Field/FieldType/VideoItem.php | 19 +-- .../Field/FieldWidget/VideoEmbedWidget.php | 2 +- .../VideoRemoteStreamWrapper.php | 2 +- .../src/Kernel/VideoItemSerializationTest.php | 69 +++++++++++ video.info.yml | 10 +- 12 files changed, 233 insertions(+), 125 deletions(-) create mode 100644 src/Plugin/Field/FieldFormatter/VideoUrlFormatter.php create mode 100644 tests/src/Kernel/VideoItemSerializationTest.php diff --git a/composer.json b/composer.json index c62f03a..7d38d11 100644 --- a/composer.json +++ b/composer.json @@ -3,33 +3,11 @@ "description": "Video module allows you to embedded videos from YouTube, Vimeo, Facebook, Vine etc (Drupal 8 only) and upload videos and play using HTML5 video player.", "type": "drupal-module", "homepage": "https://www.drupal.org/project/video", - "keywords": ["Drupal", "media", "video", "transcode", "embed"], "license": "GPL-2.0+", - "authors": [ - { - "name": "Heshan Wanigasooriya", - "homepage": "https://www.drupal.org/u/heshanlk", - "role": "Maintainer" - } - ], - "minimum-stability": "8.x-2.x", "extra": { "drupal": { "package": "Field types", - "version": "8.x-2.x" - } - }, - "support": { - "issues": "https://www.drupal.org/project/issues/video", - "source": "https://www.drupal.org/project/video" - }, - "repositories": [ - { - "type": "composer", - "url": "https://packages.drupal.org/8" + "version": "8.x-1.x" } - ], - "require": { - "drupal/core": "~8.1", } } \ No newline at end of file diff --git a/modules/video_transcode/src/Form/PresetFormBase.php b/modules/video_transcode/src/Form/PresetFormBase.php index 831b66a..fcc2c33 100644 --- a/modules/video_transcode/src/Form/PresetFormBase.php +++ b/modules/video_transcode/src/Form/PresetFormBase.php @@ -3,7 +3,7 @@ namespace Drupal\video_transcode\Form; use Drupal\Core\Entity\EntityForm; -use Drupal\Core\Entity\Query\QueryFactory; +use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -24,9 +24,9 @@ class PresetFormBase extends EntityForm { /** - * @var \Drupal\Core\Entity\Query\QueryFactory + * @var \Drupal\Core\Entity\EntityTypeManager */ - protected $entityQueryFactory; + protected $entityTypeManager; /** * Construct the PresetFormBase. @@ -36,11 +36,11 @@ class PresetFormBase extends EntityForm { * from the container. We later use this query factory to build an entity * query for the exists() method. * - * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory + * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager * An entity query factory for the preset entity type. */ - public function __construct(QueryFactory $query_factory) { - $this->entityQueryFactory = $query_factory; + public function __construct(EntityTypeManager $entity_type_manager) { + $this->entityTypeManager = $entity_type_manager; } /** @@ -58,7 +58,7 @@ public function __construct(QueryFactory $query_factory) { * pass the factory to our class as a constructor parameter. */ public static function create(ContainerInterface $container) { - return new static($container->get('entity.query')); + return new static($container->get('entity_type.manager')); } /** @@ -521,8 +521,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { * TRUE if this format already exists, FALSE otherwise. */ public function exists($entity_id, array $element, FormStateInterface $form_state) { - // Use the query factory to build a new preset entity query. - $query = $this->entityQueryFactory->get('video_transcode_preset'); + // Use the entity type manager to build a new preset entity query. + $query = $this->entityTypeManager->getStorage('video_transcode_preset')->getQuery(); // Query the entity ID to see if its in use. $result = $query->condition('id', $element['#field_prefix'] . $entity_id) diff --git a/src/Plugin/Field/FieldFormatter/VideoEmbedPlayerFormatter.php b/src/Plugin/Field/FieldFormatter/VideoEmbedPlayerFormatter.php index ac5fda4..17fa8b5 100644 --- a/src/Plugin/Field/FieldFormatter/VideoEmbedPlayerFormatter.php +++ b/src/Plugin/Field/FieldFormatter/VideoEmbedPlayerFormatter.php @@ -42,7 +42,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $file = File::load($item->target_id); if(!$file) continue; $metadata = isset($item->data) ? unserialize($item->data) : []; - $scheme = \Drupal::service('file_system')->uriScheme($file->getFileUri()); + #$scheme = file_uri_scheme($file->getFileUri()); + $scheme = \Drupal\Core\StreamWrapper\StreamWrapperManager::getScheme($file->getFileUri()); $provider = $this->providerManager->loadProviderFromStream($scheme, $file, $metadata); if($provider){ $element[$delta] = $provider->renderEmbedCode($settings); @@ -146,21 +147,9 @@ public static function isApplicable(FieldDefinitionInterface $field_definition) if(empty($field_definition->getTargetBundle())){ return TRUE; } - else { - $form_mode = 'default'; - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->load($field_definition->getTargetEntityTypeId() . '.' . $field_definition->getTargetBundle() . '.' . $form_mode); - if (!$entity_form_display) { - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->create([ - 'targetEntityType' => $field_definition->getTargetEntityTypeId(), - 'bundle' => $field_definition->getTargetBundle(), - 'mode' => $form_mode, - 'status' => TRUE, - ]); - } + else{ + #$entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default'); + $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($widget_id == 'video_embed'){ diff --git a/src/Plugin/Field/FieldFormatter/VideoEmbedThumbnailFormatter.php b/src/Plugin/Field/FieldFormatter/VideoEmbedThumbnailFormatter.php index 5b8f438..ae2a646 100644 --- a/src/Plugin/Field/FieldFormatter/VideoEmbedThumbnailFormatter.php +++ b/src/Plugin/Field/FieldFormatter/VideoEmbedThumbnailFormatter.php @@ -49,31 +49,20 @@ class VideoEmbedThumbnailFormatter extends FormatterBase implements ContainerFac public function viewElements(FieldItemListInterface $items, $langcode) { // load widget settings $field_definition = $this->fieldDefinition; - $form_mode = 'default'; - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->load($field_definition->getTargetEntityTypeId() . '.' . $field_definition->getTargetBundle() . '.' . $form_mode); - if (!$entity_form_display) { - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->create([ - 'targetEntityType' => $field_definition->getTargetEntityTypeId(), - 'bundle' => $field_definition->getTargetBundle(), - 'mode' => $form_mode, - 'status' => TRUE, - ]); - } + #$entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default'); + $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_settings = $widget->getSettings(); $element = []; foreach ($items as $delta => $item) { $file = File::load($item->target_id); $metadata = isset($item->data) ? unserialize($item->data) : []; - $scheme = file_uri_scheme($file->getFileUri()); - $provider = $this->providerManager->loadProviderFromStream($scheme, $file, $metadata, $widget_settings); + #$scheme = file_uri_scheme($file->getFileUri()); + $scheme = \Drupal\Core\StreamWrapper\StreamWrapperManager::getScheme($file->getFileUri()); + $provider = $this->providerManager->loadProviderFromStream($scheme, $file, $metadata, $widget_settings); $url = FALSE; if ($this->getSetting('link_image_to') == static::LINK_CONTENT) { - $url = $items->getEntity()->toUrl(); + $url = $items->getEntity()->urlInfo(); } elseif ($this->getSetting('link_image_to') == static::LINK_PROVIDER) { $url = Url::fromUri(file_create_url($file->getFileUri())); @@ -174,21 +163,9 @@ public static function isApplicable(FieldDefinitionInterface $field_definition) return TRUE; } else{ - $form_mode = 'default'; - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->load($field_definition->getTargetEntityTypeId() . '.' . $field_definition->getTargetBundle() . '.' . $form_mode); - if (!$entity_form_display) { - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->create([ - 'targetEntityType' => $field_definition->getTargetEntityTypeId(), - 'bundle' => $field_definition->getTargetBundle(), - 'mode' => $form_mode, - 'status' => TRUE, - ]); - } - $widget = $entity_form_display->getRenderer($field_definition->getName()); + #$entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default'); + $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($widget_id == 'video_embed'){ return TRUE; diff --git a/src/Plugin/Field/FieldFormatter/VideoPlayerFormatter.php b/src/Plugin/Field/FieldFormatter/VideoPlayerFormatter.php index 6a40f66..dfb69a7 100644 --- a/src/Plugin/Field/FieldFormatter/VideoPlayerFormatter.php +++ b/src/Plugin/Field/FieldFormatter/VideoPlayerFormatter.php @@ -169,15 +169,16 @@ public function viewElements(FieldItemListInterface $items, $langcode) { // Collect cache tags to be added for each item in the field. foreach ($files as $delta => $file) { $video_uri = $file->getFileUri(); + $relative_url = file_url_transform_relative(file_create_url($video_uri)); $elements[$delta] = [ '#theme' => 'video_player_formatter', - '#items' => [Url::fromUri(file_create_url($video_uri))], + '#items' => [Url::fromUserInput($relative_url)], '#player_attributes' => $this->getSettings(), ]; } return $elements; } - + /** * {@inheritdoc} */ @@ -186,20 +187,8 @@ public static function isApplicable(FieldDefinitionInterface $field_definition) return TRUE; } else{ - $form_mode = 'default'; - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->load($field_definition->getTargetEntityTypeId() . '.' . $field_definition->getTargetBundle() . '.' . $form_mode); - if (!$entity_form_display) { - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->create([ - 'targetEntityType' => $field_definition->getTargetEntityTypeId(), - 'bundle' => $field_definition->getTargetBundle(), - 'mode' => $form_mode, - 'status' => TRUE, - ]); - } + #$entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default'); + $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($widget_id == 'video_upload'){ @@ -208,4 +197,4 @@ public static function isApplicable(FieldDefinitionInterface $field_definition) } return FALSE; } -} \ No newline at end of file +} diff --git a/src/Plugin/Field/FieldFormatter/VideoPlayerListFormatter.php b/src/Plugin/Field/FieldFormatter/VideoPlayerListFormatter.php index f5f5d84..3839dc4 100644 --- a/src/Plugin/Field/FieldFormatter/VideoPlayerListFormatter.php +++ b/src/Plugin/Field/FieldFormatter/VideoPlayerListFormatter.php @@ -43,7 +43,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $video_items = []; foreach ($files as $delta => $file) { $video_uri = $file->getFileUri(); - $video_items[] = Url::fromUri(file_create_url($video_uri)); + $relative_url = file_url_transform_relative(file_create_url($video_uri)); + $video_items[] = Url::fromUserInput($relative_url); } $elements[] = [ '#theme' => 'video_player_formatter', @@ -52,7 +53,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ]; return $elements; } - + /** * {@inheritdoc} */ @@ -61,20 +62,8 @@ public static function isApplicable(FieldDefinitionInterface $field_definition) return TRUE; } else{ - $form_mode = 'default'; - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->load($field_definition->getTargetEntityTypeId() . '.' . $field_definition->getTargetBundle() . '.' . $form_mode); - if (!$entity_form_display) { - $entity_form_display = \Drupal::entityTypeManager() - ->getStorage('entity_form_display') - ->create([ - 'targetEntityType' => $field_definition->getTargetEntityTypeId(), - 'bundle' => $field_definition->getTargetBundle(), - 'mode' => $form_mode, - 'status' => TRUE, - ]); - } + #$entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default'); + $entity_form_display= \Drupal::service('entity_display.repository')->getFormDisplay($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default'); $widget = $entity_form_display->getRenderer($field_definition->getName()); if ($widget) { $widget_id = $widget->getBaseId(); diff --git a/src/Plugin/Field/FieldFormatter/VideoUrlFormatter.php b/src/Plugin/Field/FieldFormatter/VideoUrlFormatter.php new file mode 100644 index 0000000..12c1839 --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/VideoUrlFormatter.php @@ -0,0 +1,108 @@ +currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('current_user') + ); + } + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + $elements = array(); + $files = $this->getEntitiesToView($items, $langcode); + + // Early opt-out if the field is empty. + if (empty($files)) { + return $elements; + } + + // Collect cache tags to be added for each item in the field. + foreach ($files as $delta => $file) { + $video_uri = file_create_url($file->getFileUri()); + $elements[$delta] = ['#markup' => $video_uri]; + } + return $elements; + } +} \ No newline at end of file diff --git a/src/Plugin/Field/FieldType/VideoItem.php b/src/Plugin/Field/FieldType/VideoItem.php index 64eca13..0e3678c 100644 --- a/src/Plugin/Field/FieldType/VideoItem.php +++ b/src/Plugin/Field/FieldType/VideoItem.php @@ -30,7 +30,10 @@ * }, * }, * list_class = "\Drupal\file\Plugin\Field\FieldType\FileFieldItemList", - * constraints = {"ReferenceAccess" = {}, "FileValidation" = {}} + * constraints = {"ReferenceAccess" = {}, "FileValidation" = {}}, + * serialized_property_names = { + * "data" + * } * ) */ class VideoItem extends FileItem { @@ -38,9 +41,9 @@ class VideoItem extends FileItem { /** * The entity manager. * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface + * @var \Drupal\Core\Entity\EntityManagerInterface */ - protected $entityTypeManager; + protected $entityManager; /** * {@inheritdoc} @@ -175,13 +178,13 @@ public function isDisplayed() { /** * Gets the entity manager. * - * @return \Drupal\Core\Entity\EntityTypeManagerInterface. + * @return \Drupal\Core\Entity\EntityManagerInterface. */ - protected function getEntityTypeManager() { - if (!isset($this->entityTypeManager)) { - $this->entityTypeManager = \Drupal::entityTypeManager(); + protected function getEntityManager() { + if (!isset($this->entityManager)) { + $this->entityManager = \Drupal::entityManager(); } - return $this->entityTypeManager; + return $this->entityManager; } } diff --git a/src/Plugin/Field/FieldWidget/VideoEmbedWidget.php b/src/Plugin/Field/FieldWidget/VideoEmbedWidget.php index b438a3b..a0abe5c 100644 --- a/src/Plugin/Field/FieldWidget/VideoEmbedWidget.php +++ b/src/Plugin/Field/FieldWidget/VideoEmbedWidget.php @@ -390,7 +390,7 @@ public function extractFormValues(FieldItemListInterface $items, array $form, Fo $matches = $provider_matches['matches']; $uri = $definition['stream_wrapper'] . '://' . $matches['id']; - $storage = \Drupal::entityTypeManager()->getStorage('file'); + $storage = \Drupal::entityManager()->getStorage('file'); $results = $storage->getQuery() ->condition('uri', $uri) ->execute(); diff --git a/src/StreamWrapper/VideoRemoteStreamWrapper.php b/src/StreamWrapper/VideoRemoteStreamWrapper.php index aa88215..d6593ee 100644 --- a/src/StreamWrapper/VideoRemoteStreamWrapper.php +++ b/src/StreamWrapper/VideoRemoteStreamWrapper.php @@ -45,7 +45,7 @@ public function getDirectoryPath() { */ public function getExternalUrl() { $path = str_replace('\\', '/', $this->getTarget()); - return static::baseUrl() . '/' . UrlHelper::encodePath($path); + return static::baseUrl() . '/' . $path; } /** diff --git a/tests/src/Kernel/VideoItemSerializationTest.php b/tests/src/Kernel/VideoItemSerializationTest.php new file mode 100644 index 0000000..fc78a3e --- /dev/null +++ b/tests/src/Kernel/VideoItemSerializationTest.php @@ -0,0 +1,69 @@ +installEntitySchema('user'); + $this->serializer = \Drupal::service('serializer'); + + // Create a generic video field. + FieldStorageConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_test', + 'type' => 'video', + ])->save(); + + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_test', + 'bundle' => 'entity_test', + ])->save(); + } + + /** + * Tests the deserialization. + */ + public function testVideoDeserialization() { + $entity = EntityTest::create(); + $json = json_decode($this->serializer->serialize($entity, 'json'), TRUE); + $json['field_test'][0]['data'] = 'string data'; + $serialized = json_encode($json, TRUE); + $this->setExpectedException(\LogicException::class, 'The generic FieldItemNormalizer cannot denormalize string values for "data" properties of the "field_test" field (field item class: Drupal\video\Plugin\Field\FieldType\VideoItem).'); + $this->serializer->deserialize($serialized, EntityTest::class, 'json'); + } + +} diff --git a/video.info.yml b/video.info.yml index bc436b8..027b2af 100644 --- a/video.info.yml +++ b/video.info.yml @@ -2,6 +2,12 @@ name: Video type: module description: A field type for storing uploaded or embedded videos. package: Field types -core: 8.x +# core: 8.x dependencies: - - file \ No newline at end of file + - file +# Information added by Drupal.org packaging script on 2019-02-20 +version: '8.x-1.4' +core: '8.x' +project: 'video' +datestamp: 1550683092 +core_version_requirement: ^8 || ^9 \ No newline at end of file