You are here

function skinr_preprocess in Skinr 7.2

Same name and namespace in other branches
  1. 8.2 skinr.module \skinr_preprocess()
  2. 6.2 skinr.module \skinr_preprocess()
  3. 6 skinr.module \skinr_preprocess()

Implements hook_preprocess().

@todo Optimize this function by removing dependencies on skinr_get_skin_info() and similar resource heavy functions. @todo Account for Drupal's caching being enabled and make it work.

File

./skinr.module, line 74
Handles core Skinr functionality.

Code

function skinr_preprocess(&$variables, $hook) {

  // Fix for update script.
  if (defined('MAINTENANCE_MODE')) {
    return;
  }
  $data =& drupal_static(__FUNCTION__);

  // Caching the data returned from the following functions is reported to
  // improve performance.
  if (!isset($data)) {
    $data['current_theme'] = skinr_current_theme();
    $data['skin_info'] = skinr_get_skin_info();
    $data['theme_registry'] = theme_get_registry();
    $data['skip_cache'] = FALSE;
    $implementations = module_implements('skinr_preprocess_alter');

    // skinr_panels is special case that only runs when skinr_context is enabled.
    if (count($implementations) > 0 && $implementations[0] !== 'skinr_panels') {

      // Skip caching whenever a module implements skinr_preprocess_alter.
      $data['skip_cache'] = TRUE;
    }
  }
  $original_hook = isset($data['theme_registry'][$hook]['original hook']) ? $data['theme_registry'][$hook]['original hook'] : $hook;

  // An array of $elements based on $module and $original_hook, derived from $variables.
  $array_elements = skinr_invoke_all('skinr_elements', $variables, $original_hook, 'preprocess');
  $timeit = FALSE;
  if (is_array($array_elements) && isset($array_elements[$original_hook]) && in_array('system__navigation', $array_elements[$original_hook])) {
    $timeit = TRUE;
  }
  if ($timeit) {
    timer_start(__FUNCTION__ . '__loop');
  }
  foreach ($array_elements as $module => $elements) {
    $applied_skins = array();
    foreach ($elements as $element) {
      if ($timeit) {
        timer_start(__FUNCTION__ . '__cache');
      }
      $cid = 'skinr_preprocess:' . $module . ':' . $element . ':' . $data['current_theme'];
      if (!$data['skip_cache'] && ($cached = cache_get($cid))) {

        // This type of caching is incompatible with skinr_context.
        $applied_skins = $cached->data;
      }
      else {

        // Get a list of skin configuration IDs to pass to
        // skinr_skin_load_multiple().
        $params = array(
          'theme' => $data['current_theme'],
          'module' => $module,
          'element' => $element,
          'status' => 1,
        );
        $sids = skinr_skin_get_sids($params);
        $skins = !empty($sids) ? skinr_skin_load_multiple($sids) : array();

        // Invoke hook_skinr_preprocess_alter() in all modules.
        $context = array(
          'hook' => $hook,
          'variables' => &$variables,
          'theme' => $data['current_theme'],
          'module' => $module,
          'elements' => $elements,
        );
        drupal_alter('skinr_preprocess', $skins, $context);
        $applied_skins = array();
        foreach ($skins as $skin) {
          $applied_skins = array(
            $skin->skin => $skin->options,
          ) + $applied_skins;
        }

        // Cache data.
        if (!$data['skip_cache']) {
          cache_set($cid, $applied_skins);
        }
      }
    }

    // Use drupal_process_attached() to add attachements such as JS and CSS.
    if (!empty($applied_skins)) {
      foreach ($applied_skins as $skin_name => $skin_options) {

        // Special case for _additional.
        if ($skin_name == '_additional') {
          continue;
        }

        // Make sure this skin is enabled for the current theme.
        if (isset($data['skin_info'][$skin_name]['attached'])) {
          $elements = array(
            '#attached' => $data['skin_info'][$skin_name]['attached'],
          );
          drupal_process_attached($elements);
        }
        if (!is_array($skin_options)) {
          $skin_options = array(
            $skin_options,
          );
        }
        foreach ($skin_options as $skin_option) {
          if (isset($data['skin_info'][$skin_name]['options'][$skin_option]['attached'])) {
            $elements = array(
              '#attached' => $data['skin_info'][$skin_name]['options'][$skin_option]['attached'],
            );
            drupal_process_attached($elements);
          }
        }
      }
      $variables['classes_array'] = array_merge($variables['classes_array'], skinr_flatten_skins_array($applied_skins));
    }
  }
}