metatag_context.context.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. // Provide token types.
  25. $options = array(
  26. 'token types' => array('node', 'term', 'user'),
  27. );
  28. $form['help'] = array(
  29. '#prefix' => '<p><em>',
  30. '#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'))),
  31. '#suffix' => '</em></p>',
  32. );
  33. $form['basic_header'] = array(
  34. '#prefix' => '<hr /><h3>',
  35. '#markup' => t('Basic tags'),
  36. '#suffix' => '</h3>',
  37. );
  38. // Load the basic Metatag form.
  39. metatag_metatags_form($form, $instance, $data['metatags'][LANGUAGE_NONE], $options);
  40. // Stop the meta tag fields appearing within a fieldset.
  41. $form['metatags']['#type'] = 'container';
  42. unset($form['metatags']['#collapsed']);
  43. unset($form['metatags']['#collapsible']);
  44. unset($form['#submit']);
  45. // Flatten the fieldsets because otherwise the Context module will not save
  46. // them properly.
  47. foreach (element_children($form['metatags'][LANGUAGE_NONE]) as $fieldset) {
  48. $child = $form['metatags'][LANGUAGE_NONE][$fieldset];
  49. if (isset($child['#type']) && $child['#type'] == 'fieldset') {
  50. $form['metatags'][LANGUAGE_NONE][$fieldset . '_heading'] = array(
  51. '#prefix' => '<hr /><h3>',
  52. '#markup' => $form['metatags'][LANGUAGE_NONE][$fieldset]['#title'],
  53. '#suffix' => '</h3>',
  54. );
  55. if (isset($form['metatags'][LANGUAGE_NONE][$fieldset]['#description'])) {
  56. $form['metatags'][LANGUAGE_NONE][$fieldset . '_description'] = array(
  57. '#prefix' => '<p>',
  58. '#markup' => $form['metatags'][LANGUAGE_NONE][$fieldset]['#description'],
  59. '#suffix' => '</p>',
  60. );
  61. }
  62. foreach ($form['metatags'][LANGUAGE_NONE][$fieldset] as $key => $value) {
  63. if (substr($key, 0, 1) == '#') {
  64. unset($form['metatags'][LANGUAGE_NONE][$fieldset][$key]);
  65. continue;
  66. }
  67. $form['metatags'][LANGUAGE_NONE][$key] = $value;
  68. unset($form['metatags'][LANGUAGE_NONE][$key]['#parents']);
  69. unset($form['metatags'][LANGUAGE_NONE][$fieldset][$key]);
  70. }
  71. unset($form['metatags'][LANGUAGE_NONE][$fieldset]);
  72. }
  73. }
  74. // Show all takens.
  75. $form['metatags']['tokens']['#token_types'] = 'all';
  76. $form['metatag_admin'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Show on metatag admin page.'),
  79. '#weight' => -98,
  80. '#default_value' => isset($data['metatag_admin']) ? $data['metatag_admin'] : '',
  81. );
  82. // Add weight for current metatag_context.
  83. $default_weight = 0;
  84. if (!empty($context->reactions['metatag_context_reaction']['weight'])) {
  85. $default_weight = (int) $context->reactions['metatag_context_reaction']['weight'];
  86. }
  87. $form['weight'] = array(
  88. '#type' => 'textfield',
  89. '#title' => t('Weight'),
  90. '#size' => 2,
  91. '#default_value' => $default_weight,
  92. '#description' => t('A higher weight will make this context be executed later, overriding other context meta tags.'),
  93. '#weight' => -99,
  94. );
  95. return $form;
  96. }
  97. /**
  98. * Output a list of active contexts.
  99. */
  100. function execute() {
  101. $output = &drupal_static('metatag_context');
  102. if (!isset($output)) {
  103. $metatags = array();
  104. $output = array();
  105. $contexts = context_active_contexts();
  106. $options = array();
  107. $instance_names = array();
  108. foreach ($contexts as $context) {
  109. if (!empty($context->reactions['metatag_context_reaction']['metatags'])) {
  110. $metadata_array = $context->reactions['metatag_context_reaction']['metatags'];
  111. if (isset($metadata_array[LANGUAGE_NONE])) {
  112. $metadata_array = $metadata_array[LANGUAGE_NONE];
  113. }
  114. // Translate all of the meta tags using i18n, but don't update the
  115. // strings.
  116. metatag_translate_metatags($metadata_array, 'metatag_context:' . $context->name, NULL, FALSE);
  117. // Add the meta tags to the output.
  118. foreach ($metadata_array as $langcode => $tags) {
  119. foreach ($tags as $name => $value) {
  120. $metatags[$langcode][$name] = $value;
  121. }
  122. }
  123. // Add this context to the list of instances.
  124. $weight = isset($context->reactions['metatag_context_reaction']['weight'])
  125. ? $context->reactions['metatag_context_reaction']['weight']
  126. : 0;
  127. $instance_names[] = array('name' => $context->name, 'weight' => $weight);
  128. }
  129. }
  130. // Only proceed if metatags were assigned.
  131. if (!empty($metatags)) {
  132. // Load the global defaults.
  133. $metatags += metatag_config_load_with_defaults('');
  134. // Sort by weight.
  135. uasort($instance_names, 'drupal_sort_weight');
  136. // Keep names only.
  137. $instance_names = array_map('current', $instance_names);
  138. // Compile the identifier for this combination based on the context
  139. // names.
  140. $instance = 'context:' . implode(',', $instance_names);
  141. $options['instance'] = $instance;
  142. // If an entity & entity type were saved elsewhere, grab them for later.
  143. // @see hook_entity_prepare_view().
  144. $entities = drupal_static('metatag_context_entities');
  145. if (!empty($entities) && (count($entities[1]) == 1)) {
  146. $data = array_values($entities[1]);
  147. $options['entity'] = $data[0];
  148. $options['entity_type'] = $entities[0];
  149. $options['token data'] = array($entities[0] => $data[0]);
  150. }
  151. // Don't output meta tags that only contain the pager.
  152. $current_pager = metatag_get_current_pager();
  153. foreach ($metatags as $metatag => $data) {
  154. if ($metatag_instance = metatag_get_instance($metatag, $data)) {
  155. $tag_output = $metatag_instance->getElement($options);
  156. // Don't output the pager if that's all there is.
  157. if ($tag_output != $current_pager) {
  158. $output[$metatag] = $tag_output;
  159. }
  160. }
  161. }
  162. // Allow the output meta tags to be modified using
  163. // hook_metatag_metatags_view_alter().
  164. drupal_alter('metatag_metatags_view', $output, $instance);
  165. }
  166. }
  167. }
  168. }
  169. /**
  170. * Implements hook_context_default_contexts().
  171. */
  172. function metatag_context_context_default_contexts() {
  173. $defaults = array();
  174. $context = new stdClass();
  175. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  176. $context->api_version = 3;
  177. $context->name = 'metatag_context_user_login';
  178. $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.';
  179. $context->tag = 'Metatag';
  180. $context->conditions = array(
  181. 'path' => array(
  182. 'values' => array(
  183. 'user' => 'user',
  184. 'user/login' => 'user/login',
  185. ),
  186. ),
  187. 'user' => array(
  188. 'values' => array(
  189. 'anonymous user' => 'anonymous user',
  190. ),
  191. ),
  192. );
  193. $context->reactions = array(
  194. 'metatag_context_reaction' => array(
  195. 'metatags' => array(
  196. 'und' => array(
  197. 'title' => array(
  198. 'value' => '[current-page:title] | [site:name]',
  199. 'default' => '[current-page:title] | [site:name]',
  200. ),
  201. ),
  202. ),
  203. 'metatag_admin' => 1,
  204. ),
  205. );
  206. $context->condition_mode = 1;
  207. $defaults[$context->name] = $context;
  208. $context = new stdClass();
  209. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  210. $context->api_version = 3;
  211. $context->name = 'metatag_context_user_register';
  212. $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.';
  213. $context->tag = 'Metatag';
  214. $context->conditions = array(
  215. 'path' => array(
  216. 'values' => array(
  217. 'user/register' => 'user/register',
  218. ),
  219. ),
  220. 'user' => array(
  221. 'values' => array(
  222. 'anonymous user' => 'anonymous user',
  223. ),
  224. ),
  225. );
  226. $context->reactions = array(
  227. 'metatag_context_reaction' => array(
  228. 'metatags' => array(
  229. 'und' => array(
  230. 'title' => array(
  231. 'value' => '[current-page:title] | [site:name]',
  232. 'default' => '[current-page:title] | [site:name]',
  233. ),
  234. ),
  235. ),
  236. 'metatag_admin' => 1,
  237. ),
  238. );
  239. $context->condition_mode = 1;
  240. $defaults[$context->name] = $context;
  241. if (module_exists('forum')) {
  242. $context = new stdClass();
  243. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  244. $context->api_version = 3;
  245. $context->name = 'metatag_context_forum';
  246. $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.';
  247. $context->tag = 'Metatag';
  248. $context->conditions = array(
  249. 'path' => array(
  250. 'values' => array(
  251. 'forum' => 'forum',
  252. ),
  253. ),
  254. );
  255. $context->reactions = array(
  256. 'metatag_context_reaction' => array(
  257. 'metatags' => array(
  258. 'und' => array(
  259. 'title' => array(
  260. 'value' => '[current-page:title] | [site:name]',
  261. 'default' => '[current-page:title] | [site:name]',
  262. ),
  263. ),
  264. ),
  265. 'metatag_admin' => 1,
  266. ),
  267. );
  268. $context->condition_mode = 1;
  269. $defaults[$context->name] = $context;
  270. }
  271. if (module_exists('blog')) {
  272. $context = new stdClass();
  273. $context->disabled = TRUE; /* Edit this to true to make a default context disabled initially */
  274. $context->api_version = 3;
  275. $context->name = 'metatag_context_blog';
  276. $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.';
  277. $context->tag = 'Metatag';
  278. $context->conditions = array(
  279. 'path' => array(
  280. 'values' => array(
  281. 'blog' => 'blog',
  282. ),
  283. ),
  284. );
  285. $context->reactions = array(
  286. 'metatag_context_reaction' => array(
  287. 'metatags' => array(
  288. 'und' => array(
  289. 'title' => array(
  290. 'value' => '[current-page:title] | [site:name]',
  291. 'default' => '[current-page:title] | [site:name]',
  292. ),
  293. ),
  294. ),
  295. 'metatag_admin' => 1,
  296. ),
  297. );
  298. $context->condition_mode = 1;
  299. $defaults[$context->name] = $context;
  300. }
  301. // Translatables
  302. // Included for use with string extractors like potx.
  303. t('Metatag');
  304. return $defaults;
  305. }