diff --git a/src/Plugin/MediaEntity/Type/Twitter.php b/src/Plugin/MediaEntity/Type/Twitter.php index ce93d30..28f699a 100644 --- a/src/Plugin/MediaEntity/Type/Twitter.php +++ b/src/Plugin/MediaEntity/Type/Twitter.php @@ -10,6 +10,7 @@ use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Render\RendererInterface; use Drupal\media_entity\MediaInterface; use Drupal\media_entity\MediaTypeBase; +use Drupal\media_entity\MediaTypeSourceFieldTrait; use Drupal\media_entity\MediaTypeException; use Drupal\media_entity_twitter\TweetFetcherInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -24,6 +25,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * ) */ class Twitter extends MediaTypeBase { + use MediaTypeSourceFieldTrait; /** * Config factory service. @@ -135,7 +137,7 @@ class Twitter extends MediaTypeBase { 'image_local_uri' => $this->t('Gets URI of the locally saved image.'), 'content' => $this->t('This tweet content'), 'retweet_count' => $this->t('Retweet count for this tweet'), - 'profile_image_url_https' => $this->t('Link to profile image') + 'profile_image_url_https' => $this->t('Link to profile image'), ); } @@ -224,24 +226,23 @@ class Twitter extends MediaTypeBase { /** * {@inheritdoc} */ + public function sourceFieldInfo() { + return [ + 'allowed_types' => ['string', 'string_long', 'link'], + 'default_label' => $this->t('Tweet URL'), + 'default_type' => 'string', + 'default_widget' => 'string_textfield', + 'default_formatter' => 'twitter_embed', + ]; + } + + /** + * {@inheritdoc} + */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $options = []; - $allowed_field_types = ['string', 'string_long', 'link']; - /** @var \Drupal\media_entity\MediaBundleInterface $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 = $this->sourceFieldConfigurationForm($form, $form_state); - $form['source_field'] = array( - '#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['source_field']['#description'] = $this->t('Field on media entity that stores Twitter embed code or URL.'); $form['use_twitter_api'] = array( '#type' => 'select', diff --git a/src/Tests/TweetEmbedFormatterTest.php b/tests/src/Functional/TweetEmbedFormatterTest.php similarity index 97% rename from src/Tests/TweetEmbedFormatterTest.php rename to tests/src/Functional/TweetEmbedFormatterTest.php index 1625f49..4ce0644 100644 --- a/src/Tests/TweetEmbedFormatterTest.php +++ b/tests/src/Functional/TweetEmbedFormatterTest.php @@ -1,18 +1,15 @@