You are here

public static function VideoEmbedField::createVideoEmbedField in Video Embed Field 8

The function that is invoked during the insert of media bundles.

Parameters

string $media_bundle_id: The ID of the media bundle.

2 calls to VideoEmbedField::createVideoEmbedField()
UpgradeManager::upgradeBundle in modules/video_embed_media/src/UpgradeManager.php
Upgrade a whole bundle to use video_embed_field.
video_embed_media_media_bundle_insert in modules/video_embed_media/video_embed_media.module
Implements hook_ENTITY_TYPE_insert() for media_bundle.

File

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

Class

VideoEmbedField
Provides media type plugin for video embed field.

Namespace

Drupal\video_embed_media\Plugin\MediaEntity\Type

Code

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();
}