metatag_twitter_cards.metatag.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /**
  3. * @file
  4. * Metatag integration for the Metatag Twitter Cards module.
  5. */
  6. /**
  7. * Implements hook_metatag_bundled_config_alter().
  8. */
  9. function metatag_twitter_cards_metatag_bundled_config_alter(array &$configs) {
  10. foreach ($configs as &$config) {
  11. switch ($config->instance) {
  12. case 'global':
  13. $config->config += array(
  14. 'twitter:card' => array('value' => 'summary'),
  15. 'twitter:title' => array('value' => '[current-page:title]'),
  16. 'twitter:url' => array('value' => '[current-page:url:absolute]'),
  17. );
  18. break;
  19. case 'global:frontpage':
  20. $config->config += array(
  21. 'twitter:description' => array('value' => '[site:slogan]'),
  22. 'twitter:title' => array('value' => '[site:name]'),
  23. 'twitter:url' => array('value' => '[site:url]'),
  24. );
  25. break;
  26. // On error pages point everything to the homepage.
  27. case 'global:403':
  28. case 'global:404':
  29. $config->config += array(
  30. 'twitter:title' => array('value' => '[site:name]'),
  31. 'twitter:url' => array('value' => '[site:url]'),
  32. );
  33. break;
  34. case 'node':
  35. $config->config += array(
  36. 'twitter:description' => array('value' => '[node:summary]'),
  37. 'twitter:title' => array('value' => '[node:title]'),
  38. );
  39. break;
  40. case 'taxonomy_term':
  41. $config->config += array(
  42. 'twitter:description' => array('value' => '[term:description]'),
  43. 'twitter:title' => array('value' => '[term:name]'),
  44. );
  45. break;
  46. case 'user':
  47. $config->config += array(
  48. 'twitter:title' => array('value' => '[user:name]'),
  49. );
  50. if (variable_get('user_pictures')) {
  51. $config->config += array(
  52. 'twitter:image' => array('value' => '[user:picture:url]'),
  53. );
  54. }
  55. break;
  56. }
  57. }
  58. }
  59. /**
  60. * Implements hook_metatag_info().
  61. */
  62. function metatag_twitter_cards_metatag_info() {
  63. $info['groups']['twitter-cards'] = array(
  64. 'label' => t('Twitter card'),
  65. 'description' => t('A set of meta tags specially for controlling the summaries displayed when content is shared on <a href="!url">Twitter</a>.', array('!url' => 'https://twitter.com/')),
  66. 'form' => array(
  67. '#weight' => 60,
  68. ),
  69. );
  70. // Twitter Cards meta tags stack after the Open Graph tags.
  71. $weight = 40;
  72. // Defaults used for all cards.
  73. $defaults = array(
  74. 'class' => 'DrupalTextMetaTag',
  75. 'group' => 'twitter-cards',
  76. 'element' => array(
  77. '#theme' => 'metatag_twitter_cards',
  78. ),
  79. );
  80. $info['tags']['twitter:card'] = array(
  81. 'label' => t('Twitter card type'),
  82. 'description' => t('Notes: no other fields are required for a <em>Summary</em> card, a <em>Photo</em> card requires the \'image\' field, a <em>Media player</em> card requires the \'title\', \'description\', \'media player URL\', \'media player width\', \'media player height\' and \'image\' fields, a <em>Summary Card with Large Image</em> card requires the \'Summary\' field and the \'image\' field, a <em>Gallery Card</em> requires all the \'Gallery Image\' fields, an <em>App Card</em> requires the \'iPhone app ID\' field, the \'iPad app ID\' field and the \'Google Play app ID\' field, a <em>Product Card</em> requires the \'description\' field, the \'image\' field, the \'Label 1\' field, the \'Data 1\' field, the \'Label 2\' field and the \'Data 2\' field.'),
  83. 'weight' => ++$weight,
  84. 'form' => array(
  85. '#type' => 'select',
  86. '#options' => array(
  87. 'summary' => t('Summary (default)'),
  88. 'summary_large_image' => t('Summary with large image'),
  89. 'photo' => t('Photo'),
  90. 'player' => t('Media player'),
  91. 'gallery' => t('Gallery'),
  92. 'app' => t('App'),
  93. 'product' => t('Product'),
  94. ),
  95. '#empty_option' => t('- None -'),
  96. ),
  97. ) + $defaults;
  98. $info['tags']['twitter:site'] = array(
  99. 'label' => t('Site\'s Twitter account'),
  100. 'description' => t('The @username for the website, which will be displayed in the Card\'s footer; must include the @ symbol.'),
  101. 'context' => array('global'),
  102. 'weight' => ++$weight,
  103. 'devel_generate' => array(
  104. 'type' => 'twitter',
  105. ),
  106. ) + $defaults;
  107. $info['tags']['twitter:site:id'] = array(
  108. 'label' => t('Site\'s Twitter account ID'),
  109. 'description' => t('The numerical Twitter account ID for the website, which will be displayed in the Card\'s footer.'),
  110. 'context' => array('global'),
  111. 'weight' => ++$weight,
  112. 'devel_generate' => array(
  113. 'type' => 'integer',
  114. ),
  115. ) + $defaults;
  116. $info['tags']['twitter:creator'] = array(
  117. 'label' => t('Creator\'s Twitter account'),
  118. 'description' => t('The @username for the content creator / author for this page, including the @ symbol.'),
  119. 'weight' => ++$weight,
  120. 'devel_generate' => array(
  121. 'type' => 'twitter',
  122. ),
  123. ) + $defaults;
  124. $info['tags']['twitter:creator:id'] = array(
  125. 'label' => t('Creator\'s Twitter account ID'),
  126. 'description' => t('The numerical Twitter account ID for the content creator / author for this page.'),
  127. 'weight' => ++$weight,
  128. 'devel_generate' => array(
  129. 'type' => 'integer',
  130. ),
  131. ) + $defaults;
  132. $info['tags']['twitter:url'] = array(
  133. 'label' => t('Page URL'),
  134. 'description' => t('The permalink / canonical URL of the current page.'),
  135. 'weight' => ++$weight,
  136. 'devel_generate' => array(
  137. 'type' => 'canonical',
  138. ),
  139. ) + $defaults;
  140. $info['tags']['twitter:title'] = array(
  141. 'label' => t('Title'),
  142. 'description' => t('The page\'s title, which should be concise; it will be truncated at 70 characters by Twitter. This field is required unless this the \'type\' field is set to "photo".'),
  143. 'maxlength' => 70,
  144. 'weight' => ++$weight,
  145. ) + $defaults;
  146. $info['tags']['twitter:description'] = array(
  147. 'label' => t('Description'),
  148. 'description' => t('A description that concisely summarizes the content of the page, as appropriate for presentation within a Tweet. Do not re-use the title text as the description, or use this field to describe the general services provided by the website. The string will be truncated, by Twitter, at the word to 200 characters.'),
  149. 'maxlength' => 200,
  150. 'weight' => ++$weight,
  151. ) + $defaults;
  152. $info['tags']['twitter:dnt'] = array(
  153. 'label' => t('Do Not Track'),
  154. 'description' => t('By default Twitter tracks visitors when a tweet is embedded on a page using the official APIs. Setting this to "on" will <a href="@url">stop Twitter from tracking visitors</a>.', array('@url' => 'https://dev.twitter.com/web/overview/privacy#what-privacy-options-do-website-publishers-have')),
  155. 'weight' => ++$weight,
  156. ) + $defaults;
  157. $info['tags']['twitter:image'] = array(
  158. 'label' => t('Image URL'),
  159. 'description' => t('The URL to a unique image representing the content of the page. Do not use a generic image such as your website logo, author photo, or other image that spans multiple pages. Images larger than 120x120px will be resized and cropped square based on longest dimension. Images smaller than 60x60px will not be shown. If the \'type\' is set to <em>Photo</em> then the image must be at least 280x150px.'),
  160. 'image' => TRUE,
  161. 'weight' => ++$weight,
  162. 'replaces' => array(
  163. 'twitter:image:src',
  164. ),
  165. 'devel_generate' => array(
  166. 'type' => 'image',
  167. ),
  168. ) + $defaults;
  169. $info['tags']['twitter:image:width'] = array(
  170. 'label' => t('Image width'),
  171. 'description' => t('The width of the image being linked to, in pixels.'),
  172. 'weight' => ++$weight,
  173. 'dependencies' => array(
  174. array(
  175. 'dependency' => 'twitter:image',
  176. 'attribute' => 'value',
  177. 'condition' => 'filled',
  178. 'value' => TRUE,
  179. ),
  180. ),
  181. 'devel_generate' => array(
  182. 'type' => 'integer',
  183. ),
  184. ) + $defaults;
  185. $info['tags']['twitter:image:height'] = array(
  186. 'label' => t('Image height'),
  187. 'description' => t('The height of the image being linked to, in pixels.'),
  188. 'weight' => ++$weight,
  189. 'dependencies' => array(
  190. array(
  191. 'dependency' => 'twitter:image',
  192. 'attribute' => 'value',
  193. 'condition' => 'filled',
  194. 'value' => TRUE,
  195. ),
  196. ),
  197. 'devel_generate' => array(
  198. 'type' => 'integer',
  199. ),
  200. ) + $defaults;
  201. $info['tags']['twitter:image:alt'] = array(
  202. 'label' => 'Image alternative text',
  203. 'description' => 'The alternative text of the image being linked to. Limited to 420 characters.',
  204. 'weight' => ++$weight,
  205. 'dependencies' => array(
  206. array(
  207. 'dependency' => 'twitter:image',
  208. 'attribute' => 'value',
  209. 'condition' => 'filled',
  210. 'value' => TRUE,
  211. ),
  212. ),
  213. ) + $defaults;
  214. // 'gallery' cards.
  215. $gallery_defaults = array(
  216. 'image' => TRUE,
  217. 'dependencies' => array(
  218. array(
  219. 'dependency' => 'twitter:card',
  220. 'attribute' => 'value',
  221. 'condition' => 'value',
  222. 'value' => 'gallery',
  223. ),
  224. ),
  225. ) + $defaults;
  226. $info['tags']['twitter:image0'] = array(
  227. 'label' => t('1st Gallery Image'),
  228. 'description' => t('A URL to the image representing the first photo in your gallery.'),
  229. 'weight' => ++$weight,
  230. ) + $gallery_defaults;
  231. $info['tags']['twitter:image1'] = array(
  232. 'label' => t('2nd Gallery Image'),
  233. 'description' => t('A URL to the image representing the second photo in your gallery.'),
  234. 'weight' => ++$weight,
  235. ) + $gallery_defaults;
  236. $info['tags']['twitter:image2'] = array(
  237. 'label' => t('3rd Gallery Image'),
  238. 'description' => t('A URL to the image representing the third photo in your gallery.'),
  239. 'weight' => ++$weight,
  240. ) + $gallery_defaults;
  241. $info['tags']['twitter:image3'] = array(
  242. 'label' => t('4th Gallery Image'),
  243. 'description' => t('A URL to the image representing the fourth photo in your gallery.'),
  244. 'weight' => ++$weight,
  245. ) + $gallery_defaults;
  246. // 'player' cards.
  247. $player_defaults = array(
  248. 'dependencies' => array(
  249. array(
  250. 'dependency' => 'twitter:card',
  251. 'attribute' => 'value',
  252. 'condition' => 'value',
  253. 'value' => 'player',
  254. ),
  255. ),
  256. ) + $defaults;
  257. $info['tags']['twitter:player'] = array(
  258. 'label' => t('Media player URL'),
  259. 'description' => t('The full URL for loading a media player. Required when using a <em>Media player</em> card.'),
  260. 'weight' => ++$weight,
  261. 'devel_generate' => array(
  262. 'type' => 'url',
  263. ),
  264. ) + $player_defaults;
  265. $info['tags']['twitter:player:width'] = array(
  266. 'label' => t('Media player width'),
  267. 'description' => t('The width of the media player iframe, in pixels. Required when using a <em>Media player</em> card.'),
  268. 'weight' => ++$weight,
  269. 'devel_generate' => array(
  270. 'type' => 'integer',
  271. ),
  272. ) + $player_defaults;
  273. $info['tags']['twitter:player:height'] = array(
  274. 'label' => t('Media player height'),
  275. 'description' => t('The height of the media player iframe, in pixels. Required when using a <em>Media player</em> card.'),
  276. 'weight' => ++$weight,
  277. 'devel_generate' => array(
  278. 'type' => 'integer',
  279. ),
  280. ) + $player_defaults;
  281. $info['tags']['twitter:player:stream'] = array(
  282. 'label' => t('MP4 media stream URL'),
  283. 'description' => t('The full URL for an MP4 video (h.264) or audio (AAC) stream, takes precidence over the other media player field.'),
  284. 'weight' => ++$weight,
  285. 'devel_generate' => array(
  286. 'type' => 'url',
  287. ),
  288. ) + $player_defaults;
  289. $info['tags']['twitter:player:stream:content_type'] = array(
  290. 'label' => t('MP4 media stream MIME type'),
  291. 'description' => t('The MIME type for the media contained in the stream URL, as defined by <a href="!url">RFC 4337</a>.', array('!url' => 'http://tools.ietf.org/rfc/rfc4337.txt')),
  292. 'weight' => ++$weight,
  293. 'devel_generate' => array(
  294. 'maxlength' => 1,
  295. ),
  296. ) + $player_defaults;
  297. // 'app' cards.
  298. $info['tags']['twitter:app:country'] = array(
  299. 'label' => t('App Store Country'),
  300. 'description' => t('If your application is not available in the US App Store, you must set this value to the two-letter country code for the App Store that contains your application.'),
  301. 'weight' => ++$weight,
  302. 'element' => array(
  303. '#theme' => 'metatag_twitter_cards',
  304. ),
  305. ) + $defaults;
  306. $info['tags']['twitter:app:name:iphone'] = array(
  307. 'label' => t('iPhone app name'),
  308. 'description' => t("The name of the iPhone app."),
  309. 'weight' => ++$weight,
  310. 'element' => array(
  311. '#theme' => 'metatag_twitter_cards',
  312. ),
  313. ) + $defaults;
  314. $info['tags']['twitter:app:id:iphone'] = array(
  315. 'label' => t('iPhone app ID'),
  316. 'description' => t("String value, should be the numeric representation of your iPhone app's ID in the App Store."),
  317. 'weight' => ++$weight,
  318. 'element' => array(
  319. '#theme' => 'metatag_twitter_cards',
  320. ),
  321. ) + $defaults;
  322. $info['tags']['twitter:app:url:iphone'] = array(
  323. 'label' => t('iPhone app\'s custom URL scheme'),
  324. 'description' => t('The iPhone app\'s custom URL scheme (must include "://" after the scheme name).'),
  325. 'weight' => ++$weight,
  326. 'element' => array(
  327. '#theme' => 'metatag_twitter_cards',
  328. ),
  329. ) + $defaults;
  330. $info['tags']['twitter:app:name:ipad'] = array(
  331. 'label' => t('iPad app name'),
  332. 'description' => t("The name of the iPad app."),
  333. 'weight' => ++$weight,
  334. 'element' => array(
  335. '#theme' => 'metatag_twitter_cards',
  336. ),
  337. ) + $defaults;
  338. $info['tags']['twitter:app:id:ipad'] = array(
  339. 'label' => t('iPad app ID'),
  340. 'description' => t("String value, should be the numeric representation of your iPad app's ID in the App Store."),
  341. 'weight' => ++$weight,
  342. 'element' => array(
  343. '#theme' => 'metatag_twitter_cards',
  344. ),
  345. ) + $defaults;
  346. $info['tags']['twitter:app:url:ipad'] = array(
  347. 'label' => t('iPad app\'s custom URL scheme'),
  348. 'description' => t('The iPad app\'s custom URL scheme (must include "://" after the scheme name).'),
  349. 'weight' => ++$weight,
  350. 'element' => array(
  351. '#theme' => 'metatag_twitter_cards',
  352. ),
  353. ) + $defaults;
  354. $info['tags']['twitter:app:name:googleplay'] = array(
  355. 'label' => t('Google Play app name'),
  356. 'description' => t("The name of the app in the Google Play app store."),
  357. 'weight' => ++$weight,
  358. 'element' => array(
  359. '#theme' => 'metatag_twitter_cards',
  360. ),
  361. ) + $defaults;
  362. $info['tags']['twitter:app:id:googleplay'] = array(
  363. 'label' => t('Google Play app ID'),
  364. 'description' => t("Your app ID in the Google Play Store (i.e. \"com.android.app\")."),
  365. 'weight' => ++$weight,
  366. 'element' => array(
  367. '#theme' => 'metatag_twitter_cards',
  368. ),
  369. ) + $defaults;
  370. $info['tags']['twitter:app:url:googleplay'] = array(
  371. 'label' => t('Google Play app\'s custom URL scheme'),
  372. 'description' => t('The Google Play app\'s custom URL scheme (must include "://" after the scheme name).'),
  373. 'weight' => ++$weight,
  374. 'element' => array(
  375. '#theme' => 'metatag_twitter_cards',
  376. ),
  377. ) + $defaults;
  378. // 'product' cards.
  379. $product_defaults = array(
  380. 'dependencies' => array(
  381. array(
  382. 'dependency' => 'twitter:card',
  383. 'attribute' => 'value',
  384. 'condition' => 'value',
  385. 'value' => 'product',
  386. ),
  387. ),
  388. ) + $defaults;
  389. $info['tags']['twitter:label1'] = array(
  390. 'label' => t('Label 1'),
  391. 'description' => t('This field expects a string, and you can specify values for labels such as price, items in stock, sizes, etc.'),
  392. 'weight' => ++$weight,
  393. 'element' => array(
  394. '#theme' => 'metatag_twitter_cards',
  395. ),
  396. ) + $product_defaults;
  397. $info['tags']['twitter:data1'] = array(
  398. 'label' => t('Data 1'),
  399. 'description' => t('This field expects a string, and allows you to specify the types of data you want to offer (price, country, etc.).'),
  400. 'weight' => ++$weight,
  401. 'element' => array(
  402. '#theme' => 'metatag_twitter_cards',
  403. ),
  404. ) + $product_defaults;
  405. $info['tags']['twitter:label2'] = array(
  406. 'label' => t('Label 2'),
  407. 'description' => t('This field expects a string, and you can specify values for labels such as price, items in stock, sizes, etc.'),
  408. 'weight' => ++$weight,
  409. 'element' => array(
  410. '#theme' => 'metatag_twitter_cards',
  411. ),
  412. ) + $product_defaults;
  413. $info['tags']['twitter:data2'] = array(
  414. 'label' => t('Data 2'),
  415. 'description' => t('This field expects a string, and allows you to specify the types of data you want to offer (price, country, etc.).'),
  416. 'weight' => ++$weight,
  417. 'element' => array(
  418. '#theme' => 'metatag_twitter_cards',
  419. ),
  420. ) + $product_defaults;
  421. return $info;
  422. }