skinr_panels.module 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file
  4. * Provides Skinr integration with Panels.
  5. *
  6. * NOTE: When panels are stored in code, rather than DB, we run into lack of
  7. * context problems. See the below link for a workaround. This is a limitation
  8. * with the Panels module.
  9. *
  10. * @link http://drupal.org/node/1160924 Undefined property: stdClass::$did in panels_skinr_preprocess_index_handler() @endlink
  11. * @link http://drupal.org/node/1292662 Workaround for undefined property: stdClass::$did in panels_skinr_preprocess_index_handler() @endlink
  12. */
  13. /**
  14. * Implements hook_skinr_api().
  15. */
  16. function skinr_panels_skinr_api_2() {
  17. return array();
  18. }
  19. /**
  20. * Implements hook_theme_registry_alter().
  21. *
  22. * Re-order preprocess functions to prioritize skinr_ui_preprocess, which adds
  23. * contextual links, over template_preprocess_HOOK functions. This fixes a
  24. * problem with the way panels handles contextual links.
  25. */
  26. function skinr_panels_theme_registry_alter(&$theme_registry) {
  27. $preprocess_functions = array();
  28. foreach ($theme_registry['panels_pane']['preprocess functions'] as $function) {
  29. if ($function == 'skinr_ui_preprocess' || $function == 'skinr_panels_preprocess') {
  30. continue;
  31. }
  32. $preprocess_functions[] = $function;
  33. if ($function == 'template_preprocess') {
  34. // Insert our preprocess function right after template_preprocess to give it priority over template_preprocess_HOOK functions.
  35. $preprocess_functions[] = 'skinr_panels_preprocess';
  36. $preprocess_functions[] = 'skinr_ui_preprocess';
  37. }
  38. }
  39. $theme_registry['panels_pane']['preprocess functions'] = $preprocess_functions;
  40. // Add a preprocess function to theme_links(). This is a total hack.
  41. $theme_registry['links']['preprocess functions'][] = 'skinr_panels_preprocess_links';
  42. }
  43. /**
  44. * Implements hook_preprocess().
  45. */
  46. function skinr_panels_preprocess(&$variables, $hook) {
  47. if ($hook == 'panels_pane' && user_access('edit skin settings')) {
  48. // Get contextual links.
  49. $contextual_links = array();
  50. $counter = 0;
  51. $array_elements = skinr_invoke_all('skinr_elements', $variables, $hook, 'contextual_links');
  52. $module = 'panels';
  53. $elements = $array_elements[$module];
  54. foreach ($elements as $element) {
  55. $contextual_links['skinr-' . $module . '-' . $counter++] = array(
  56. 'admin/structure/skinr/edit/nojs', array($module, $element),
  57. );
  58. }
  59. if (!empty($contextual_links)) {
  60. // Need to set contextual links through Skinr API so we have a valid, and
  61. // consistent, link title. It's also used in our hook_preprocess_links()
  62. // hack.
  63. _skinr_ui_set_contextual_links($hook, $contextual_links);
  64. // Render links.
  65. $element = array(
  66. '#type' => 'contextual_links',
  67. '#contextual_links' => $contextual_links,
  68. );
  69. $element = contextual_pre_render_links($element);
  70. // Add in the Skinr links.
  71. if (isset($variables['content']->admin_links) && is_array($variables['content']->admin_links)) {
  72. $variables['content']->admin_links += $element['#links'];
  73. }
  74. else {
  75. $variables['content']->admin_links = $element['#links'];
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Implements hook_preprocess_links().
  82. *
  83. * This hack is panels on panel pages only.
  84. */
  85. function skinr_panels_preprocess_links(&$variables, $hook) {
  86. if (isset($variables['links'][0]['title']) && $variables['links'][0]['title'] == t('Edit @type', array('@type' => 'Panel')) && user_access('edit skin settings')) {
  87. // Get contextual links.
  88. $contextual_links = skinr_ui_get_contextual_links();
  89. if (isset($contextual_links['panels_pane'])) {
  90. $contextual_links = $contextual_links['panels_pane'];
  91. // Render links.
  92. $element = array(
  93. '#type' => 'contextual_links',
  94. '#contextual_links' => $contextual_links,
  95. );
  96. $element = contextual_pre_render_links($element);
  97. // Hack in the Skinr links.
  98. $variables['links'] += $element['#links'];
  99. }
  100. }
  101. }