You are here

function lightning_media_update_8019 in Lightning Media 8.4

Same name and namespace in other branches
  1. 8.3 lightning_media.install \lightning_media_update_8019()

Installs the core Media library module.

File

./lightning_media.install, line 313
Contains install and update routines for Lightning Media.

Code

function lightning_media_update_8019() {

  // Lightning used to provide a Media Library view mode in the past.
  // This is conflicting with the module's Media library view mode.
  $view_mode = EntityViewMode::load('media.media_library');
  if ($view_mode && !Drupal::moduleHandler()
    ->moduleExists('media_library')) {
    $label = $view_mode
      ->label();
    throw new UpdateException("Can't install Media library, because the media.media_library view mode ({$label}) already exists.");
  }
  Drupal::service('module_installer')
    ->install([
    'media_library',
  ]);
  foreach (MediaType::loadMultiple() as $media_type) {
    $bundle = $media_type
      ->id();
    if (EntityViewDisplay::load("media.{$bundle}.media_library")) {
      continue;
    }

    // If the Media library view mode doesn't already exist create one
    // containing only the thumbnail field.
    $display = EntityViewDisplay::create([
      'targetEntityType' => 'media',
      'bundle' => $bundle,
      'mode' => 'media_library',
      'status' => TRUE,
    ]);
    $display
      ->set('content', []);
    $display
      ->setComponent('thumbnail', [
      'type' => 'image',
      'region' => 'content',
      'label' => 'hidden',
      'settings' => [
        'image_style' => 'thumbnail',
      ],
    ]);
    $display
      ->save();
  }
}