You are here

function advagg_advagg_changed_files in Advanced CSS/JS Aggregation 7.2

Implements hook_advagg_changed_files().

If the color module is enabled regenerate color module css when it changes. If a responsive file inside an adaptive theme has changed, regenerate it.

Related topics

File

./advagg.advagg.inc, line 377
Advanced CSS/JS aggregation module.

Code

function advagg_advagg_changed_files(array $files, array $types) {

  // * @param array $files
  // *   List of files that have changed.
  // * @param array $types
  // *   Array with the css and or the js key.
  if (module_exists('locale')) {
    _advagg_locale_changed_files($files, $types);
  }

  // Keep track of what themes have been done.
  static $themes_done;
  if (!isset($themes_done)) {
    $themes_done = array();
  }

  // Skip if no css changed.
  if (empty($types['css'])) {
    return;
  }
  foreach ($files as $filename => $meta_data) {

    // Only care about css files.
    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
    if ($ext !== 'css') {
      continue;
    }
    advagg_advagg_scan_for_changes($filename, TRUE);
  }

  // Save error states and clear them.
  $errors_before = drupal_static('form_set_error', array());
  form_clear_error();

  // See if the css file is used the theme.
  $themes = list_themes();
  $changed_files = array_keys($files);
  $submit_ran = FALSE;
  foreach ($themes as $theme_name => $theme_values) {
    $files_in_theme = array();
    foreach ($changed_files as $css_file) {

      // Skip if we already did a form submit for this theme.
      if (!empty($themes_done[$theme_name])) {
        continue;
      }

      // Skip if the file that was changed is not in this themes directory.
      $theme_path = drupal_get_path('theme', $theme_name);
      if (!empty($theme_path) && strpos($css_file, $theme_path) !== 0) {
        continue;
      }
      $files_in_theme[] = $css_file;
    }

    // Skip the theme if none of the changed files live in here.
    if (empty($files_in_theme)) {
      continue;
    }

    // Get the form for this theme.
    $router_item = menu_get_item('admin/appearance/settings/' . $theme_name);
    if ($router_item['include_file']) {
      require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
    }
    $form = drupal_get_form('system_theme_settings', $theme_name);

    // Get the form defaults.
    $defaults = array();
    advagg_get_defaults_from_form($defaults, $form);
    $rebuild = FALSE;
    if (isset($defaults['atcore_version_test'])) {

      // Rebuild if the theme is an adaptive theme.
      $rebuild = TRUE;
    }
    if (!$rebuild && module_exists('color')) {
      foreach ($files_in_theme as $css_file) {
        if (isset($form['color'])) {

          // Rebuild if the file that was changed is a color module file.
          foreach ($defaults['info']['css'] as $theme_file) {
            if ($theme_path . '/' . $theme_file === $css_file) {
              $rebuild = TRUE;
              break 2;
            }
          }
        }
      }
    }

    // Skip if themes css does not need to be generated dynamically.
    if (empty($rebuild)) {
      continue;
    }

    // Build the palette value.
    $palette = array();
    if (isset($form['color'])) {
      foreach (element_children($form['color']['palette']) as $key) {
        $palette[$key] = $form['color']['palette'][$key]['#value'];
      }
    }

    // Build the form state array.
    $form_state = array(
      'values' => array(
        'palette' => $palette,
      ),
      'build_info' => array(
        'args' => array(
          0 => $theme_name,
        ),
      ),
    );
    $form_state['values'] += $defaults;
    if (isset($defaults['atcore_version_test'])) {

      // Validate form.
      at_core_settings_validate($form, $form_state);
      $errors = form_set_error();
      if (empty($errors)) {

        // Only submit if no errors.
        at_core_settings_submit($form, $form_state);
        $themes_done[$theme_name] = TRUE;
        $submit_ran = TRUE;
      }
    }
    elseif (isset($form['color'])) {

      // Validate form.
      color_scheme_form_validate($form, $form_state);
      $errors = form_set_error();
      if (empty($errors)) {

        // Only submit if no errors.
        color_scheme_form_submit($form, $form_state);
        $themes_done[$theme_name] = TRUE;
        $submit_ran = TRUE;
      }
    }

    // Reset form errors.
    form_clear_error();
  }

  // Save error states back.
  $form_set_error =& drupal_static('form_set_error', array());
  $form_set_error = $errors_before;

  // Rescan again as the submit will generate new files in the css dir.
  if ($submit_ran) {
    advagg_push_new_changes();
  }
}