From 9bd1bd66b323d8b924682390afbaf70bb8b461f3 Mon Sep 17 00:00:00 2001 From: Brian Canini <canini.16@osu.edu> Date: Fri, 24 Apr 2020 13:16:10 -0400 Subject: [PATCH] Updating drupal/media_entity_twitter (2.3.0 => 2.4.0) --- composer.json | 2 +- composer.lock | 23 +++-- vendor/composer/installed.json | 24 ++--- .../media_entity_twitter/composer.json | 4 +- .../schema/media_entity_twitter.schema.yml | 8 ++ .../media_entity_twitter.info.yml | 10 +- .../media_entity_twitter.install | 3 +- .../src/Form/TwitterMediaLibraryAddForm.php | 96 +++++++++++++++++++ .../FieldFormatter/TwitterEmbedFormatter.php | 50 +++++++++- .../src/Plugin/media/Source/Twitter.php | 5 +- .../Functional/TweetEmbedFormatterTest.php | 7 +- 11 files changed, 200 insertions(+), 32 deletions(-) create mode 100644 web/modules/media_entity_twitter/src/Form/TwitterMediaLibraryAddForm.php diff --git a/composer.json b/composer.json index 765ba3a469..149934362d 100644 --- a/composer.json +++ b/composer.json @@ -136,7 +136,7 @@ "drupal/magnific_popup": "1.3", "drupal/mathjax": "2.7", "drupal/media_entity_browser": "2.0-alpha2", - "drupal/media_entity_twitter": "2.3", + "drupal/media_entity_twitter": "2.4", "drupal/menu_block": "1.4", "drupal/menu_block_title": "1.1", "drupal/menu_breadcrumb": "1.12", diff --git a/composer.lock b/composer.lock index 3cfa302ea5..173b503248 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2a360f2c481e8fc30575fcbbbda15969", + "content-hash": "4c1bdae7927770b326c330931383380c", "packages": [ { "name": "alchemy/zippy", @@ -5711,20 +5711,20 @@ }, { "name": "drupal/media_entity_twitter", - "version": "2.3.0", + "version": "2.4.0", "source": { "type": "git", "url": "https://git.drupalcode.org/project/media_entity_twitter.git", - "reference": "8.x-2.3" + "reference": "8.x-2.4" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/media_entity_twitter-8.x-2.3.zip", - "reference": "8.x-2.3", - "shasum": "de0f25deaa97e09c6851c09ee01c1e2f3f505f48" + "url": "https://ftp.drupal.org/files/projects/media_entity_twitter-8.x-2.4.zip", + "reference": "8.x-2.4", + "shasum": "11bca09cc6dd678dc7585d16d66167a09c6e42fb" }, "require": { - "drupal/core": "^8.4", + "drupal/core": "^8.8 || ^9", "j7mbo/twitter-api-php": "~1.0" }, "type": "drupal-module", @@ -5733,8 +5733,8 @@ "dev-2.x": "2.x-dev" }, "drupal": { - "version": "8.x-2.3", - "datestamp": "1579597383", + "version": "8.x-2.4", + "datestamp": "1585646747", "security-coverage": { "status": "covered", "message": "Covered by Drupal's security advisory policy" @@ -6068,7 +6068,8 @@ "homepage": "https://www.drupal.org/project/migrate_devel", "support": { "source": "http://cgit.drupalcode.org/migrate_devel" - } + }, + "time": "2017-06-25T23:46:13+00:00" }, { "name": "drupal/migrate_plus", @@ -9487,6 +9488,7 @@ "email": "jakub.onderka@gmail.com" } ], + "abandoned": "php-parallel-lint/php-console-color", "time": "2018-09-29T17:23:10+00:00" }, { @@ -9533,6 +9535,7 @@ } ], "description": "Highlight PHP code in terminal", + "abandoned": "php-parallel-lint/php-console-highlighter", "time": "2018-09-29T18:48:56+00:00" }, { diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 26b557ccb4..ac8938464d 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -5885,21 +5885,21 @@ }, { "name": "drupal/media_entity_twitter", - "version": "2.3.0", - "version_normalized": "2.3.0.0", + "version": "2.4.0", + "version_normalized": "2.4.0.0", "source": { "type": "git", "url": "https://git.drupalcode.org/project/media_entity_twitter.git", - "reference": "8.x-2.3" + "reference": "8.x-2.4" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/media_entity_twitter-8.x-2.3.zip", - "reference": "8.x-2.3", - "shasum": "de0f25deaa97e09c6851c09ee01c1e2f3f505f48" + "url": "https://ftp.drupal.org/files/projects/media_entity_twitter-8.x-2.4.zip", + "reference": "8.x-2.4", + "shasum": "11bca09cc6dd678dc7585d16d66167a09c6e42fb" }, "require": { - "drupal/core": "^8.4", + "drupal/core": "^8.8 || ^9", "j7mbo/twitter-api-php": "~1.0" }, "type": "drupal-module", @@ -5908,8 +5908,8 @@ "dev-2.x": "2.x-dev" }, "drupal": { - "version": "8.x-2.3", - "datestamp": "1579597383", + "version": "8.x-2.4", + "datestamp": "1585646747", "security-coverage": { "status": "covered", "message": "Covered by Drupal's security advisory policy" @@ -9788,7 +9788,8 @@ "name": "Jakub Onderka", "email": "jakub.onderka@gmail.com" } - ] + ], + "abandoned": "php-parallel-lint/php-console-color" }, { "name": "jakub-onderka/php-console-highlighter", @@ -9836,7 +9837,8 @@ "homepage": "http://www.acci.cz/" } ], - "description": "Highlight PHP code in terminal" + "description": "Highlight PHP code in terminal", + "abandoned": "php-parallel-lint/php-console-highlighter" }, { "name": "league/container", diff --git a/web/modules/media_entity_twitter/composer.json b/web/modules/media_entity_twitter/composer.json index 138cae8108..64a80b7b79 100644 --- a/web/modules/media_entity_twitter/composer.json +++ b/web/modules/media_entity_twitter/composer.json @@ -1,5 +1,7 @@ { - "name": "drupal-contrib:media_entity_twitter", + "name": "drupal/media_entity_twitter", + "type": "drupal-module", + "license": "GPL-2.0-or-later", "require": { "j7mbo/twitter-api-php": "~1.0" } diff --git a/web/modules/media_entity_twitter/config/schema/media_entity_twitter.schema.yml b/web/modules/media_entity_twitter/config/schema/media_entity_twitter.schema.yml index 643dfc6b44..0261d5c0df 100644 --- a/web/modules/media_entity_twitter/config/schema/media_entity_twitter.schema.yml +++ b/web/modules/media_entity_twitter/config/schema/media_entity_twitter.schema.yml @@ -28,3 +28,11 @@ media.source.twitter: generate_thumbnails: type: boolean label: 'Enable automatic thumbnail generation' + +field.formatter.settings.twitter_embed: + type: mapping + label: 'Twitter field display format settings' + mapping: + conversation: + type: boolean + label: 'Show previous tweet' diff --git a/web/modules/media_entity_twitter/media_entity_twitter.info.yml b/web/modules/media_entity_twitter/media_entity_twitter.info.yml index 718e5679d6..0c97051226 100644 --- a/web/modules/media_entity_twitter/media_entity_twitter.info.yml +++ b/web/modules/media_entity_twitter/media_entity_twitter.info.yml @@ -2,11 +2,11 @@ name: Media Entity Twitter description: 'Media Entity Twitter provider.' type: module package: Media -core: 8.x +core_version_requirement: ^8.8 || ^9 dependencies: - - drupal:media (>= 8.4) + - drupal:media -# Information added by Drupal.org packaging script on 2020-01-21 -version: '8.x-2.3' +# Information added by Drupal.org packaging script on 2020-03-31 +version: '8.x-2.4' project: 'media_entity_twitter' -datestamp: 1579597387 +datestamp: 1585646749 diff --git a/web/modules/media_entity_twitter/media_entity_twitter.install b/web/modules/media_entity_twitter/media_entity_twitter.install index daa65ab75b..d56b65fb6c 100644 --- a/web/modules/media_entity_twitter/media_entity_twitter.install +++ b/web/modules/media_entity_twitter/media_entity_twitter.install @@ -15,7 +15,8 @@ function media_entity_twitter_install() { $destination = \Drupal::config('media.settings')->get('icon_base_uri'); \Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); - $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/'); + $file_system = \Drupal::service('file_system'); + $files = $file_system->scanDirectory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/'); foreach ($files as $file) { // When reinstalling the media module we don't want to copy the icons when // they already exist. The icons could be replaced (by a contrib module or diff --git a/web/modules/media_entity_twitter/src/Form/TwitterMediaLibraryAddForm.php b/web/modules/media_entity_twitter/src/Form/TwitterMediaLibraryAddForm.php new file mode 100644 index 0000000000..93486dcc97 --- /dev/null +++ b/web/modules/media_entity_twitter/src/Form/TwitterMediaLibraryAddForm.php @@ -0,0 +1,96 @@ +<?php + +namespace Drupal\media_entity_twitter\Form; + +use Drupal\Core\Form\FormBuilderInterface; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; +use Drupal\media\MediaTypeInterface; +use Drupal\media_library\Form\AddFormBase; + +/** + * Creates a form to create Twitter media entities from within Media Library. + */ +class TwitterMediaLibraryAddForm extends AddFormBase { + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'media_entity_twitter_media_library_add'; + } + + /** + * {@inheritdoc} + */ + protected function buildInputElement(array $form, FormStateInterface $form_state) { + $media_type = $this->getMediaType($form_state); + + $form['container'] = [ + '#type' => 'container', + '#title' => $this->t('Add @type', [ + '@type' => $media_type->label(), + ]), + ]; + + $form['container']['url'] = [ + '#type' => 'url', + '#title' => $this->getSourceFieldDefinition($media_type)->getLabel(), + '#description' => $this->getSourceFieldDefinition($media_type)->getDescription(), + '#required' => TRUE, + '#attributes' => [ + 'placeholder' => 'https://', + ], + ]; + + $form['container']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Add'), + '#button_type' => 'primary', + '#submit' => ['::addButtonSubmit'], + '#ajax' => [ + 'callback' => '::updateFormCallback', + 'wrapper' => 'media-library-wrapper', + // Add a fixed URL to post the form since AJAX forms are automatically + // posted to <current> instead of $form['#action']. + // @todo Remove when https://www.drupal.org/project/drupal/issues/2504115 + // is fixed. + // Follow along with changes in \Drupal\media_library\Form\OEmbedForm. + 'url' => Url::fromRoute('media_library.ui'), + 'options' => [ + 'query' => $this->getMediaLibraryState($form_state)->all() + [ + FormBuilderInterface::AJAX_FORM_REQUEST => TRUE, + ], + ], + ], + ]; + + return $form; + } + + /** + * Submit handler for the add button. + * + * @param array $form + * The form render array. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + */ + public function addButtonSubmit(array $form, FormStateInterface $form_state) { + $this->processInputValues([$form_state->getValue('url')], $form, $form_state); + } + + /** + * Returns the definition of the source field for a media type. + * + * @param \Drupal\media\MediaTypeInterface $media_type + * The media type to get the source definition for. + * + * @return \Drupal\Core\Field\FieldDefinitionInterface|null + * The field definition. + */ + protected function getSourceFieldDefinition(MediaTypeInterface $media_type) { + return $media_type->getSource()->getSourceFieldDefinition($media_type); + } + +} diff --git a/web/modules/media_entity_twitter/src/Plugin/Field/FieldFormatter/TwitterEmbedFormatter.php b/web/modules/media_entity_twitter/src/Plugin/Field/FieldFormatter/TwitterEmbedFormatter.php index 694452fde8..8399a49da2 100644 --- a/web/modules/media_entity_twitter/src/Plugin/Field/FieldFormatter/TwitterEmbedFormatter.php +++ b/web/modules/media_entity_twitter/src/Plugin/Field/FieldFormatter/TwitterEmbedFormatter.php @@ -5,6 +5,7 @@ use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; +use Drupal\Core\Form\FormStateInterface; use Drupal\media_entity_twitter\Plugin\media\Source\Twitter; /** @@ -20,6 +21,47 @@ */ class TwitterEmbedFormatter extends FormatterBase { + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + $settings['conversation'] = FALSE; + + return $settings; + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $form = parent::settingsForm($form, $form_state); + + $form['conversation'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show previous tweet?'), + '#description' => $this->t('If the requested tweet was a reply to another one, this option will display a summary of the previous tweet too. By default only the requested tweet will be displayed.'), + '#default_value' => $this->getSetting('conversation'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = []; + + if ($this->getSetting('conversation')) { + $summary[] = $this->t('Show previous tweet, if applicable.'); + } + else { + $summary[] = $this->t('Do not show previous tweet, if applicable.'); + } + + return $summary; + } + /** * Extracts the embed code from a field item. * @@ -67,10 +109,16 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#path' => 'https://twitter.com/' . $matches['user'] . '/statuses/' . $matches['id'], '#attributes' => [ 'class' => ['twitter-tweet', 'element-hidden'], - 'data-conversation' => 'none', 'lang' => 'en', ], ]; + + // If the option was not selected to show the conversation, then pass + // the API option to disable the conversation. + // @see https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/overview + if (!$this->getSetting('conversation')) { + $element[$delta]['#attributes']['data-conversation'] = 'none'; + } } } diff --git a/web/modules/media_entity_twitter/src/Plugin/media/Source/Twitter.php b/web/modules/media_entity_twitter/src/Plugin/media/Source/Twitter.php index a1d994039d..72018dfee2 100644 --- a/web/modules/media_entity_twitter/src/Plugin/media/Source/Twitter.php +++ b/web/modules/media_entity_twitter/src/Plugin/media/Source/Twitter.php @@ -27,7 +27,10 @@ * label = @Translation("Twitter"), * allowed_field_types = {"string", "string_long", "link"}, * default_thumbnail_filename = "twitter.png", - * description = @Translation("Provides business logic and metadata for Twitter.") + * description = @Translation("Provides business logic and metadata for Twitter."), + * forms = { + * "media_library_add" = "\Drupal\media_entity_twitter\Form\TwitterMediaLibraryAddForm", + * } * ) */ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInterface { diff --git a/web/modules/media_entity_twitter/tests/src/Functional/TweetEmbedFormatterTest.php b/web/modules/media_entity_twitter/tests/src/Functional/TweetEmbedFormatterTest.php index 8470b3a36b..2df19dca8c 100644 --- a/web/modules/media_entity_twitter/tests/src/Functional/TweetEmbedFormatterTest.php +++ b/web/modules/media_entity_twitter/tests/src/Functional/TweetEmbedFormatterTest.php @@ -21,6 +21,11 @@ class TweetEmbedFormatterTest extends MediaFunctionalTestBase { 'link', ]; + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + /** * Tests adding and editing a twitter embed formatter. */ @@ -44,7 +49,7 @@ public function testManageEmbedFormatter() { // @todo Replace entity_get_form_display() when #2367933 is done. // https://www.drupal.org/node/2872159. - entity_get_form_display('media', $bundle->id(), 'default') + \Drupal::service('entity_display.repository')->getFormDisplay('media', $bundle->id(), 'default') ->setComponent($source_field->getName(), $component) ->save(); -- GitLab