diff --git a/config/schema/media_entity_twitter.schema.yml b/config/schema/media_entity_twitter.schema.yml index 04c37e6..bfd40d8 100644 --- a/config/schema/media_entity_twitter.schema.yml +++ b/config/schema/media_entity_twitter.schema.yml @@ -13,6 +13,9 @@ media_entity.bundle.type.twitter: source_field: type: string label: 'Field with embed code/URL' + create_source_field: + type: boolean + label: 'Whether to automatically create a source field on bundle creation or not' use_twitter_api: type: boolean label: 'Whether to use twitter api or not' diff --git a/src/Plugin/MediaEntity/Type/Twitter.php b/src/Plugin/MediaEntity/Type/Twitter.php index ce93d30..5b3a8dd 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. @@ -115,6 +117,7 @@ class Twitter extends MediaTypeBase { public function defaultConfiguration() { return [ 'use_twitter_api' => FALSE, + 'create_source_field' => TRUE, 'generate_thumbnails' => FALSE, ]; } @@ -135,7 +138,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'), ); } @@ -221,27 +224,40 @@ class Twitter extends MediaTypeBase { return FALSE; } + public function allowedSourceFieldTypes() { + return ['string', 'string_long', 'link']; + } + + public function defaultSourceFieldLabel() { + return $this->t('Tweet URL'); + } + + public function defaultSourceFieldName() { + return 'field_media_twitter'; + } + + public function defaultSourceFieldType() { + return 'string'; + } + + public function defaultSourceFieldWidget() { + return 'string_textfield'; + } + + /** + * The id of the formatter to be used when creating the default source field. + */ + 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', @@ -324,15 +340,13 @@ class Twitter extends MediaTypeBase { public function attachConstraints(MediaInterface $media) { parent::attachConstraints($media); - if (isset($this->configuration['source_field'])) { - $source_field_name = $this->configuration['source_field']; - if ($media->hasField($source_field_name)) { - foreach ($media->get($source_field_name) as &$embed_code) { - /** @var \Drupal\Core\TypedData\DataDefinitionInterface $typed_data */ - $typed_data = $embed_code->getDataDefinition(); - $typed_data->addConstraint('TweetEmbedCode'); - $typed_data->addConstraint('TweetVisible'); - } + $source_field_name = empty($this->configuration['source_field']) ? static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME : $this->configuration['source_field']; + if ($media->hasField($source_field_name)) { + foreach ($media->get($source_field_name) as &$embed_code) { + /** @var \Drupal\Core\TypedData\DataDefinitionInterface $typed_data */ + $typed_data = $embed_code->getDataDefinition(); + $typed_data->addConstraint('TweetEmbedCode'); + $typed_data->addConstraint('TweetVisible'); } } } @@ -433,14 +447,12 @@ class Twitter extends MediaTypeBase { protected function matchRegexp(MediaInterface $media) { $matches = array(); - if (isset($this->configuration['source_field'])) { - $source_field = $this->configuration['source_field']; - if ($media->hasField($source_field)) { - $property_name = $media->{$source_field}->first()->mainPropertyName(); - foreach (static::$validationRegexp as $pattern => $key) { - if (preg_match($pattern, $media->{$source_field}->{$property_name}, $matches)) { - return $matches; - } + $source_field_name = empty($this->configuration['source_field']) ? static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME : $this->configuration['source_field']; + if ($media->hasField($source_field_name)) { + $property_name = $media->{$source_field_name}->first()->mainPropertyName(); + foreach (static::$validationRegexp as $pattern => $key) { + if (preg_match($pattern, $media->{$source_field_name}->{$property_name}, $matches)) { + return $matches; } } }