You are here

class VideoEmbedField in Video Embed Field 8

Same name in this branch
  1. 8 src/Plugin/Field/FieldType/VideoEmbedField.php \Drupal\video_embed_field\Plugin\Field\FieldType\VideoEmbedField
  2. 8 src/Plugin/migrate/cckfield/VideoEmbedField.php \Drupal\video_embed_field\Plugin\migrate\cckfield\VideoEmbedField
  3. 8 modules/video_embed_media/src/Plugin/MediaEntity/Type/VideoEmbedField.php \Drupal\video_embed_media\Plugin\MediaEntity\Type\VideoEmbedField

Provides media type plugin for video embed field.

Plugin annotation


@MediaType(
  id = "video_embed_field",
  label = @Translation("Video embed field"),
  description = @Translation("Enables video_embed_field integration with media_entity.")
)

Hierarchy

Expanded class hierarchy of VideoEmbedField

4 files declare their use of VideoEmbedField
DefaultNameTest.php in modules/video_embed_media/tests/src/Kernel/DefaultNameTest.php
ProvidedFieldsTest.php in modules/video_embed_media/tests/src/Kernel/ProvidedFieldsTest.php
UpgradeManager.php in modules/video_embed_media/src/UpgradeManager.php
video_embed_media.module in modules/video_embed_media/video_embed_media.module
The module file for video_embed_media.

File

modules/video_embed_media/src/Plugin/MediaEntity/Type/VideoEmbedField.php, line 25

Namespace

Drupal\video_embed_media\Plugin\MediaEntity\Type
View source
class VideoEmbedField extends MediaTypeBase {

  /**
   * The name of the field on the media entity.
   */
  const VIDEO_EMBED_FIELD_DEFAULT_NAME = 'field_media_video_embed_field';

  /**
   * The video provider manager.
   *
   * @var \Drupal\video_embed_field\ProviderManagerInterface
   */
  protected $providerManager;

  /**
   * The media settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $mediaSettings;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, Config $config, ProviderManagerInterface $provider_manager, Config $media_settings) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $config);
    $this->providerManager = $provider_manager;
    $this->mediaSettings = $media_settings;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('config.factory')
      ->get('media_entity.settings'), $container
      ->get('video_embed_field.provider_manager'), $container
      ->get('config.factory')
      ->get('media_entity.settings'));
  }

  /**
   * {@inheritdoc}
   */
  public function thumbnail(MediaInterface $media) {
    if ($provider = $this
      ->loadProvider($media)) {
      $provider
        ->downloadThumbnail();
      return $provider
        ->getLocalThumbnailUri();
    }
    return $this
      ->getDefaultThumbnail();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $options = [];
    foreach ($this->entityFieldManager
      ->getFieldDefinitions('media', $form_state
      ->getFormObject()
      ->getEntity()
      ->id()) as $field_name => $field) {
      if ($field
        ->getType() == 'video_embed_field') {
        $options[$field_name] = $field
          ->getLabel();
      }
    }
    if (empty($options)) {
      $form['summary']['#markup'] = $this
        ->t('A video embed field will be created on this media bundle when you save this form. You can return to this configuration screen to alter the video field used for this bundle, or you can use the one provided.');
    }
    if (!empty($options)) {
      $form['source_field'] = [
        '#type' => 'select',
        '#required' => TRUE,
        '#title' => $this
          ->t('Source Video Field'),
        '#description' => $this
          ->t('The field on the media entity that contains the video URL.'),
        '#default_value' => empty($this->configuration['source_field']) ? VideoEmbedField::VIDEO_EMBED_FIELD_DEFAULT_NAME : $this->configuration['source_field'],
        '#options' => $options,
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getField(MediaInterface $media, $name) {
    if (!($url = $this
      ->getVideoUrl($media))) {
      return FALSE;
    }
    $provider = $this->providerManager
      ->loadProviderFromInput($url);
    $definition = $this->providerManager
      ->loadDefinitionFromInput($url);
    switch ($name) {
      case 'id':
        return $provider
          ->getIdFromInput($url);
      case 'source':
        return $definition['id'];
      case 'source_name':
        return $definition['id'];
      case 'image_local':
      case 'image_local_uri':
        return $this
          ->thumbnail($media);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function providedFields() {
    return [
      'id' => $this
        ->t('Video ID.'),
      'source' => $this
        ->t('Video source machine name.'),
      'source_name' => $this
        ->t('Video source human name.'),
      'image_local' => $this
        ->t('Copies thumbnail image to the local filesystem and returns the URI.'),
      'image_local_uri' => $this
        ->t('Gets URI of the locally saved image.'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultName(MediaInterface $media) {
    if ($provider = $this
      ->loadProvider($media)) {
      return $this
        ->loadProvider($media)
        ->getName();
    }
    return parent::getDefaultThumbnail();
  }

  /**
   * Load a video provider given a media entity.
   *
   * @param \Drupal\media_entity\MediaInterface $media
   *   The media entity.
   *
   * @return \Drupal\video_embed_field\ProviderPluginInterface
   *   The provider plugin.
   */
  protected function loadProvider(MediaInterface $media) {
    $video_url = $this
      ->getVideoUrl($media);
    return !empty($video_url) ? $this->providerManager
      ->loadProviderFromInput($video_url) : FALSE;
  }

  /**
   * Get the video URL from a media entity.
   *
   * @param \Drupal\media_entity\MediaInterface $media
   *   The media entity.
   *
   * @return string|bool
   *   A video URL or FALSE on failure.
   */
  protected function getVideoUrl(MediaInterface $media) {
    $field_name = empty($this->configuration['source_field']) ? VideoEmbedField::VIDEO_EMBED_FIELD_DEFAULT_NAME : $this->configuration['source_field'];
    $video_url = $media->{$field_name}->value;
    return !empty($video_url) ? $video_url : FALSE;
  }

  /**
   * The function that is invoked during the insert of media bundles.
   *
   * @param string $media_bundle_id
   *   The ID of the media bundle.
   */
  public static function createVideoEmbedField($media_bundle_id) {
    if (!($storage = FieldStorageConfig::loadByName('media', static::VIDEO_EMBED_FIELD_DEFAULT_NAME))) {
      FieldStorageConfig::create([
        'field_name' => static::VIDEO_EMBED_FIELD_DEFAULT_NAME,
        'entity_type' => 'media',
        'type' => 'video_embed_field',
      ])
        ->save();
    }
    FieldConfig::create([
      'entity_type' => 'media',
      'field_name' => static::VIDEO_EMBED_FIELD_DEFAULT_NAME,
      'label' => 'Video URL',
      'required' => TRUE,
      'bundle' => $media_bundle_id,
    ])
      ->save();

    // Make the field visible on the form display.
    $form_display = \Drupal::service('entity_display.repository')
      ->getFormDisplay('media', $media_bundle_id, 'default');
    $form_display
      ->setComponent(static::VIDEO_EMBED_FIELD_DEFAULT_NAME, [
      'type' => 'video_embed_field_textfield',
    ])
      ->save();

    // Make the field visible on the media entity itself.
    $display = \Drupal::service('entity_display.repository')
      ->getViewDisplay('media', $media_bundle_id, 'default');
    $display
      ->setComponent(static::VIDEO_EMBED_FIELD_DEFAULT_NAME, [
      'type' => 'video_embed_field_video',
    ])
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultThumbnail() {
    return $this->mediaSettings
      ->get('icon_base') . '/video.png';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaTypeBase::$config protected property Media entity image config object.
MediaTypeBase::$entityFieldManager protected property The entity field manager service.
MediaTypeBase::$entityTypeManager protected property The entity type manager service.
MediaTypeBase::$label protected property Plugin label.
MediaTypeBase::attachConstraints public function Attaches type-specific constraints to media. Overrides MediaTypeInterface::attachConstraints
MediaTypeBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
MediaTypeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration 1
MediaTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
MediaTypeBase::label public function Returns the display label. Overrides MediaTypeInterface::label
MediaTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
MediaTypeBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
MediaTypeBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
VideoEmbedField::$mediaSettings protected property The media settings.
VideoEmbedField::$providerManager protected property The video provider manager.
VideoEmbedField::buildConfigurationForm public function Form constructor. Overrides MediaTypeBase::buildConfigurationForm
VideoEmbedField::create public static function Creates an instance of the plugin. Overrides MediaTypeBase::create
VideoEmbedField::createVideoEmbedField public static function The function that is invoked during the insert of media bundles.
VideoEmbedField::getDefaultName public function Provide a default name for the media. Overrides MediaTypeBase::getDefaultName
VideoEmbedField::getDefaultThumbnail public function Gets the default thumbnail image. Overrides MediaTypeBase::getDefaultThumbnail
VideoEmbedField::getField public function Gets a media-related field/value. Overrides MediaTypeInterface::getField
VideoEmbedField::getVideoUrl protected function Get the video URL from a media entity.
VideoEmbedField::loadProvider protected function Load a video provider given a media entity.
VideoEmbedField::providedFields public function Gets list of fields provided by this plugin. Overrides MediaTypeInterface::providedFields
VideoEmbedField::thumbnail public function Gets thumbnail image. Overrides MediaTypeInterface::thumbnail
VideoEmbedField::VIDEO_EMBED_FIELD_DEFAULT_NAME constant The name of the field on the media entity.
VideoEmbedField::__construct public function Constructs a new class instance. Overrides MediaTypeBase::__construct