You are here

function lightning_media_video_update_8004 in Lightning Media 8.4

Creates the media_library form display for videos.

1 call to lightning_media_video_update_8004()
Update8004Test::testUpdate in modules/lightning_media_video/tests/src/Kernel/Update8004Test.php
Tests the update function.

File

modules/lightning_media_video/lightning_media_video.install, line 60
Contains install and update routines for Lightning Media Video.

Code

function lightning_media_video_update_8004() {
  $config = Config::forModule('lightning_media_video')
    ->optional();
  $type_map = [
    'video' => [
      'remote_video',
      'field_media_oembed_video',
    ],
    'video_file' => [
      'video',
      'field_media_video_file',
    ],
  ];
  $media_types = array_keys($type_map);
  $media_types = MediaType::loadMultiple($media_types);

  // In Lightning Media 4, the video media type was renamed to remote_video and
  // the video_file media type was renamed to video. This update existed prior
  // to Lightning Media 4, so we need to ensure that the media_library form
  // display is created for the existing media types.
  foreach ($media_types as $original_type => $media_type) {
    $form_display = EntityFormDisplay::load("media.{$original_type}.media_library");

    // Only create the form display if it hasn't been created already.
    if (empty($form_display)) {
      list($new_type, $new_source_field) = $type_map[$original_type];

      // Adapt the form display we ship for the destination media type, so that
      // it works with the source media type.
      $values = $config
        ->read("core.entity_form_display.media.{$new_type}.media_library");
      $values['bundle'] = $original_type;
      unset($values['id'], $values['dependencies'], $values['hidden'][$new_source_field]);
      EntityFormDisplay::create($values)
        ->removeComponent($media_type
        ->getSource()
        ->getSourceFieldDefinition($media_type)
        ->getName())
        ->save();
    }
  }
}