function MediaLibraryDisplayManager::configureViewDisplay

Same name and namespace in other branches
  1. 11.x core/modules/media_library/src/MediaLibraryDisplayManager.php \Drupal\media_library\MediaLibraryDisplayManager::configureViewDisplay()

Ensures that the given media type has a media_library view display.

Parameters

\Drupal\media\MediaTypeInterface $type: The media type to configure.

Return value

bool Whether a view display has been created or not.

Throws

\Drupal\Core\Entity\EntityStorageException

2 calls to MediaLibraryDisplayManager::configureViewDisplay()
MediaLibraryHooks::mediaTypeFormSubmit in core/modules/media_library/src/Hook/MediaLibraryHooks.php
Submit callback for media type form.
media_library_install in core/modules/media_library/media_library.install
Implements hook_install().

File

core/modules/media_library/src/MediaLibraryDisplayManager.php, line 78

Class

MediaLibraryDisplayManager
The media library form and view display setup.

Namespace

Drupal\media_library

Code

public static function configureViewDisplay(MediaTypeInterface $type) : bool {
  $display = EntityViewDisplay::load('media.' . $type->id() . '.media_library');
  if ($display) {
    return FALSE;
  }
  $values = [
    'targetEntityType' => 'media',
    'bundle' => $type->id(),
    'mode' => 'media_library',
    'status' => TRUE,
  ];
  $display = EntityViewDisplay::create($values);
  // Remove all default components.
  foreach (array_keys($display->getComponents()) as $name) {
    $display->removeComponent($name);
  }
  // @todo Remove dependency on 'medium' and 'thumbnail' image styles from
  //   media and media library modules.
  //   https://www.drupal.org/project/drupal/issues/3030437
  $image_style = ImageStyle::load('medium');
  // Expose the thumbnail component. If the medium image style doesn't exist,
  // use the fallback 'media_library' image style.
  $display->setComponent('thumbnail', [
    'type' => 'image',
    'label' => 'hidden',
    'settings' => [
      'image_style' => $image_style ? $image_style->id() : 'media_library',
      'image_link' => '',
    ],
  ]);
  return (bool) $display->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.