metatag_panels.module 8.1 KB

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