metatag.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. interface DrupalMetaTagInterface {
  3. /**
  4. * Constructor
  5. *
  6. * @param array $info
  7. * The information about the meta tag from metatag_get_info().
  8. */
  9. function __construct(array $info, array $data = array());
  10. function getForm();
  11. //function validateForm();
  12. //function processForm();
  13. function getValue();
  14. function getElement();
  15. function tidyValue($value);
  16. }
  17. class DrupalDefaultMetaTag implements DrupalMetaTagInterface {
  18. protected $info;
  19. protected $data = array('value' => '');
  20. function __construct(array $info, array $data = NULL) {
  21. $this->info = $info;
  22. if (isset($data)) {
  23. $this->data = $data;
  24. }
  25. }
  26. public function getForm(array $options = array()) {
  27. return array();
  28. }
  29. public function getValue(array $options = array()) {
  30. return tidyValue($this->data['value']);
  31. }
  32. public function getElement(array $options = array()) {
  33. $element = isset($this->info['element']) ? $this->info['element'] : array();
  34. $value = $this->getValue($options);
  35. if (strlen($value) === 0) {
  36. return array();
  37. }
  38. $element += array(
  39. '#theme' => 'metatag',
  40. '#tag' => 'meta',
  41. '#id' => 'metatag_' . $this->info['name'],
  42. '#name' => $this->info['name'],
  43. '#value' => $value,
  44. );
  45. // Add header information if desired.
  46. if (!empty($this->info['header'])) {
  47. $element['#attached']['drupal_add_http_header'][] = array($this->info['header'], $value);
  48. }
  49. return array(
  50. '#attached' => array('drupal_add_html_head' => array(array($element, $element['#id']))),
  51. );
  52. }
  53. /**
  54. * Remove unwanted formatting from a meta tag.
  55. *
  56. * @param $value string
  57. * The meta tag value to be tidied up.
  58. *
  59. * @return string
  60. * The meta tag value after it has been tidied up.
  61. */
  62. public function tidyValue($value) {
  63. // Convert any HTML entities into regular characters.
  64. $value = decode_entities($value);
  65. // Remove any HTML code that might have been included.
  66. $value = strip_tags($value);
  67. // Strip errant whitespace.
  68. $value = str_replace(array("\r\n", "\n", "\r", "\t"), ' ', $value);
  69. $value = str_replace(' ', ' ', $value);
  70. $value = str_replace(' ', ' ', $value);
  71. $value = trim($value);
  72. return $value;
  73. }
  74. }
  75. /**
  76. * Text-based meta tag controller.
  77. */
  78. class DrupalTextMetaTag extends DrupalDefaultMetaTag {
  79. public function getForm(array $options = array()) {
  80. $options += array(
  81. 'token types' => array(),
  82. );
  83. $form['value'] = isset($this->info['form']) ? $this->info['form'] : array();
  84. $form['value'] += array(
  85. '#type' => 'textfield',
  86. '#title' => $this->info['label'],
  87. '#description' => !empty($this->info['description']) ? $this->info['description'] : '',
  88. '#default_value' => isset($this->data['value']) ? $this->data['value'] : '',
  89. '#element_validate' => array('token_element_validate'),
  90. '#token_types' => $options['token types'],
  91. '#maxlength' => 1024,
  92. );
  93. return $form;
  94. }
  95. public function getValue(array $options = array()) {
  96. $options += array(
  97. 'instance' => '',
  98. 'token data' => array(),
  99. 'clear' => TRUE,
  100. 'sanitize' => TRUE,
  101. 'raw' => FALSE,
  102. );
  103. $name = "metatag:" . $options["instance"] . ":" . $this->info["name"];
  104. $value = metatag_translate($name, $this->data['value']);
  105. if (empty($options['raw'])) {
  106. // Give other modules the opportunity to use hook_metatag_pattern_alter()
  107. // to modify defined token patterns and values before replacement.
  108. drupal_alter('metatag_pattern', $value, $options['token data']);
  109. $value = token_replace($value, $options['token data'], $options);
  110. }
  111. return $this->tidyValue($value);
  112. }
  113. }
  114. /**
  115. * Link type meta tag controller.
  116. */
  117. class DrupalLinkMetaTag extends DrupalTextMetaTag {
  118. public function getElement(array $options = array()) {
  119. $element = isset($this->info['element']) ? $this->info['element'] : array();
  120. $value = $this->getValue($options);
  121. if (strlen($value) === 0) {
  122. return array();
  123. }
  124. $element += array(
  125. '#theme' => 'metatag_link_rel',
  126. '#tag' => 'link',
  127. '#id' => 'metatag_' . $this->info['name'],
  128. '#name' => $this->info['name'],
  129. '#value' => $value,
  130. );
  131. if (!isset($this->info['header']) || !empty($this->info['header'])) {
  132. // Also send the generator in the HTTP header.
  133. // @todo This does not support 'rev' or alternate link headers.
  134. $element['#attached']['drupal_add_http_header'][] = array('Link', '<' . check_plain($value) . '>;' . drupal_http_header_attributes(array('rel' => $element['#name'])), TRUE);
  135. }
  136. return array(
  137. '#attached' => array('drupal_add_html_head' => array(array($element, $element['#id']))),
  138. );
  139. }
  140. }
  141. /**
  142. * Title meta tag controller.
  143. *
  144. * This extends DrupalTextMetaTag as we need to alter variables in
  145. * template_preprocess_html() rather output a normal meta tag.
  146. */
  147. class DrupalTitleMetaTag extends DrupalTextMetaTag {
  148. public function getElement(array $options = array()) {
  149. $element = array();
  150. $value = check_plain($this->getValue($options));
  151. $element['#attached']['metatag_set_preprocess_variable'][] = array('html', 'head_title', $value);
  152. $element['#attached']['metatag_set_preprocess_variable'][] = array('html', 'head_array', array('title' => $value));
  153. return $element;
  154. }
  155. }
  156. /**
  157. * Multiple value meta tag controller.
  158. */
  159. class DrupalListMetaTag extends DrupalDefaultMetaTag {
  160. function __construct(array $info, array $data = NULL) {
  161. // Ensure that the $data['value] argument is an array.
  162. if (empty($data['value'])) {
  163. $data['value'] = array();
  164. }
  165. $data['value'] = (array) $data['value'];
  166. parent::__construct($info, $data);
  167. }
  168. public function getForm(array $options = array()) {
  169. $form['value'] = isset($this->info['form']) ? $this->info['form'] : array();
  170. $form['value'] += array(
  171. '#type' => 'checkboxes',
  172. '#title' => $this->info['label'],
  173. '#description' => !empty($this->info['description']) ? $this->info['description'] : '',
  174. '#default_value' => isset($this->data['value']) ? $this->data['value'] : array(),
  175. );
  176. return $form;
  177. }
  178. public function getValue(array $options = array()) {
  179. $values = array_keys(array_filter($this->data['value']));
  180. sort($values);
  181. $value = implode(', ', $values);
  182. return $this->tidyValue($value);
  183. }
  184. }
  185. /**
  186. * Date interval meta tag controller.
  187. */
  188. class DrupalDateIntervalMetaTag extends DrupalDefaultMetaTag {
  189. public function getForm(array $options = array()) {
  190. $form['value'] = array(
  191. '#type' => 'textfield',
  192. '#title' => t('@title interval', array('@title' => $this->info['label'])),
  193. '#default_value' => isset($this->data['value']) ? $this->data['value'] : '',
  194. '#element_validate' => array('element_validate_integer_positive'),
  195. '#maxlength' => 4,
  196. '#description' => isset($this->info['description']) ? $this->info['description'] : '',
  197. );
  198. $form['period'] = array(
  199. '#type' => 'select',
  200. '#title' => t('@title interval type', array('@title' => $this->info['label'])),
  201. '#default_value' => isset($this->data['period']) ? $this->data['period'] : '',
  202. '#options' => array(
  203. '' => t('- none -'),
  204. 'day' => t('Day(s)'),
  205. 'week' => t('Week(s)'),
  206. 'month' => t('Month(s)'),
  207. 'year' => t('Year(s)'),
  208. ),
  209. );
  210. return $form;
  211. }
  212. public function getValue(array $options = array()) {
  213. $value = '';
  214. if (!empty($this->data['value'])) {
  215. $interval = intval($this->data['value']);
  216. if (!empty($interval) && !empty($this->data['period'])) {
  217. $period = $this->data['period'];
  218. $value = format_plural($interval, '@count ' . $period, '@count ' . $period . 's');
  219. }
  220. }
  221. return $value;
  222. }
  223. }