metatag_context.context.inc 8.7 KB

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