You are here

lightning_media_video.module in Lightning Media 8.4

Support for videos as media entities in Lightning.

File

modules/lightning_media_video/lightning_media_video.module
View source
<?php

/**
 * @file
 * Support for videos as media entities in Lightning.
 */
use Drupal\lightning_core\OverrideHelper as Override;
use Drupal\lightning_media\Form\AddByUrlForm;
use Drupal\lightning_media_video\Plugin\media\Source\VideoFile;
use Drupal\media\MediaInterface;

/**
 * Implements hook_media_source_info_alter().
 */
function lightning_media_video_media_source_info_alter(array &$sources) {
  if (isset($sources['video_embed_field'])) {
    $sources['video_embed_field']['input_match'] = [
      'field_types' => [
        'string',
        'string_long',
        'video_embed_field',
      ],
    ];
    $sources['video_embed_field']['preview'] = TRUE;
    $sources['video_embed_field']['forms']['media_library_add'] = AddByUrlForm::class;

    // Since the base class will not exist if video_embed_field is not enabled,
    // we cannot import this class at the top of the file, or things blow up.
    Override::pluginClass($sources['video_embed_field'], '\\Drupal\\lightning_media_video\\Plugin\\media\\Source\\Video');
  }
  Override::pluginClass($sources['video_file'], VideoFile::class);
}

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function lightning_media_video_media_view_alter(array &$build, MediaInterface $media) {

  // If we are rendering an embedded video, give it a minimum width so that the
  // responsive styles provided by Video Embed Field don't accidentally cause
  // the video to be "invisible".
  if (isset($build['#embed']) && $media
    ->getSource()
    ->getPluginId() === 'video_embed_field') {
    $build['#attributes']['style'][] = 'min-width: 50%;';
  }
}