class VideoEmbedField in Video Embed Field 8
Same name in this branch
- 8 src/Plugin/Field/FieldType/VideoEmbedField.php \Drupal\video_embed_field\Plugin\Field\FieldType\VideoEmbedField
- 8 src/Plugin/migrate/cckfield/VideoEmbedField.php \Drupal\video_embed_field\Plugin\migrate\cckfield\VideoEmbedField
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\media_entity\MediaTypeBase implements ContainerFactoryPluginInterface, MediaTypeInterface uses StringTranslationTrait
- class \Drupal\video_embed_media\Plugin\MediaEntity\Type\VideoEmbedField
- class \Drupal\media_entity\MediaTypeBase implements ContainerFactoryPluginInterface, MediaTypeInterface uses StringTranslationTrait
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\TypeView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaTypeBase:: |
protected | property | Media entity image config object. | |
MediaTypeBase:: |
protected | property | The entity field manager service. | |
MediaTypeBase:: |
protected | property | The entity type manager service. | |
MediaTypeBase:: |
protected | property | Plugin label. | |
MediaTypeBase:: |
public | function |
Attaches type-specific constraints to media. Overrides MediaTypeInterface:: |
|
MediaTypeBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
MediaTypeBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurablePluginInterface:: |
1 |
MediaTypeBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurablePluginInterface:: |
|
MediaTypeBase:: |
public | function |
Returns the display label. Overrides MediaTypeInterface:: |
|
MediaTypeBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface:: |
|
MediaTypeBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
MediaTypeBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
VideoEmbedField:: |
protected | property | The media settings. | |
VideoEmbedField:: |
protected | property | The video provider manager. | |
VideoEmbedField:: |
public | function |
Form constructor. Overrides MediaTypeBase:: |
|
VideoEmbedField:: |
public static | function |
Creates an instance of the plugin. Overrides MediaTypeBase:: |
|
VideoEmbedField:: |
public static | function | The function that is invoked during the insert of media bundles. | |
VideoEmbedField:: |
public | function |
Provide a default name for the media. Overrides MediaTypeBase:: |
|
VideoEmbedField:: |
public | function |
Gets the default thumbnail image. Overrides MediaTypeBase:: |
|
VideoEmbedField:: |
public | function |
Gets a media-related field/value. Overrides MediaTypeInterface:: |
|
VideoEmbedField:: |
protected | function | Get the video URL from a media entity. | |
VideoEmbedField:: |
protected | function | Load a video provider given a media entity. | |
VideoEmbedField:: |
public | function |
Gets list of fields provided by this plugin. Overrides MediaTypeInterface:: |
|
VideoEmbedField:: |
public | function |
Gets thumbnail image. Overrides MediaTypeInterface:: |
|
VideoEmbedField:: |
constant | The name of the field on the media entity. | ||
VideoEmbedField:: |
public | function |
Constructs a new class instance. Overrides MediaTypeBase:: |