You are here

function magic_form_system_theme_settings_alter in Magic 7

Same name and namespace in other branches
  1. 7.2 magic.module \magic_form_system_theme_settings_alter()

Implements hook_form_alter().

File

./magic.module, line 215
Keep Frontend DRY; sprinkle it with MAGIC!

Code

function magic_form_system_theme_settings_alter(&$form, &$form_state) {
  $theme = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : '';

  // Are we the global form? We do NOT wanna touch that craziness.
  if (empty($theme)) {
    return;
  }

  // Magic Performance Vertical Tabs set
  $magic_settings = array(
    '#type' => 'vertical_tabs',
    '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'magic') . '/css/magic.admin.css',
      ),
    ),
    '#weight' => 100,
  );

  // CSS Magic Grouping
  $magic_settings['css'] = array(
    '#type' => 'fieldset',
    '#title' => t('CSS Enhancements'),
  );

  // Embedded Media Query Option
  $magic_settings['css']['magic_embedded_mqs'] = array(
    '#type' => 'checkbox',
    '#title' => t('Embed Media Queries'),
    '#description' => t('Embed media queries instead of attaching them to the <pre>&lt;link&gt;</pre> tags, reducing the number of separate CSS aggregates.'),
    '#default_value' => theme_get_setting('magic_embedded_mqs', $theme),
  );

  // CSS Exclude Options
  $magic_settings['css']['magic_css_excludes'] = array(
    '#type' => 'textarea',
    '#title' => t('Exclude CSS files'),
    '#description' => t("<p>One entry per line.</p> <p>The <strong><code>*</code></strong> character is a wildcard to match all similar items, for instance <code>system/*.css</code> will remove all CSS provided by the System module. <p>The <strong><code>~</code></strong> character is a reserved character to keep all similar items if they would otherwise be removed, for instance <code>~system/system.menus.css</code> to keep System module's menu CSS even if we remove the rest of System module's CSS. </p> <p>You may use: <br><strong><code>:all</code></strong> to target all CSS files. <br><strong><code>:core</code></strong> to target all Core provided CSS files. <br><strong><code>:contrib</code></strong> to target all Contrib provided CSS files. <br><strong><code>:base-theme</code></strong> to target all base theme provided CSS files. <br><strong><code>:current-theme</code></strong> to target all CSS files provided by the current theme."),
    '#default_value' => theme_get_setting('magic_css_excludes', $theme),
  );

  // JavaScript Magic Grouping
  $magic_settings['js'] = array(
    '#type' => 'fieldset',
    '#title' => t('JavaScript Enhancements'),
  );

  // Footer JavaScript Option
  $magic_settings['js']['magic_footer_js'] = array(
    '#type' => 'checkbox',
    '#title' => t('Move JavaScript to the Footer'),
    '#description' => t("Will move all JavaScript to the bottom of your page. This can be overridden on an individual basis by setting the <pre>'force header' => true</pre> option in <pre>drupal_add_js</pre> or by using <pre>hook_js_alter</pre> to add the option to other JavaScript files."),
    '#default_value' => theme_get_setting('magic_footer_js', $theme),
  );

  // Keep Libraries in Head JavaScript Option
  $magic_settings['js']['magic_library_head'] = array(
    '#type' => 'checkbox',
    '#title' => t('Keep Libraries in the Head'),
    '#description' => t("If you have JavaScript inline in the body of your document, such as if you are displaying ads, you may need to keep Drupal JS Libraries in the head instead of moving them to the footer. This will keep Drupal libraries in the head while still moving all other JavaScript to the footer."),
    '#default_value' => theme_get_setting('magic_library_head', $theme),
    '#states' => array(
      'visible' => array(
        ':input[name=magic_footer_js]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Experimental JavaScript Option
  $magic_settings['js']['magic_experimental_js'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Experimental JavaScript Handling'),
    '#description' => t("This will enable additional options for JavaScript, including browser options, <pre>async</pre> and <pre>defer</pre> attributes, and optional additional attributes."),
    '#default_value' => theme_get_setting('magic_experimental_js', $theme),
  );

  // JavaScript Exclude Options
  $magic_settings['js']['magic_js_excludes'] = array(
    '#type' => 'textarea',
    '#title' => t('Exclude JavaScript files'),
    '#description' => t("<p>One entry per line.</p> <p>The <strong><code>*</code></strong> character is a wildcard to match all similar items, for instance <code>overlay/*.js</code> will remove all JS provided by the Overlay module.</p> <p>The <strong><code>~</code></strong> character is a reserved character to keep all similar items if they would otherwise be removed, for instance <code>~overlay/overlay-child.js</code> to keep Overlay module's overlay-child JS even if we remove the rest of Overlay module's JS.</p> <p>You may use: <br><strong><code>:all</code></strong> to target all JS files. <br><strong><code>:core</code></strong> to target all Core provided JS files. <br><strong><code>:contrib</code></strong> to target all Contrib provided JS files. <br><strong><code>:base-theme</code></strong> to target all base theme provided JS files. <br><strong><code>:current-theme</code></strong> to target all JS files provided by the current theme. <br><strong><code>:settings</code></strong> to remove the settings array."),
    '#default_value' => theme_get_setting('magic_js_excludes', $theme),
  );

  // We are add an extra submit / validate handler, other can add their own.
  $magic_settings['#submit'] = array();
  $magic_settings['#vaidate'] = array();

  // Run the magic settings through a hook. We do this because magic itself is
  // weighted lower than other modules for the CSS/JS work we do. This will
  // allow modules and themes to actually alter the form array we use.
  $magic_settings_hook = module_invoke_all('magic', $magic_settings, $theme);
  $magic_settings = array_merge_recursive($magic_settings, $magic_settings_hook);

  // Now, the fun part. Drupal doesn't actually allow the theme we are editing
  // to alter it. Because it does weird things. We are going to fix that.
  $all_themes = list_themes();
  $this_theme = $all_themes[$theme];
  $functions = array(
    $theme . '_magic_alter',
  );
  if (isset($this_theme->base_themes)) {
    foreach ($this_theme->base_themes as $base_theme_key => $base_theme) {
      $functions[] = $base_theme_key . '_magic_alter';
    }
  }
  $functions = array_reverse($functions);
  foreach ($functions as $function) {
    if (function_exists($function)) {
      $function($magic_settings, $theme);
    }
  }

  // We now need to merge any custom #submit or #validate hooks and add them to
  // the form. We then unset
  if (!empty($magic_settings['#submit'])) {
    $form['#submit'] = array_merge($form['#submit'], $magic_settings['#submit']);
  }
  unset($magic_settings['#submit']);
  if (!empty($magic_settings['#validate'])) {
    $form['#validate'] = array_merge($form['#validate'], $magic_settings['#validate']);
  }
  unset($magic_settings['#submit']);
  $form['magic_performance'] = $magic_settings;

  // Add export button to export current theme settings
  $form['actions']['export'] = array(
    '#type' => 'button',
    '#value' => t('Export Settings'),
    '#executes_submit_callback' => TRUE,
    '#submit' => array(
      'magic_goto_export',
    ),
  );

  // We need a custom submit handler to store the CSS and JS paths as arrays.
  array_unshift($form['#submit'], 'magic_extension_assets_theme_settings_form_submit');
}