<?php

/**
 * @file
 * Provides Skinr integration with Panels.
 *
 * NOTE: When panels are stored in code, rather than DB, we run into lack of
 *   context problems. See the below link for a workaround. This is a limitation
 *   with the Panels module.
 *
 * @link http://drupal.org/node/1160924 Undefined property: stdClass::$did in panels_skinr_preprocess_index_handler() @endlink
 * @link http://drupal.org/node/1292662 Workaround for undefined property: stdClass::$did in panels_skinr_preprocess_index_handler() @endlink
 */

/**
 * Implements hook_skinr_api().
 */
function skinr_panels_skinr_api_2() {
  return array();
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Re-order preprocess functions to prioritize skinr_ui_preprocess, which adds
 * contextual links, over template_preprocess_HOOK functions. This fixes a
 * problem with the way panels handles contextual links.
 */
function skinr_panels_theme_registry_alter(&$theme_registry) {
  $preprocess_functions = array();
  foreach ($theme_registry['panels_pane']['preprocess functions'] as $function) {
    if ($function == 'skinr_ui_preprocess' || $function == 'skinr_panels_preprocess') {
      continue;
    }

    $preprocess_functions[] = $function;

    if ($function == 'template_preprocess') {
      // Insert our preprocess function right after template_preprocess to give it priority over template_preprocess_HOOK functions.
      $preprocess_functions[] = 'skinr_panels_preprocess';
      $preprocess_functions[] = 'skinr_ui_preprocess';
    }
  }
  $theme_registry['panels_pane']['preprocess functions'] = $preprocess_functions;

  // Add a preprocess function to theme_links(). This is a total hack.
  $theme_registry['links']['preprocess functions'][] = 'skinr_panels_preprocess_links';
}
