metatag_context.context.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @file
  4. * Context reaction for Metatag.
  5. */
  6. class metatag_context_reaction extends context_reaction {
  7. function options_form($context) {
  8. $form = array();
  9. // Don't care about the instance name, the data is being managed by
  10. // Context and not Metatag.
  11. $instance = "";
  12. // Load the previously saved settings.
  13. $data = $this->fetch_from_context($context);
  14. if (!isset($data['metatags'])) {
  15. $data['metatags'] = array();
  16. }
  17. // Support the pre-v1.0 data structures that were not nested with the
  18. // language code.
  19. if (!isset($data['metatags'][LANGUAGE_NONE])) {
  20. $data['metatags'] = array(
  21. LANGUAGE_NONE => $data['metatags'],
  22. );
  23. }
  24. // No options currently available.
  25. $options = array();
  26. $form['help'] = array(
  27. '#prefix' => '<p><em>',
  28. '#markup' => t('Values assigned here inherit from the <a href="@url" title="Edit the global default meta tags.">global defaults</a> and will override any other meta tags assigned elsewhere.', array('@url' => url('admin/config/search/metatags/config/global'))),
  29. '#suffix' => '</em></p>',
  30. );
  31. $form['basic_header'] = array(
  32. '#prefix' => '<hr /><h3>',
  33. '#markup' => t('Basic tags'),
  34. '#suffix' => '</h3>',
  35. );
  36. // Load the basic Metatag form.
  37. metatag_metatags_form($form, $instance, $data['metatags'][LANGUAGE_NONE], $options);
  38. // Stop the meta tag fields appearing within a fieldset.
  39. $form['metatags']['#type'] = 'container';
  40. unset($form['metatags']['#collapsed']);
  41. unset($form['metatags']['#collapsible']);
  42. unset($form['#submit']);
  43. // Flatten the fieldsets because otherwise the Context module will not save
  44. // them properly.
  45. foreach (element_children($form['metatags'][LANGUAGE_NONE]) as $fieldset) {
  46. $child = $form['metatags'][LANGUAGE_NONE][$fieldset];
  47. if (isset($child['#type']) && $child['#type'] == 'fieldset') {
  48. $form['metatags'][LANGUAGE_NONE][$fieldset . '_heading'] = array(
  49. '#prefix' => '<hr /><h3>',
  50. '#markup' => $form['metatags'][LANGUAGE_NONE][$fieldset]['#title'],
  51. '#suffix' => '</h3>',
  52. );
  53. if (isset($form['metatags'][LANGUAGE_NONE][$fieldset]['#description'])) {
  54. $form['metatags'][LANGUAGE_NONE][$fieldset . '_description'] = array(
  55. '#prefix' => '<p>',
  56. '#markup' => $form['metatags'][LANGUAGE_NONE][$fieldset]['#description'],
  57. '#suffix' => '</p>',
  58. );
  59. }
  60. foreach ($form['metatags'][LANGUAGE_NONE][$fieldset] as $key => $value) {
  61. if (substr($key, 0, 1) == '#') {
  62. unset($form['metatags'][LANGUAGE_NONE][$fieldset][$key]);
  63. continue;
  64. }
  65. $form['metatags'][LANGUAGE_NONE][$key] = $value;
  66. unset($form['metatags'][LANGUAGE_NONE][$key]['#parents']);
  67. unset($form['metatags'][LANGUAGE_NONE][$fieldset][$key]);
  68. }
  69. unset($form['metatags'][LANGUAGE_NONE][$fieldset]);
  70. }
  71. }
  72. // Show all takens.
  73. $form['metatags']['tokens']['#token_types'] = 'all';
  74. $form['metatag_admin'] = array(
  75. '#type' => 'checkbox',
  76. '#title' => t('Show on metatag admin page.'),
  77. '#weight' => -98,
  78. '#default_value' => isset($data['metatag_admin']) ? $data['metatag_admin'] : '',
  79. );
  80. return $form;
  81. }
  82. /**
  83. * Output a list of active contexts.
  84. */
  85. function execute() {
  86. $output = &drupal_static('metatag_context');
  87. if (!isset($output)) {
  88. $metatags = array();
  89. $output = array();
  90. $contexts = context_active_contexts();
  91. $options = array();
  92. $instance_names = array();
  93. foreach ($contexts as $context) {
  94. if (!empty($context->reactions['metatag_context_reaction']['metatags'])) {
  95. $metadata_array = $context->reactions['metatag_context_reaction']['metatags'];
  96. if (isset($metadata_array[LANGUAGE_NONE])) {
  97. $metadata_array = $metadata_array[LANGUAGE_NONE];
  98. }
  99. foreach ($metadata_array as $key => $data) {
  100. if (!empty($data['value'])) {
  101. $metatags[$key] = $data;//t(check_plain($data['value']));
  102. }
  103. }
  104. // Add this context to the list of instances.
  105. $instance_names[] = $context->name;
  106. }
  107. }
  108. // Only proceed if metatags were assigned.
  109. if (!empty($metatags)) {
  110. // Load the global defaults.
  111. $metatags += metatag_config_load_with_defaults('');
  112. // Compile the identifier for this combination based on the context
  113. // names.
  114. asort($instance_names);
  115. $instance = 'context:' . implode(',', $instance_names);
  116. $options['instance'] = $instance;
  117. foreach ($metatags as $metatag => $data) {
  118. if ($metatag_instance = metatag_get_instance($metatag, $data)) {
  119. $output[$metatag] = $metatag_instance->getElement($options);
  120. }
  121. }
  122. // Allow the output meta tags to be modified using
  123. // hook_metatag_metatags_view_alter().
  124. drupal_alter('metatag_metatags_view', $output, $instance);
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * Implements hook_context_default_contexts().
  131. */
  132. function metatag_context_context_default_contexts() {
  133. $defaults = array();
  134. $context = new stdClass();
  135. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  136. $context->api_version = 3;
  137. $context->name = 'user_login';
  138. $context->description = 'A default Metatag:Context definition for the user login page. This needs to be enabled and then it can be customized as needed.';
  139. $context->tag = 'Metatag';
  140. $context->conditions = array(
  141. 'path' => array(
  142. 'values' => array(
  143. 'user' => 'user',
  144. 'user/login' => 'user/login',
  145. ),
  146. ),
  147. 'user' => array(
  148. 'values' => array(
  149. 'anonymous user' => 'anonymous user',
  150. ),
  151. ),
  152. );
  153. $context->reactions = array(
  154. 'metatag_context_reaction' => array(
  155. 'metatags' => array(
  156. 'title' => array(
  157. 'value' => '[current-page:title] | [site:name]',
  158. 'default' => '[current-page:title] | [site:name]',
  159. ),
  160. ),
  161. 'metatag_admin' => 1,
  162. ),
  163. );
  164. $context->condition_mode = 1;
  165. $defaults[$context->name] = $context;
  166. $context = new stdClass();
  167. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  168. $context->api_version = 3;
  169. $context->name = 'user_register';
  170. $context->description = 'A default Metatag:Context definition for the user registration page. This needs to be enabled and then it can be customized as needed.';
  171. $context->tag = 'Metatag';
  172. $context->conditions = array(
  173. 'path' => array(
  174. 'values' => array(
  175. 'user/register' => 'user/register',
  176. ),
  177. ),
  178. 'user' => array(
  179. 'values' => array(
  180. 'anonymous user' => 'anonymous user',
  181. ),
  182. ),
  183. );
  184. $context->reactions = array(
  185. 'metatag_context_reaction' => array(
  186. 'metatags' => array(
  187. 'title' => array(
  188. 'value' => '[current-page:title] | [site:name]',
  189. 'default' => '[current-page:title] | [site:name]',
  190. ),
  191. ),
  192. 'metatag_admin' => 1,
  193. ),
  194. );
  195. $context->condition_mode = 1;
  196. $defaults[$context->name] = $context;
  197. if (module_exists('forum')) {
  198. $context = new stdClass();
  199. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  200. $context->api_version = 3;
  201. $context->name = 'forum';
  202. $context->description = 'A default Metatag:Context definition for the main forum page. This needs to be enabled and then it can be customized as needed.';
  203. $context->tag = 'Metatag';
  204. $context->conditions = array(
  205. 'path' => array(
  206. 'values' => array(
  207. 'forum' => 'forum',
  208. ),
  209. ),
  210. );
  211. $context->reactions = array(
  212. 'metatag_context_reaction' => array(
  213. 'metatags' => array(
  214. 'title' => array(
  215. 'value' => '[current-page:title] | [site:name]',
  216. 'default' => '[current-page:title] | [site:name]',
  217. ),
  218. ),
  219. 'metatag_admin' => 1,
  220. ),
  221. );
  222. $context->condition_mode = 1;
  223. $defaults[$context->name] = $context;
  224. }
  225. if (module_exists('blog')) {
  226. $context = new stdClass();
  227. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  228. $context->api_version = 3;
  229. $context->name = 'blog';
  230. $context->description = 'A default Metatag:Context definition for the main blog page. This needs to be enabled and then it can be customized as needed. Note: this does not cover the individual user blog pages, only the main blog page.';
  231. $context->tag = 'Metatag';
  232. $context->conditions = array(
  233. 'path' => array(
  234. 'values' => array(
  235. 'blog' => 'blog',
  236. ),
  237. ),
  238. );
  239. $context->reactions = array(
  240. 'metatag_context_reaction' => array(
  241. 'metatags' => array(
  242. 'title' => array(
  243. 'value' => '[current-page:title] | [site:name]',
  244. 'default' => '[current-page:title] | [site:name]',
  245. ),
  246. ),
  247. 'metatag_admin' => 1,
  248. ),
  249. );
  250. $context->condition_mode = 1;
  251. $defaults[$context->name] = $context;
  252. }
  253. // Translatables
  254. // Included for use with string extractors like potx.
  255. t('Metatag');
  256. return $defaults;
  257. }