metatag_panels.module 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. $conf = empty($handler->conf['metatag_panels']) ? array() : $handler->conf['metatag_panels']['metatags'];
  53. // Load the metatag form (passed by reference).
  54. metatag_metatags_form($form, $instance, $conf, $options);
  55. // Modify metatag form defaults.
  56. $form['metatags']['#collapsible'] = FALSE;
  57. $form['metatags']['#collapsed'] = FALSE;
  58. // Don't show the Metatag options until it's enabled.
  59. $form['metatags']['#states'] = array(
  60. 'visible' => array(
  61. ':input[name="metatags_enabled"]' => array('checked' => TRUE),
  62. ),
  63. );
  64. return $form;
  65. }
  66. /**
  67. * Submission handler for Metatag panels configuration form.
  68. */
  69. function metatag_panels_form_submit($form, $form_state) {
  70. $conf = array(
  71. 'enabled' => $form_state['values']['metatags_enabled'],
  72. 'metatags' => $form_state['values']['metatags'],
  73. );
  74. $form_state['handler']->conf['metatag_panels'] = $conf;
  75. }
  76. /**
  77. * Implements hook_ctools_render_alter().
  78. */
  79. function metatag_panels_ctools_render_alter($info, $page, $context) {
  80. $output = &drupal_static('metatag_panels');
  81. $handler = $context['handler'];
  82. if (empty($handler->conf['metatag_panels']) || !$handler->conf['metatag_panels']['enabled']) {
  83. return;
  84. }
  85. $metatags = $handler->conf['metatag_panels']['metatags'];
  86. $metatags += metatag_config_load_with_defaults('');
  87. if (empty($metatags)) {
  88. return;
  89. }
  90. // Get the contexts that exist within this panel.
  91. ctools_include('context-task-handler');
  92. $task_object = ctools_context_handler_get_task_object($context['task'], $context['subtask'], $context['handler']);
  93. $task_contexts = ctools_context_load_contexts($task_object, TRUE, $context['contexts']);
  94. // Build the tokens out of CTools contexts.
  95. $tokens = array();
  96. foreach ($task_contexts as $task_context) {
  97. $tokens[$task_context->keyword] = $task_context->data;
  98. }
  99. // Build the Metatag.
  100. $options = array(
  101. 'instance' => 'panels:' . $handler->name,
  102. 'token data' => $tokens,
  103. );
  104. foreach ($metatags as $metatag => $data) {
  105. $metatag_instance = metatag_get_instance($metatag, $data);
  106. if ($metatag_instance) {
  107. $output[$metatag] = $metatag_instance->getElement($options);
  108. }
  109. }
  110. // Give third-parties the opportunity to alter the metatag for a given instance.
  111. drupal_alter('metatag_metatags_view', $output, $options['instance']);
  112. }
  113. /**
  114. * Implements hook_page_build().
  115. *
  116. * @see metatag_panels_ctools_render_alter()
  117. */
  118. function metatag_panels_page_build(&$page) {
  119. $metatags = drupal_static('metatag_panels');
  120. if (!empty($metatags)) {
  121. $page['content']['metatags']['global'] = $metatags;
  122. }
  123. }