Commit 4180e821 authored by git's avatar git Committed by Scott Euser
Browse files

Issue #3090054 by olivier.br, mr.york, scott_euser, frast: Add the ability to...

Issue #3090054 by olivier.br, mr.york, scott_euser, frast: Add the ability to limit templates that include site settings for performance reasons
parent 651c03d6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
template_key: site_settings
disable_auto_loading: false
+3 −0
Original line number Diff line number Diff line
@@ -5,3 +5,6 @@ site_settings.config:
    template_key:
      type: string
      label: 'The key at which site settings should be made available in templates'
    disable_auto_loading:
      type: boolean
      label: 'Whether the default auto-loading of site settings into all templates should be disabled'
+17 −1
Original line number Diff line number Diff line
@@ -10,9 +10,11 @@
 */
function site_settings_install() {

  // On initial install set template_key to site_settings.
  // On initial install set template_key and disable_auto_loading
  // for site_settings.
  $config = \Drupal::configFactory()->getEditable('site_settings.config');
  $config->set('template_key', 'site_settings');
  $config->set('disable_auto_loading', FALSE);
  $config->save();

}
@@ -166,3 +168,17 @@ function site_settings_update_8006() {
    $schema->dropField('site_setting_entity', 'user_id');
  }
}

/**
 * Set the default auto-loading if upgrading.
 */
function site_settings_update_8007() {

  // On initial install set disable_auto_loading for site_settings.
  $config = \Drupal::configFactory()->getEditable('site_settings.config');
  $disable_auto_loading = $config->get('disable_auto_loading');
  if (!$disable_auto_loading) {
    $config->set('disable_auto_loading', FALSE);
    $config->save();
  }
}
+7 −3
Original line number Diff line number Diff line
@@ -66,11 +66,15 @@ function site_settings_preprocess(&$variables) {
  // with a particular module.
  $config = \Drupal::config('site_settings.config');
  $template_key = $config->get('template_key');
  $disable_auto_loading = $config->get('disable_auto_loading');
  if (!$disable_auto_loading) {
    $template_key = $config->get('template_key');

    // Load the site settings into the specified key.
    $site_settings = \Drupal::service('site_settings.loader');
    $variables[$template_key] = $site_settings->loadAll();
  }
}

/**
 * Process callback for the batch created in the replicate form.
+9 −0
Original line number Diff line number Diff line
@@ -77,6 +77,14 @@ class SiteSettingsConfigForm extends ConfigFormBase {
      ],
    ];

    // Disable autoloading.
    $form['disable_auto_loading'] = [
      '#type' => 'checkbox',
      '#title' => t('Disable auto-loading'),
      '#description' => t('By default, site settings are passed to every template. On a larger site with many templates or a site with many site settings, this can have an impact on performance. Please see the project homepage for details on how to implement your own autoloader in your theme or module. Note that you will need to clear the cache for the change to take effect.'),
      '#default_value' => $config->get('disable_auto_loading'),
    ];

    return parent::buildForm($form, $form_state);
  }

@@ -102,6 +110,7 @@ class SiteSettingsConfigForm extends ConfigFormBase {
    parent::submitForm($form, $form_state);
    $this->config('site_settings.config')
      ->set('template_key', $form_state->getValue('template_key'))
      ->set('disable_auto_loading', $form_state->getValue('disable_auto_loading'))
      ->save();
  }

Loading