metatag_panels.module 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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
  50. // locally within panels.
  51. $instance = '';
  52. $options = array('token types' => $token_types);
  53. $metatags = empty($handler->conf['metatag_panels']) ? array() : $handler->conf['metatag_panels']['metatags'];
  54. // This leaves some possibility for future versions to support translation.
  55. if (!isset($metatags[LANGUAGE_NONE])) {
  56. $metatags = array(LANGUAGE_NONE => $metatags);
  57. }
  58. // Load the metatag form (passed by reference).
  59. metatag_metatags_form($form, $instance, $metatags[LANGUAGE_NONE], $options);
  60. // Add CTools substitutions list to the form.
  61. $rows = array();
  62. foreach ($contexts as $context) {
  63. foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
  64. $rows[] = array(
  65. check_plain($keyword),
  66. t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
  67. );
  68. }
  69. }
  70. if (!empty($rows)) {
  71. $form['contexts'] = array(
  72. '#title' => t('Substitutions'),
  73. '#type' => 'fieldset',
  74. '#collapsible' => TRUE,
  75. '#collapsed' => TRUE,
  76. );
  77. $header = array(t('Keyword'), t('Value'));
  78. $form['contexts']['context'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows)));
  79. $form['contexts']['#states'] = array(
  80. 'visible' => array(
  81. ':input[name="metatags_enabled"]' => array('checked' => TRUE),
  82. ),
  83. );
  84. }
  85. // Modify metatag form defaults.
  86. $form['metatags']['#collapsible'] = FALSE;
  87. $form['metatags']['#collapsed'] = FALSE;
  88. // Don't show the Metatag options until it's enabled.
  89. $form['metatags']['#states'] = array(
  90. 'visible' => array(
  91. ':input[name="metatags_enabled"]' => array('checked' => TRUE),
  92. ),
  93. );
  94. return $form;
  95. }
  96. /**
  97. * Submission handler for Metatag panels configuration form.
  98. */
  99. function metatag_panels_form_submit($form, $form_state) {
  100. $conf = array(
  101. 'enabled' => $form_state['values']['metatags_enabled'],
  102. 'metatags' => array(),
  103. );
  104. // Only bother saving the meta tags if they were enabled.
  105. if ($conf['enabled']) {
  106. $conf['metatags'] = $form_state['values']['metatags'][LANGUAGE_NONE];
  107. // Translate the meta tags.
  108. metatag_translations_update($conf['metatags'], 'metatag_panels:' . $form_state['handler']->name);
  109. }
  110. // Save the values for later.
  111. $form_state['handler']->conf['metatag_panels'] = $conf;
  112. }
  113. /**
  114. * Implements hook_ctools_render_alter().
  115. */
  116. function metatag_panels_ctools_render_alter($info, $page, $context) {
  117. // By default do not add meta tags to admin pages. To enable meta tags on
  118. // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  119. if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
  120. return;
  121. }
  122. $output = &drupal_static('metatag_panels');
  123. $handler = $context['handler'];
  124. if (empty($handler->conf['metatag_panels']) || !$handler->conf['metatag_panels']['enabled']) {
  125. return;
  126. }
  127. $metatags = $handler->conf['metatag_panels']['metatags'];
  128. if (!is_array($metatags) || empty($metatags)) {
  129. $metatags = array();
  130. }
  131. // If meta tags were found but they're not nested for the language, fix it.
  132. // This leaves some possibility for future versions to support translation.
  133. if (!empty($metatags) && !isset($metatags[LANGUAGE_NONE])) {
  134. $metatags = array(LANGUAGE_NONE => $metatags);
  135. }
  136. // Translate all of the meta tags using i18n.
  137. metatag_translate_metatags($metatags[LANGUAGE_NONE], 'metatag_panels:' . $handler->name, NULL, FALSE);
  138. // Append global defaults.
  139. $all_metatags = array();
  140. foreach ($metatags as $langcode => $values) {
  141. if (!empty($values)) {
  142. $all_metatags = $values + metatag_config_load_with_defaults('');
  143. }
  144. }
  145. $metatags = $all_metatags;
  146. if (empty($metatags)) {
  147. return;
  148. }
  149. // Substitute Panels context variables.
  150. foreach ($metatags as $metatag => $data) {
  151. if (is_string($data['value']) && strpos($data['value'], '%') !== FALSE) {
  152. $metatags[$metatag]['value'] = ctools_context_keyword_substitute($data['value'], array(), $context['handler']->conf['display']->context);
  153. }
  154. }
  155. // Get the contexts that exist within this panel.
  156. ctools_include('context-task-handler');
  157. $task_object = ctools_context_handler_get_task_object($context['task'], $context['subtask'], $context['handler']);
  158. $task_contexts = ctools_context_load_contexts($task_object, TRUE, $context['contexts']);
  159. // Build the tokens out of CTools contexts.
  160. $tokens = array();
  161. foreach ($task_contexts as $task_context) {
  162. $tokens[$task_context->keyword] = $task_context->data;
  163. }
  164. // Because of page execution order, sometimes the page title does not get set
  165. // by Panels in time for metatags to use it, so we'll explicitly set it here
  166. // if we need to.
  167. if (!empty($info['title'])) {
  168. drupal_set_title($info['title'], PASS_THROUGH);
  169. }
  170. // Don't output meta tags that only contain the pager.
  171. $current_pager = metatag_get_current_pager();
  172. // Build the Metatag.
  173. $options = array(
  174. 'instance' => 'panels:' . $handler->name,
  175. 'token data' => $tokens,
  176. );
  177. foreach ($metatags as $metatag => $data) {
  178. // Render CTools context substitution values prior to rendering the meta
  179. // tag.
  180. if (is_string($data['value'])) {
  181. $data['value'] = ctools_context_keyword_substitute(trim($data['value']), array(), $task_contexts);
  182. }
  183. $metatag_instance = metatag_get_instance($metatag, $data);
  184. if ($metatag_instance) {
  185. $tag_output = $metatag_instance->getElement($options);
  186. // Don't output the pager if that's all there is.
  187. if ($tag_output != $current_pager) {
  188. $output[$metatag] = $tag_output;
  189. }
  190. }
  191. }
  192. // Give third-parties the opportunity to alter the metatag for a given
  193. // instance.
  194. drupal_alter('metatag_metatags_view', $output, $options['instance']);
  195. }
  196. /**
  197. * Implements hook_page_build().
  198. *
  199. * @see metatag_panels_ctools_render_alter()
  200. */
  201. function metatag_panels_page_build(&$page) {
  202. // By default do not add meta tags to admin pages. To enable meta tags on
  203. // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  204. if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
  205. return;
  206. }
  207. $metatags = drupal_static('metatag_panels');
  208. if (!empty($metatags)) {
  209. // The page region can be changed.
  210. $region = variable_get('metatag_page_region', 'content');
  211. $page[$region]['metatags']['global'] = $metatags;
  212. }
  213. }