metatag_panels.module 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @file
  4. * Main file for metatag_panels module.
  5. */
  6. /**
  7. * Implements hook_page_manager_variant_operations_alter().
  8. */
  9. function metatag_panels_page_manager_variant_operations_alter(&$operations, $handler) {
  10. // Use this obnoxious construct to safely insert our item.
  11. reset($operations['children']);
  12. $children_operations = array();
  13. while (list($key, $value) = each($operations['children'])) {
  14. $children_operations[$key] = $value;
  15. if ($key == 'context') {
  16. $children_operations['meta'] = array(
  17. 'title' => t('Meta tags'),
  18. 'description' => t('Edit variant level meta tags.'),
  19. 'form' => 'metatag_panels_form',
  20. );
  21. }
  22. }
  23. $operations['children'] = $children_operations;
  24. }
  25. /**
  26. * Metatag panels configuration form.
  27. */
  28. function metatag_panels_form($form, $form_state) {
  29. $handler = $form_state['handler'];
  30. // Load available contexts
  31. ctools_include('context-task-handler');
  32. $contexts = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $handler);
  33. // Convert contexts into keywords readable by the token engine.
  34. $token_types = array();
  35. foreach ($contexts as $context) {
  36. if ($context->keyword == 'taxonomy_term') {
  37. $token_types[] = 'term';
  38. }
  39. else {
  40. $token_types[] = $context->keyword;
  41. }
  42. }
  43. // Allow the user to enable/disable meta tags for this panel.
  44. $form['settings']['metatags_enabled'] = array(
  45. '#type' => 'checkbox',
  46. '#title' => t('Enable Metatag configuration.'),
  47. '#default_value' => isset($handler->conf['metatag_panels']['enabled']) ? $handler->conf['metatag_panels']['enabled'] : FALSE,
  48. );
  49. // Don't set any metatag instance name as the configuration data is managed locally within panels.
  50. $instance = '';
  51. $options = array('token types' => $token_types);
  52. $metatags = empty($handler->conf['metatag_panels']) ? array() : $handler->conf['metatag_panels']['metatags'];
  53. // This leaves some possibility for future versions to support translation.
  54. if (!isset($metatags[LANGUAGE_NONE])) {
  55. $metatags = array(LANGUAGE_NONE => $metatags);
  56. }
  57. // Load the metatag form (passed by reference).
  58. metatag_metatags_form($form, $instance, $metatags[LANGUAGE_NONE], $options);
  59. // Modify metatag form defaults.
  60. $form['metatags']['#collapsible'] = FALSE;
  61. $form['metatags']['#collapsed'] = FALSE;
  62. // Don't show the Metatag options until it's enabled.
  63. $form['metatags']['#states'] = array(
  64. 'visible' => array(
  65. ':input[name="metatags_enabled"]' => array('checked' => TRUE),
  66. ),
  67. );
  68. return $form;
  69. }
  70. /**
  71. * Submission handler for Metatag panels configuration form.
  72. */
  73. function metatag_panels_form_submit($form, $form_state) {
  74. $conf = array(
  75. 'enabled' => $form_state['values']['metatags_enabled'],
  76. 'metatags' => $form_state['values']['metatags'][LANGUAGE_NONE],
  77. );
  78. $form_state['handler']->conf['metatag_panels'] = $conf;
  79. }
  80. /**
  81. * Implements hook_ctools_render_alter().
  82. */
  83. function metatag_panels_ctools_render_alter($info, $page, $context) {
  84. $output = &drupal_static('metatag_panels');
  85. $handler = $context['handler'];
  86. if (empty($handler->conf['metatag_panels']) || !$handler->conf['metatag_panels']['enabled']) {
  87. return;
  88. }
  89. $metatags = $handler->conf['metatag_panels']['metatags'];
  90. if (!is_array($metatags) || empty($metatags)) {
  91. $metatags = array();
  92. }
  93. // If meta tags were found but they're not nested for the language, fix it.
  94. // This leaves some possibility for future versions to support translation.
  95. if (!empty($metatags) && !isset($metatags[LANGUAGE_NONE])) {
  96. $metatags = array(LANGUAGE_NONE => $metatags);
  97. }
  98. // Append global defaults.
  99. $all_metatags = array();
  100. foreach ($metatags as $langcode => $values) {
  101. if (!empty($values)) {
  102. $all_metatags = $values + metatag_config_load_with_defaults('');
  103. }
  104. }
  105. $metatags = $all_metatags;
  106. if (empty($metatags)) {
  107. return;
  108. }
  109. // Get the contexts that exist within this panel.
  110. ctools_include('context-task-handler');
  111. $task_object = ctools_context_handler_get_task_object($context['task'], $context['subtask'], $context['handler']);
  112. $task_contexts = ctools_context_load_contexts($task_object, TRUE, $context['contexts']);
  113. // Build the tokens out of CTools contexts.
  114. $tokens = array();
  115. foreach ($task_contexts as $task_context) {
  116. $tokens[$task_context->keyword] = $task_context->data;
  117. }
  118. // Because of page execution order, sometimes the page title does not get set
  119. // by Panels in time for metatags to use it, so we'll explicitly set it here
  120. // if we need to.
  121. if (!empty($info['title'])) {
  122. drupal_set_title($info['title'], PASS_THROUGH);
  123. }
  124. // Build the Metatag.
  125. $options = array(
  126. 'instance' => 'panels:' . $handler->name,
  127. 'token data' => $tokens,
  128. );
  129. foreach ($metatags as $metatag => $data) {
  130. $metatag_instance = metatag_get_instance($metatag, $data);
  131. if ($metatag_instance) {
  132. $output[$metatag] = $metatag_instance->getElement($options);
  133. }
  134. }
  135. // Give third-parties the opportunity to alter the metatag for a given instance.
  136. drupal_alter('metatag_metatags_view', $output, $options['instance']);
  137. }
  138. /**
  139. * Implements hook_page_build().
  140. *
  141. * @see metatag_panels_ctools_render_alter()
  142. */
  143. function metatag_panels_page_build(&$page) {
  144. $metatags = drupal_static('metatag_panels');
  145. if (!empty($metatags)) {
  146. // The page region can be changed.
  147. $region = variable_get('metatag_page_region', 'content');
  148. $page[$region]['metatags']['global'] = $metatags;
  149. }
  150. }