diff --git a/core/modules/media_library/media_library.install b/core/modules/media_library/media_library.install index 81ecb72306..43ddd71e93 100644 --- a/core/modules/media_library/media_library.install +++ b/core/modules/media_library/media_library.install @@ -5,6 +5,8 @@ * Install, update and uninstall functions for the media_library module. */ +use Drupal\Core\Config\FileStorage; +use Drupal\Core\Config\InstallStorage; use Drupal\media\Entity\MediaType; use Drupal\views\Entity\View; @@ -12,13 +14,23 @@ * Implements hook_install(). */ function media_library_install() { - // Change the path to the original media view. + // Modify the 'media' view, respecting pre-existing customizations. /** @var \Drupal\views\Entity\View $view */ if ($view = View::load('media')) { $display = &$view->getDisplay('media_page_list'); - if (!empty($display)) { - $display['display_options']['path'] = 'admin/content/media-table'; - unset($display['display_options']['menu']); + if ($display && $display['display_plugin'] === 'page') { + $defaults = _media_library_read_default_media_view(); + + // Change the path, if it hasn't already been changed by the site builder. + if ($display['display_options']['path'] === $defaults['display_options']['path']) { + $display['display_options']['path'] .= '-table'; + } + + // If the default menu options are being used, remove the menu item. + // Otherwise, respect pre-existing customizations. + if (isset($display['display_options']['menu']) && $display['display_options']['menu'] === $defaults['display_options']['menu']) { + unset($display['display_options']['menu']); + } $view->trustData()->save(); } } @@ -34,27 +46,39 @@ function media_library_install() { * Implements hook_uninstall(). */ function media_library_uninstall() { - // Restore the path to the original media view. + // Undo changes to the 'media' view made by media_library_install(). /** @var \Drupal\views\Entity\View $view */ if ($view = View::load('media')) { $display = &$view->getDisplay('media_page_list'); - if (!empty($display)) { - $display['display_options']['path'] = 'admin/content/media'; - $display['display_options']['menu'] = [ - 'type' => 'tab', - 'title' => 'Media', - 'description' => '', - 'expanded' => FALSE, - 'parent' => '', - 'weight' => 0, - 'context' => '0', - 'menu_name' => 'main', - ]; + if ($display && $display['display_plugin'] === 'page') { + $defaults = _media_library_read_default_media_view(); + if ($display['display_options']['path'] === $defaults['display_options']['path'] . '-table') { + $display['display_options']['path'] = $defaults['display_options']['path']; + } + if (!isset($display['display_options']['menu'])) { + $display['display_options']['menu'] = $defaults['display_options']['menu']; + } $view->trustData()->save(); } } } +/** + * Reads the default configuration of the 'media' view. + * + * @return array + * The default configuration of the 'media_page_list' display of the 'media' + * view, as shipped in the Media module's optional config. + */ +function _media_library_read_default_media_view() { + $directory = \Drupal::service('extension.list.module') + ->get('media') + ->getPath(); + + $storage = new FileStorage($directory . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY); + return $storage->read('views.view.media')['display']['media_page_list']; +} + /** * Create the 'media_library' image style. */