diff --git a/config/schema/media_entity_twitter.schema.yml b/config/schema/media_entity_twitter.schema.yml index a5b569a..643dfc6 100644 --- a/config/schema/media_entity_twitter.schema.yml +++ b/config/schema/media_entity_twitter.schema.yml @@ -7,15 +7,9 @@ media_entity_twitter.settings: label: 'Base folder for thumbnails' media.source.twitter: - type: media.source.twitter.field_aware + type: media.source.field_aware label: '"Twitter" media source configuration' - -media.source.twitter.field_aware: - type: mapping mapping: - source_field: - type: string - label: 'Field with embed code/URL' use_twitter_api: type: boolean label: 'Whether to use twitter api or not' diff --git a/src/Plugin/media/Source/Twitter.php b/src/Plugin/media/Source/Twitter.php index d9745f8..c27f574 100644 --- a/src/Plugin/media/Source/Twitter.php +++ b/src/Plugin/media/Source/Twitter.php @@ -6,12 +6,11 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Render\RendererInterface; use Drupal\media\MediaInterface; -use Drupal\media\MediaTypeInterface; use Drupal\media\MediaSourceBase; use Drupal\media\MediaTypeException; +use Drupal\media\MediaTypeInterface; use Drupal\media_entity_twitter\TweetFetcherInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; @@ -52,13 +51,6 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte protected $tweetFetcher; /** - * The logger channel. - * - * @var \Drupal\Core\Logger\LoggerChannelInterface - */ - protected $logger; - - /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { @@ -71,8 +63,7 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte $container->get('plugin.manager.field.field_type'), $container->get('config.factory'), $container->get('renderer'), - $container->get('media_entity_twitter.tweet_fetcher'), - $container->get('logger.factory')->get('media_entity_twitter') + $container->get('media_entity_twitter.tweet_fetcher') ); } @@ -106,15 +97,12 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte * The renderer. * @param \Drupal\media_entity_twitter\TweetFetcherInterface $tweet_fetcher * The tweet fetcher. - * @param \Drupal\Core\Logger\LoggerChannelInterface $logger - * The logger channel. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, RendererInterface $renderer, TweetFetcherInterface $tweet_fetcher, LoggerChannelInterface $logger) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, RendererInterface $renderer, TweetFetcherInterface $tweet_fetcher) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory); $this->configFactory = $config_factory; $this->renderer = $renderer; $this->tweetFetcher = $tweet_fetcher; - $this->logger = $logger; } /** @@ -122,6 +110,7 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte */ public function defaultConfiguration() { return [ + 'source_field' => '', 'use_twitter_api' => FALSE, 'generate_thumbnails' => FALSE, 'consumer_key' => '', @@ -269,24 +258,6 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - $options = []; - $allowed_field_types = ['string', 'string_long', 'link']; - /** @var \Drupal\media\Entity\MediaType $bundle */ - $bundle = $form_state->getFormObject()->getEntity(); - foreach ($this->entityFieldManager->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) { - if (in_array($field->getType(), $allowed_field_types) && !$field->getFieldStorageDefinition()->isBaseField()) { - $options[$field_name] = $field->getLabel(); - } - } - - $form['source_field'] = [ - '#type' => 'select', - '#title' => $this->t('Field with source information'), - '#description' => $this->t('Field on media entity that stores Twitter embed code or URL. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'), - '#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'], - '#options' => $options, - ]; - $form['use_twitter_api'] = [ '#type' => 'select', '#title' => $this->t('Whether to use Twitter api to fetch tweets or not.'), @@ -298,7 +269,7 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte ], ]; - // @todo Evauate if this should be a site-wide configuration. + // @todo: Evaluate if this should be a site-wide configuration. $form['consumer_key'] = [ '#type' => 'textfield', '#title' => $this->t('Consumer key'), @@ -376,14 +347,7 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte * {@inheritdoc} */ public function createSourceField(MediaTypeInterface $type) { - /** @var \Drupal\field\FieldConfigInterface $field */ - $field = parent::createSourceField($type); - - // Reset the field to its default settings so that we don't inherit the - // settings from the parent class' source field. - $settings = $this->fieldTypeManager->getDefaultFieldSettings($field->getType()); - - return $field->set('settings', $settings); + return parent::createSourceField($type)->set('label', 'Tweet Url'); } /** @@ -392,8 +356,7 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte * @param mixed $id * The tweet ID. * @param string|null $media_url - * The URL of the media (i.e., photo, video, etc.) associated with the - * tweet. + * The URL of the media (i.e., photo, video, etc.) associated with the tweet. * * @return string * The desired local URI. @@ -429,7 +392,7 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte /** * Runs preg_match on embed code/URL. * - * @param Drupal\media\MediaInterface $media + * @param \Drupal\media\MediaInterface $media * Media object. * * @return array|bool @@ -443,9 +406,9 @@ class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInte if (isset($this->configuration['source_field'])) { $source_field = $this->configuration['source_field']; if ($media->hasField($source_field)) { - $property_name = $media->{$source_field}->first()->mainPropertyName(); + $property_name = $media->get($source_field)->first()->mainPropertyName(); foreach (static::$validationRegexp as $pattern => $key) { - if (preg_match($pattern, $media->{$source_field}->{$property_name}, $matches)) { + if (preg_match($pattern, $media->get($source_field)->{$property_name}, $matches)) { return $matches; } } diff --git a/src/Tests/TweetEmbedFormatterTest.php b/src/Tests/TweetEmbedFormatterTest.php deleted file mode 100644 index 2b619c0..0000000 --- a/src/Tests/TweetEmbedFormatterTest.php +++ /dev/null @@ -1,260 +0,0 @@ -mediaId; - $this->testBundle = $this->createMediaType($bundle, 'twitter'); - $this->drupalPlaceBlock('local_actions_block'); - $this->adminUser = $this->drupalCreateUser([ - 'administer media', - 'administer media types', - 'administer media fields', - 'administer media form display', - 'administer media display', - // Media permissions. - 'view media', - 'create media', - 'update media', - 'update any media', - 'delete media', - 'delete any media', - // Other permissions. - 'administer views', - ]); - $this->drupalLogin($this->adminUser); - } - - /** - * Tests adding and editing a twitter embed formatter. - */ - public function testManageEmbedFormatter() { - // Test and create one media type. - $bundle = $this->testBundle; - - // Assert that the media type has the expected values before proceeding. - $this->drupalGet('admin/structure/media/manage/' . $bundle->id()); - $this->assertFieldByName('label', $bundle->label()); - $this->assertFieldByName('source', 'twitter'); - - // Add and save link field type settings (Embed code). - $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field'); - $edit_conf = [ - 'new_storage_type' => 'link', - 'label' => 'Link URL', - 'field_name' => 'link_url', - ]; - $this->drupalPostForm(NULL, $edit_conf, t('Save and continue')); - $this->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.'); - $edit = [ - 'cardinality' => 'number', - 'cardinality_number' => '1', - ]; - $this->drupalPostForm(NULL, $edit, t('Save field settings')); - $this->assertText('Updated field ' . $edit_conf['label'] . ' field settings.'); - - // Set the new link field type as required. - $edit = [ - 'required' => TRUE, - 'settings[link_type]' => '16', - 'settings[title]' => '0', - ]; - $this->drupalPostForm(NULL, $edit, t('Save settings')); - $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.'); - - // Add and save string_long field type settings (Embed code). - $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field'); - $edit_conf = [ - 'new_storage_type' => 'string_long', - 'label' => 'Embed code', - 'field_name' => 'embed_code', - ]; - $this->drupalPostForm(NULL, $edit_conf, t('Save and continue')); - $this->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.'); - $edit = [ - 'cardinality' => 'number', - 'cardinality_number' => '1', - ]; - $this->drupalPostForm(NULL, $edit, t('Save field settings')); - $this->assertText('Updated field ' . $edit_conf['label'] . ' field settings.'); - - // Set the new string_long field type as required. - $edit = [ - 'required' => TRUE, - ]; - $this->drupalPostForm(NULL, $edit, t('Save settings')); - $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.'); - - // Assert that the new field types configurations have been successfully - // saved. - $xpath = $this->xpath('//*[@id="field-link-url"]'); - $this->assertEqual((string) $xpath[0]->td[0], 'Link URL'); - $this->assertEqual((string) $xpath[0]->td[1], 'field_link_url'); - $this->assertEqual((string) $xpath[0]->td[2]->a, 'Link'); - - $xpath = $this->xpath('//*[@id="field-embed-code"]'); - $this->assertEqual((string) $xpath[0]->td[0], 'Embed code'); - $this->assertEqual((string) $xpath[0]->td[1], 'field_embed_code'); - $this->assertEqual((string) $xpath[0]->td[2]->a, 'Text (plain, long)'); - - // Test if edit worked and if new fields values have been saved as - // expected. - $this->drupalGet('admin/structure/media/manage/' . $bundle->id()); - $this->assertFieldByName('label', $bundle->label()); - $this->assertFieldByName('source', 'twitter'); - $edit = [ - 'source_configuration[source_field]' => 'field_embed_code', - ]; - $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('The media type ' . $bundle->label() . ' has been updated.'); - $this->assertText($bundle->label()); - - $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/display'); - - // Set and save the settings of the new field types. - $edit = [ - 'fields[field_link_url][label]' => 'above', - 'fields[field_link_url][type]' => 'twitter_embed', - 'fields[field_embed_code][label]' => 'above', - 'fields[field_embed_code][type]' => 'twitter_embed', - ]; - $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('Your settings have been saved.'); - - // Create and save the media with a twitter media code. - $this->drupalGet('media/add/' . $bundle->id()); - - // Random image url from twitter. - $tweet_url = 'https://twitter.com/RamzyStinson/status/670650348319576064'; - - // Random image from twitter. - $tweet = '

' . - 'Midnight project. I ain't got no oven. So I improvise making this milo crunchy kek batik. hahahaha ' . - '#itssomething ' . - 'pic.twitter.com/Nvn4Q1v2ae

— Zi (@RamzyStinson) ' . - '' . - '28 Novembre 2015
'; - - $edit = [ - 'name[0][value]' => 'Title', - 'field_link_url[0][uri]' => $tweet_url, - 'field_embed_code[0][value]' => $tweet, - ]; - $this->drupalPostForm(NULL, $edit, t('Save')); - - // Assert that the media has been successfully saved. - $this->assertText('Title'); - - // Assert that the link url formatter exists on this page. - $this->assertText('Link URL'); - $this->assertRaw('', 'Link in embedded Tweet found.'); - - // Assert that the string_long code formatter exists on this page. - $this->assertText('Embed code'); - $this->assertRaw(''; + + $edit = [ + 'name[0][value]' => 'Title', + 'field_media_twitter[0][value]' => $tweet_url, + 'field_embed_code[0][value]' => $tweet, + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + // Assert that the media has been successfully saved. + $this->assertText('Title'); + + // Assert that the link url formatter exists on this page. + $this->assertText('Tweet Url'); + $this->assertRaw('', 'Link in embedded Tweet found.'); + + // Assert that the string_long code formatter exists on this page. + $this->assertText('Embed code'); + $this->assertRaw('