metatag_context.context.inc 11 KB

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