diff --git a/src/Plugin/MediaEntity/Type/Twitter.php b/src/Plugin/MediaEntity/Type/Twitter.php index 9d7d2dc..5b3a8dd 100644 --- a/src/Plugin/MediaEntity/Type/Twitter.php +++ b/src/Plugin/MediaEntity/Type/Twitter.php @@ -3,17 +3,14 @@ namespace Drupal\media_entity_twitter\Plugin\MediaEntity\Type; use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\Entity\EntityFormDisplay; -use Drupal\Core\Entity\Entity\EntityViewDisplay; 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\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; 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; @@ -28,21 +25,7 @@ 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'; + use MediaTypeDefaultFieldTrait; /** * Config factory service. @@ -241,39 +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 = ['' => $this->t('- None -')]; - $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(); - } - } - - // Select the source field. Only show when the bundle is not new, so there - // will potentially be fields to select. - $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']) ? static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME : $this->configuration['source_field'], - '#options' => $options, - '#access' => !$bundle->isNew(), - ); + $form = $this->defaultFieldConfigurationForm($form, $form_state); - // Add a checkbox to allow the field being created automatically on save. - $form['create_source_field'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Create a source field automatically when saving this form.'), - '#description' => $this->t('If checked, a default field will be created and used as a source field. If you uncheck the field, you will need to create a field and revisit this form later to select it.'), - '#default_value' => TRUE, - '#access' => $bundle->isNew(), - ]; + $form['source_field']['#description'] = $this->t('Field on media entity that stores Twitter embed code or URL.'); $form['use_twitter_api'] = array( '#type' => 'select', @@ -513,62 +497,4 @@ class Twitter extends MediaTypeBase { return parent::getDefaultName($media); } - /** - * {@inheritdoc} - */ - public function reactOnBundleCreated($bundle_name, $entity_type_id) { - if (!empty($this->configuration['create_source_field'])) { - $this->createDefaultSourceField($bundle_name); - } - } - - /** - * {@inheritdoc} - */ - public function createDefaultSourceField($bundle_name) { - - // Create / load the field storage. - 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', - ]); - $storage->save(); - } - - // Create the field instance. - FieldConfig::create([ - 'entity_type' => 'media', - 'field_name' => static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME, - 'label' => $this->t('Tweet URL'), - 'required' => TRUE, - 'bundle' => $bundle_name, - ])->save(); - - // Make the field visible on the form display. - /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ - $form_display = EntityFormDisplay::create([ - 'targetEntityType' => 'media', - 'bundle' => $bundle_name, - 'mode' => 'default', - 'status' => TRUE, - ]); - $form_display->setComponent(static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME, [ - 'type' => static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_WIDGET, - ])->save(); - - // Make the field visible on the media entity itself. - /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ - $display = EntityViewDisplay::create([ - 'targetEntityType' => 'media', - 'bundle' => $bundle_name, - 'mode' => 'default', - 'status' => TRUE, - ]); - $display->setComponent(static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_NAME, [ - 'type' => static::MEDIA_ENTITY_TWITTER_DEFAULT_FIELD_FORMATTER, - ])->save(); - } - }