You are here

function fusion_apply_preprocess in Fusion Accelerator 7

Same name and namespace in other branches
  1. 7.2 fusion_apply/fusion_apply.module \fusion_apply_preprocess()

Implements hook_preprocess().

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

File

fusion_apply/fusion_apply.module, line 78
Handles core Fusion Apply functionality.

Code

function fusion_apply_preprocess(&$variables, $hook) {

  // Fix for update script.
  if ($hook == 'maintenance_page') {
    return;
  }
  $config = fusion_apply_get_config_info();
  $current_theme = fusion_apply_current_theme();
  $theme_registry = theme_get_registry();
  $skin_info = fusion_apply_get_skin_info();
  $original_hook = isset($theme_registry[$hook]['original hook']) ? $theme_registry[$hook]['original hook'] : $hook;
  foreach ($config as $module => $module_settings) {
    if (!empty($module_settings['preprocess'][$original_hook])) {
      $preprocess_settings = $module_settings['preprocess'][$original_hook];
      $elements = fusion_apply_handler('preprocess_index_handler', 'preprocess', $preprocess_settings['index_handler'], $variables);
      if (empty($elements)) {

        // We can receive empty arrays; if that happens, there's no point in continuing.
        continue;
      }

      // Get a list of skin configuration IDs to pass to
      // fusion_apply_skin_load_multiple().
      $params = array(
        'theme' => $current_theme,
        'module' => $module,
        'element' => $elements,
        'status' => 1,
      );
      $sids = fusion_apply_skin_get_sids($params);
      if (empty($sids)) {

        // Nothing to apply.
        continue;
      }
      $applied_skins = array();
      foreach (fusion_apply_skin_load_multiple($sids) as $skin) {
        $applied_skins = array(
          $skin->skin => $skin->options,
        ) + $applied_skins;
      }

      // Invoke hook_fusion_apply_preprocess_alter() in all modules.
      // @todo Review whether this alter hook is useful or not, and if it's in
      //   the right place or not.
      $context = array(
        'hook' => $hook,
        'variables' => &$variables,
        'theme' => $current_theme,
        'module' => $module,
        'elements' => $elements,
        'options' => $applied_skins,
      );
      drupal_alter('fusion_apply_preprocess', $context);

      // 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($skin_info[$skin_name]['attached'])) {
            $elements['#attached'] = $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($skin_info[$skin_name]['options'][$skin_option]['attached'])) {
              $elements['#attached'] = $skin_info[$skin_name]['options'][$skin_option]['attached'];
              drupal_process_attached($elements);
            }
          }
        }
        $variables['classes_array'] = array_merge($variables['classes_array'], fusion_apply_flatten_skins_array($applied_skins));
      }
    }
  }
}