Commit 86520691 authored by git's avatar git Committed by renatog
Browse files

Issue #3271318 by dave_noga, RenatoG: Prevent JS files from loading on pages with no modals

parent cd951ddb
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -29,10 +29,10 @@ function modal_page_form_alter(&$form, FormStateInterface $form_state, $form_id)
 */
function modal_page_page_attachments(array &$attachments) {

  // Load Modals.
  $modals = \Drupal::entityTypeManager()->getStorage('modal')->loadMultiple();
  // Get Modals to show on current page.
  $modals = \Drupal::service('modal_page.modals')->getModalsToShow();

  // If there is no, skip it.
  // If there are none, skip the rest of the function.
  if (empty($modals)) {
    return FALSE;
  }
@@ -106,16 +106,11 @@ function modal_page_theme() {
 */
function modal_page_preprocess_html(&$variables) {

  // Get Modals to show on this page.
  // Get Modals to show on the current page.
  $modals = \Drupal::service('modal_page.modals')->getModalsToShow();

  // If there are none, skip the rest of the function.
  if (empty($modals)) {
    // If the page has no modals, we can remove all libraries related to the module.
    $libraries = $variables["page"]["#attached"]["library"];
    $bootstrapIndex = array_search("modal_page/modal-page-bootstrap", $libraries);
    unset($libraries[$bootstrapIndex]);
    $modalLibraryIndex = array_search("modal_page/modal", $libraries);
    unset($libraries[$modalLibraryIndex]);
    return FALSE;
  }