diff --git a/core/profiles/demo_umami/themes/umami/umami.theme b/core/profiles/demo_umami/themes/umami/umami.theme index 58ac9ed6ad..05c652fc3c 100644 --- a/core/profiles/demo_umami/themes/umami/umami.theme +++ b/core/profiles/demo_umami/themes/umami/umami.theme @@ -8,6 +8,7 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Form\FormStateInterface; use Drupal\search\SearchPageInterface; +use Drupal\views\Form\ViewsForm; /** * Implements hook_preprocess_HOOK() for HTML document templates. @@ -93,3 +94,49 @@ function umami_preprocess_breadcrumb(&$variables) { function umami_form_search_block_form_alter(&$form, FormStateInterface $form_state) { $form['keys']['#attributes']['placeholder'] = t('Search by keyword, ingredient, dish'); } + +/** + * Implements hook_preprocess_links__media_library_menu(). + * + * This targets the menu of available media types in the media library's modal + * dialog. + * + * @todo Do this in the relevant template once + * https://www.drupal.org/project/drupal/issues/3088856 is resolved. + */ +function umami_preprocess_links__media_library_menu(array &$variables) { + foreach ($variables['links'] as &$link) { + // This conditional exists because the media-library-menu__link class is + // currently added by Classy, but Umami will eventually not use Classy as a + // base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see https://www.drupal.org/node/3109287 + // @see classy_preprocess_links__media_library_menu() + if (!isset($link['link']['#options']['attributes']['class']) || !in_array('media-library-menu__link', $link['link']['#options']['attributes']['class'])) { + $link['link']['#options']['attributes']['class'][] = 'media-library-menu__link'; + } + } +} + +/** + * Implements hook_form_alter(). + * + * @todo revisit in https://drupal.org/node/3110132 + */ +function umami_form_alter(array &$form, FormStateInterface $form_state, $form_id) { + $form_object = $form_state->getFormObject(); + + if ($form_object instanceof ViewsForm && strpos($form_object->getBaseFormId(), 'views_form_media_library') === 0) { + // The conditional below exists because the media-library-views-form class + // is currently added by Classy, but Umami will eventually not use Classy as + // a base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see https://www.drupal.org/node/3109287 + // @see classy_form_alter() + if (!isset($form['#attributes']['class']) || !in_array('media-library-views-form', $form['#attributes']['class'])) { + $form['#attributes']['class'][] = 'media-library-views-form'; + } + } +} diff --git a/core/tests/Drupal/Tests/Core/Theme/ClassyPreprocessUnchangedTest.php b/core/tests/Drupal/Tests/Core/Theme/ClassyPreprocessUnchangedTest.php new file mode 100644 index 0000000000..0186c95e5e --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Theme/ClassyPreprocessUnchangedTest.php @@ -0,0 +1,26 @@ +root . '/core/themes/classy/classy.theme'); + $hash = md5($classy_theme_contents); + $this->assertSame($hash, 'c42ff3a1291a258b42f0c44010cd28c7', "The file hash for classy.theme has changed. Any additions or changes to preprocess functions should be added to the themes that inherit Classy. \nIf the changes to classy.theme are not changes to preprocess functions, update the hash in this test to: '$hash' so it will pass."); + } + +} diff --git a/core/themes/bartik/bartik.theme b/core/themes/bartik/bartik.theme index 76394c16ae..eac48ec350 100644 --- a/core/themes/bartik/bartik.theme +++ b/core/themes/bartik/bartik.theme @@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Template\Attribute; +use Drupal\views\Form\ViewsForm; /** * Implements hook_preprocess_HOOK() for HTML document templates. @@ -112,9 +113,10 @@ function bartik_theme_suggestions_form_alter(array &$suggestions, array $variabl } /** - * Implements hook_form_alter() to add classes to the search form. + * Implements hook_form_alter(). */ function bartik_form_alter(&$form, FormStateInterface $form_state, $form_id) { + // Add classes to the search form. if (in_array($form_id, ['search_block_form', 'search_form'])) { $key = ($form_id == 'search_block_form') ? 'actions' : 'basic'; if (!isset($form[$key]['submit']['#attributes'])) { @@ -122,4 +124,44 @@ function bartik_form_alter(&$form, FormStateInterface $form_state, $form_id) { } $form[$key]['submit']['#attributes']->addClass('search-form__submit'); } + + $form_object = $form_state->getFormObject(); + + // Add class to the Media Library views form. + if ($form_object instanceof ViewsForm && strpos($form_object->getBaseFormId(), 'views_form_media_library') === 0) { + // The conditional below exists because the media-library-views-form class + // is currently added by Classy, but Umami will eventually not use Classy as + // a base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see https://www.drupal.org/node/3109287 + // @see classy_form_alter() + if (!isset($form['#attributes']['class']) || !in_array('media-library-views-form', $form['#attributes']['class'])) { + $form['#attributes']['class'][] = 'media-library-views-form'; + } + } +} + +/** + * Implements hook_preprocess_links__media_library_menu(). + * + * This targets the menu of available media types in the media library's modal + * dialog. + * + * @todo Do this in the relevant template once + * https://www.drupal.org/project/drupal/issues/3088856 is resolved. + */ +function bartik_preprocess_links__media_library_menu(array &$variables) { + foreach ($variables['links'] as &$link) { + // This conditional exists because the media-library-menu__link class is + // currently added by Classy, but Bartik will eventually not use Classy as a + // base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see https://www.drupal.org/node/3109287 + // @see classy_preprocess_links__media_library_menu() + if (!isset($link['link']['#options']['attributes']['class']) || !in_array('media-library-menu__link', $link['link']['#options']['attributes']['class'])) { + $link['link']['#options']['attributes']['class'][] = 'media-library-menu__link'; + } + } } diff --git a/core/themes/claro/claro.theme b/core/themes/claro/claro.theme index 6e60bd7072..8982d6c870 100644 --- a/core/themes/claro/claro.theme +++ b/core/themes/claro/claro.theme @@ -18,6 +18,7 @@ use Drupal\Core\Url; use Drupal\media\MediaForm; use Drupal\file\FileInterface; +use Drupal\views\Form\ViewsForm; use Drupal\views\ViewExecutable; /** @@ -374,6 +375,17 @@ function claro_form_alter(array &$form, FormStateInterface $form_state, $form_id $form['header']['media_bulk_form']['#attributes']['class'][] = 'media-library-views-form__bulk_form'; } $form['actions']['submit']['#attributes']['class'] = ['media-library-select']; + + // The conditional below exists because the media-library-views-form class is + // is currently added by Classy, but Claro will eventually not use Classy as + // a base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see https://www.drupal.org/node/3109287 + // @see classy_form_alter() + if (!isset($form['#attributes']['class']) || !in_array('media-library-views-form', $form['#attributes']['class'])) { + $form['#attributes']['class'][] = 'media-library-views-form'; + } } } @@ -1177,6 +1189,14 @@ function claro_preprocess_file_widget_multiple(&$variables) { * Implements hook_preprocess_HOOK() for image_widget. */ function claro_preprocess_image_widget(&$variables) { + // This prevents image widget templates from rendering preview container HTML + // to users that do not have permission to access these previews. + // @todo revisit in https://drupal.org/node/953034 + // @todo revisit in https://drupal.org/node/3114318 + if (isset($variables['data']['preview']['#access']) && $variables['data']['preview']['#access'] === FALSE) { + unset($variables['data']['preview']); + } + // Stable adds the file size as #suffix for image file_link renderable array. // We have to remove that because we will render it in our file_link template // for every kind of files, and not just for images. @@ -1414,3 +1434,26 @@ function claro_views_pre_render(ViewExecutable $view) { } } } + +/** + * Implements hook_preprocess_links__media_library_menu(). + * + * This targets the menu of available media types in the media library's modal + * dialog. + * + * @todo Do this in the relevant template once + * https://www.drupal.org/project/drupal/issues/3088856 is resolved. + */ +function claro_preprocess_links__media_library_menu(array &$variables) { + foreach ($variables['links'] as &$link) { + // This conditional exists because the media-library-menu__link class is + // currently added by Classy, but Claro will eventually not use Classy as a + // base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see classy_preprocess_links__media_library_menu() + if (!isset($link['link']['#options']['attributes']['class']) || !in_array('media-library-menu__link', $link['link']['#options']['attributes']['class'])) { + $link['link']['#options']['attributes']['class'][] = 'media-library-menu__link'; + } + } +} diff --git a/core/themes/classy/classy.theme b/core/themes/classy/classy.theme index c9611e44a3..b0161f8486 100644 --- a/core/themes/classy/classy.theme +++ b/core/themes/classy/classy.theme @@ -3,6 +3,10 @@ /** * @file * Functions to support theming in the Classy theme. + * + * No changes that impact functionality should be made to this file, as Classy + * will be deprecated in Drupal 9. Changes should instead be made in the core + * themes Bartik, Seven, Claro and/or Umami. */ use Drupal\Core\Form\FormStateInterface; @@ -38,7 +42,18 @@ function classy_form_alter(array &$form, FormStateInterface $form_state, $form_i */ function classy_preprocess_image_widget(array &$variables) { $data = &$variables['data']; + + // This prevents image widget templates from rendering preview container HTML + // to users that do not have permission to access these previews. + // @todo revisit in https://drupal.org/node/953034 + // @todo revisit in https://drupal.org/node/3114318 if (isset($data['preview']['#access']) && $data['preview']['#access'] === FALSE) { unset($data['preview']); } } + +/** + * No changes that impact functionality should be made to this file, as Classy + * will be deprecated in Drupal 9. Changes should instead be made in the core + * themes Bartik, Seven, Claro and/or Umami. + */ diff --git a/core/themes/seven/seven.theme b/core/themes/seven/seven.theme index 86c23c6aa2..181758b78d 100644 --- a/core/themes/seven/seven.theme +++ b/core/themes/seven/seven.theme @@ -214,6 +214,17 @@ function seven_form_alter(array &$form, FormStateInterface $form_state, $form_id $form['header']['media_bulk_form']['#attributes']['class'][] = 'media-library-views-form__bulk_form'; } $form['actions']['submit']['#attributes']['class'] = ['media-library-select']; + + // This conditional exists because the media-library-views-form class is + // currently added by Classy, but Seven will eventually not use Classy as a + // base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see https://www.drupal.org/node/3109287 + // @see classy_form_alter() + if (!isset($form['#attributes']['class']) || !in_array('media-library-views-form', $form['#attributes']['class'])) { + $form['#attributes']['class'][] = 'media-library-views-form'; + } } // Add after build to add a CSS class to the form actions. if ($form_id === 'views_exposed_form' && strpos($form['#id'], 'views-exposed-form-media-library-widget') === 0) { @@ -374,3 +385,43 @@ function seven_views_pre_render(ViewExecutable $view) { } } } + +/** + * Implements hook_preprocess_links__media_library_menu(). + * + * This targets the menu of available media types in the media library's modal + * dialog. + * + * @todo Do this in the relevant template once + * https://www.drupal.org/project/drupal/issues/3088856 is resolved. + * @todo revisit in https://drupal.org/node/3110132 + */ +function seven_preprocess_links__media_library_menu(array &$variables) { + foreach ($variables['links'] as &$link) { + // This conditional exists because the media-library-menu__link class is + // currently added by Classy, but Seven will eventually not use Classy as a + // base theme. + // @todo remove conditional, keep class addition in + // https://drupal.org/node/3110137 + // @see https://www.drupal.org/node/3109287 + // @see classy_preprocess_links__media_library_menu() + if (!isset($link['link']['#options']['attributes']['class']) || !in_array('media-library-menu__link', $link['link']['#options']['attributes']['class'])) { + $link['link']['#options']['attributes']['class'][] = 'media-library-menu__link'; + } + } +} + +/** + * Implements hook_preprocess_image_widget(). + */ +function seven_preprocess_image_widget(array &$variables) { + $data = &$variables['data']; + + // This prevents image widget templates from rendering preview container HTML + // to users that do not have permission to access these previews. + // @todo revisit in https://drupal.org/node/953034 + // @todo revisit in https://drupal.org/node/3114318 + if (isset($data['preview']['#access']) && $data['preview']['#access'] === FALSE) { + unset($data['preview']); + } +}