diff --git a/src/Plugin/MediaEntity/Type/Twitter.php b/src/Plugin/MediaEntity/Type/Twitter.php index ce93d30..695903a 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\MediaTypeDefaultFieldTrait; 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 MediaTypeDefaultFieldTrait; /** * 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,45 @@ class Twitter extends MediaTypeBase { /** * {@inheritdoc} */ + public function allowedSourceFieldTypes() { + return ['string', 'string_long', 'link']; + } + + /** + * {@inheritdoc} + */ + public function defaultSourceFieldLabel() { + return $this->t('Tweet URL'); + } + + /** + * {@inheritdoc} + */ + public function defaultSourceFieldType() { + return 'string'; + } + + /** + * {@inheritdoc} + */ + public function defaultSourceFieldWidget() { + return 'string_textfield'; + } + + /** + * {@inheritdoc} + */ + public function defaultSourceFieldFormatter() { + return '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->defaultFieldConfigurationForm($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',