diff --git a/src/Plugin/MediaEntity/Type/Twitter.php b/src/Plugin/MediaEntity/Type/Twitter.php index 18fa450..ac477f4 100644 --- a/src/Plugin/MediaEntity/Type/Twitter.php +++ b/src/Plugin/MediaEntity/Type/Twitter.php @@ -2,14 +2,15 @@ namespace Drupal\media_entity_twitter\Plugin\MediaEntity\Type; +use Drupal\Component\Serialization\Json; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\media_entity\MediaInterface; use Drupal\media_entity\MediaTypeBase; use Drupal\media_entity\MediaTypeException; -use Drupal\Component\Serialization\Json; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -24,6 +25,21 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class Twitter extends MediaTypeBase { /** + * The name of the default source field on the media entity. + */ + const MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME = 'field_media_twitter'; + + /** + * The id of the widget to be used when creating the default source field. + */ + const MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_WIDGET = 'string_textfield'; + + /** + * The id of the formatter to be used when creating the default source field. + */ + const MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_FORMATTER = 'twitter_embed'; + + /** * Config factory service. * * @var \Drupal\Core\Config\ConfigFactoryInterface @@ -199,6 +215,7 @@ class Twitter extends MediaTypeBase { '#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, + '#weight' => -10, ); $form['use_twitter_api'] = array( @@ -399,4 +416,24 @@ class Twitter extends MediaTypeBase { return parent::getDefaultName($media); } + /** + * {@inheritdoc} + */ + public function getDefaultSourceField() { + if (!$storage = FieldStorageConfig ::loadByName('media', static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME)) { + $storage = FieldStorageConfig::create([ + 'field_name' => static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME, + 'entity_type' => 'media', + 'type' => 'string', + ]); + } + return [ + 'storage' => $storage, + 'field_name' => static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME, + 'label' => $this->t('Tweet URL'), + 'field_widget' => static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_WIDGET, + 'field_formatter' => static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_FORMATTER, + ]; + } + }