tweetbutton.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /**
  3. * Implements hook_help().
  4. */
  5. function tweetbutton_help($path, $arg) {
  6. switch ($path) {
  7. case 'admin/help#tweetbutton':
  8. case 'admin/config/services/tweetbutton':
  9. $output = '';
  10. $output .= '<h3>' . t('About') . '</h3>';
  11. $output = '<p>' . t('This button allows your website to let people share content on Twitter without having to leave the page. Promote strategic Twitter accounts at the same time while driving traffic to your website. These are the general configuration options for the button.') . '</p>';
  12. $output .= '<p>' . t('To add tweetbutton you have to create a tweetbutton field and add it to your content (node, taxonomy, user).') . '</p>';
  13. return $output;
  14. }
  15. }
  16. /**
  17. * Implementation of hook_field_info()
  18. */
  19. function tweetbutton_field_info() {
  20. return array(
  21. 'tweetbutton' => array(
  22. 'label' => 'Tweetbutton',
  23. 'description' => 'Creates a tweetbutton field',
  24. 'instance_settings' => array( 'tweet_text' => '', 'author_twitter' => ''),
  25. 'default_widget' => 'tweetbutton',
  26. 'default_formatter' => 'tweetbutton_formatter_button_horizontal',
  27. ),
  28. );
  29. }
  30. /**
  31. * Implementation of hook_field_is_empty()
  32. */
  33. function tweetbutton_field_is_empty($item, $field) {
  34. return FALSE;
  35. }
  36. /**
  37. * Implementation of hook_field_instance_settings_form
  38. */
  39. function tweetbutton_field_instance_settings_form($field, $instance) {
  40. $settings = $instance['settings'];
  41. $form = array();
  42. $form['tweet_text'] = array(
  43. '#type' => 'textfield',
  44. '#title' => t('Tweet text'),
  45. '#default_value' => isset($settings['tweet_text'])? $settings['tweet_text']: variable_get('tweetbutton_tweet_text'),
  46. );
  47. $form['author_twitter'] = array(
  48. '#type' => 'textfield',
  49. '#title' => t('Author twitter account'),
  50. '#default_value' => isset($settings['author_twitter'])? $settings['author_twitter']: variable_get('tweetbutton_account'),
  51. '#description' => t('This user will be @mentioned in the suggested'),
  52. );
  53. if ($instance['entity_type'] == 'taxonomy_term') {
  54. $token_type = 'term';
  55. }
  56. else {
  57. $token_type = $instance['entity_type'];
  58. }
  59. $form['tokens'] = array(
  60. '#token_types' => array($token_type),
  61. '#theme' => 'token_tree',
  62. );
  63. return $form;
  64. }
  65. /**
  66. * Implementation of hook_field_widget_info()
  67. */
  68. function tweetbutton_field_widget_info() {
  69. return array(
  70. 'tweetbutton' => array(
  71. 'label' => t('Tweetbutton'),
  72. 'field types' => array('tweetbutton'),
  73. 'behaviors' => array(
  74. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  75. 'default value' => FIELD_BEHAVIOR_DEFAULT
  76. ),
  77. ),
  78. );
  79. }
  80. /**
  81. * Implementation of hook_field_widget_form()
  82. */
  83. function tweetbutton_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  84. $default_author = isset($instance['settings']['author_twitter'])? $instance['settings']['author_twitter']: '';
  85. $default_text = isset($instance['settings']['tweet_text'])? $instance['settings']['tweet_text']: '';
  86. $element['account'] = array(
  87. '#type' => 'textfield',
  88. '#default_value' => !empty($items[$delta]['account'])? $items[$delta]['account']: $default_author,
  89. '#title' => t('Author twitter account'),
  90. '#description' => t('Leave blank to use global twitter account.'),
  91. '#maxlength' => 128,
  92. '#access' => user_access('use tweetbutton field'),
  93. );
  94. $element['text'] = array(
  95. '#type' => 'textfield',
  96. '#default_value' => !empty($items[$delta]['text'])? $items[$delta]['text']: $default_text,
  97. '#title' => t('Tweet text'),
  98. '#description' => t('Leave blank to use content title as tweet text'),
  99. '#maxlength' => 32,
  100. '#access' => user_access('use tweetbutton field'),
  101. );
  102. return $element;
  103. }
  104. /**
  105. * Implementation of hook_field_presave()
  106. */
  107. function tweetbutton_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  108. if ($field['type'] == 'tweetbutton') {
  109. foreach ($items as $delta => $item) {
  110. $data = array($entity_type => $entity);
  111. $option = array('clear' => TRUE);
  112. $items[$delta]['text'] = !empty($items[$delta]['text'])? token_replace($items[$delta]['text'], $data, $option): $entity->title;
  113. $items[$delta]['account'] = !empty($items[$delta]['account'])? token_replace($items[$delta]['account'], $data, $option): '';
  114. }
  115. }
  116. }
  117. /**
  118. * Implementation of hook_field_formatter_info()
  119. */
  120. function tweetbutton_field_formatter_info() {
  121. return array(
  122. 'tweetbutton_formatter_button_vertical' => array(
  123. 'label' => t('Tweetbutton style vertical'),
  124. 'field types' => array('tweetbutton'),
  125. 'settings' => array(
  126. 'size' => 'medium',
  127. ),
  128. ),
  129. 'tweetbutton_formatter_button_horizontal' => array(
  130. 'label' => t('Tweetbutton style horizontal'),
  131. 'field types' => array('tweetbutton'),
  132. 'settings' => array(
  133. 'size' => 'medium',
  134. ),
  135. ),
  136. 'tweetbutton_formatter_button_none' => array(
  137. 'label' => t('Tweetbutton style none'),
  138. 'field types' => array('tweetbutton'),
  139. 'settings' => array(
  140. 'size' => 'medium',
  141. ),
  142. ),
  143. );
  144. }
  145. /**
  146. * Implements hook_field_formatter_settings_form().
  147. */
  148. function tweetbutton_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  149. $display = $instance['display'][$view_mode];
  150. $settings = $display['settings'];
  151. $element['size'] = array(
  152. '#type' => 'select',
  153. '#title' => t('Tweetbutton size'),
  154. '#options' => array('medium' => t('Medium'), 'large' => t('Large')),
  155. '#default_value' => $settings['size']
  156. );
  157. return $element;
  158. }
  159. /**
  160. * Implements hook_field_formatter_settings_summary().
  161. */
  162. function tweetbutton_field_formatter_settings_summary($field, $instance, $view_mode) {
  163. $display = $instance['display'][$view_mode];
  164. $settings = $display['settings'];
  165. $options = array('medium' => t('Medium'), 'large' => t('Large'));
  166. $title = $options[$settings['size']];
  167. return t('Size: !title', array('!title' => $title));
  168. }
  169. /**
  170. * Implements hook_field_formatter_view().
  171. */
  172. function tweetbutton_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, &$items, $display) {
  173. $settings = $display['settings'];
  174. $delta = 0;
  175. switch ($display['type']) {
  176. case 'tweetbutton_formatter_button_vertical':
  177. $button_type = 'vertical';
  178. break;
  179. case 'tweetbutton_formatter_button_horizontal':
  180. $button_type = 'horizontal';
  181. break;
  182. case 'tweetbutton_formatter_button_none':
  183. $button_type = 'none';
  184. break;
  185. }
  186. if (empty($items)) {
  187. $items[$delta] = array(
  188. 'text' => '',
  189. 'account' => ''
  190. );
  191. }
  192. $uri = entity_uri($entity_type, $entity);
  193. $element[$delta] = array(
  194. '#theme' => 'tweetbutton_tweet_display',
  195. '#options' => array(
  196. 'size' => $settings['size'],
  197. 'type' => $button_type,
  198. 'account' => $items[$delta]['account'],
  199. 'tweet_text' => $items[$delta]['text'],
  200. 'url' => url($uri['path'], array('absolute' => TRUE)),
  201. ),
  202. '#entity' => $entity,
  203. '#entity_type' => $entity_type,
  204. );
  205. return $element;
  206. }
  207. /**
  208. * Implementation of hook_menu()
  209. */
  210. function tweetbutton_menu() {
  211. $items = array();
  212. $items['admin/config/services/tweetbutton'] = array(
  213. 'title' => 'Tweet Button',
  214. 'description' => 'Configure the look and behavior of the Tweet Button appears on the site.',
  215. 'page callback' => 'drupal_get_form',
  216. 'page arguments' => array('tweetbutton_admin_settings'),
  217. 'access arguments' => array('administer tweetbutton'),
  218. 'file' => 'tweetbutton.admin.inc',
  219. 'type' => MENU_NORMAL_ITEM,
  220. );
  221. return $items;
  222. }
  223. /**
  224. * Implementation of hook_theme()
  225. */
  226. function tweetbutton_theme() {
  227. return array(
  228. 'tweetbutton_tweet_display' => array(
  229. 'variables' => array('entity' => NULL, 'entity_type' => NULL, 'options' => array()),
  230. ),
  231. 'tweetbutton_follow_display' => array(
  232. 'variables' => array('options' => array(), 'screen_name' => ''),
  233. ),
  234. 'tweetbutton_hashtag_display' => array(
  235. 'variables' => array('options' => array(), 'hashtag' => ''),
  236. ),
  237. );
  238. }
  239. /**
  240. * Implementation of hook_cron()
  241. */
  242. function tweetbutton_cron() {
  243. // @TODO: Fetch count of each page where tweetbutton is on and add it to column 'count' in field
  244. // Will be useful in sorting page based on no of tweets and add views support
  245. if (!variable_get('tweetbutton_get_count', FALSE)) {
  246. return;
  247. }
  248. }
  249. /**
  250. * Implementation of hook_permission()
  251. */
  252. function tweetbutton_permission() {
  253. return array(
  254. 'administer tweetbutton' => array(
  255. 'title' => t('Administer Tweet Button'),
  256. ),
  257. 'access tweetbutton' => array(
  258. 'title' => t('Access Tweet Button'),
  259. ),
  260. 'use tweetbutton field' => array(
  261. 'title' => t('Use tweetbutton field to alter tweet text')
  262. ),
  263. );
  264. }
  265. /**
  266. * Implementation of hook_block_info()
  267. */
  268. function tweetbutton_block_info() {
  269. $blocks['tweetbutton_tweet'] = array(
  270. 'info' => t('Tweetbutton tweet'),
  271. );
  272. $blocks['tweetbutton_follow'] = array(
  273. 'info' => t('Tweetbutton follow'),
  274. );
  275. return $blocks;
  276. }
  277. /**
  278. * Implementation of hook_block_configure
  279. */
  280. function tweetbutton_block_view($delta = '') {
  281. if ($delta == 'tweetbutton_tweet') {
  282. $block['subject'] = t('Tweetbutton tweet');
  283. $block['content'] = theme('tweetbutton_tweet_display');
  284. return $block;
  285. }
  286. elseif ($delta == 'tweetbutton_follow') {
  287. $block['subject'] = t('Tweetbutton follow');
  288. $block['content'] = theme('tweetbutton_follow_display');
  289. return $block;
  290. }
  291. }
  292. /**
  293. * Helper function to build the required tweet attributes
  294. * @param $options
  295. * Context specific options
  296. * @return
  297. * List of attributes to be rendered for twitter tweetbutton
  298. */
  299. function tweetbutton_tweet_get_attributes($options = array()) {
  300. global $language;
  301. $default_option = array(
  302. 'type' => variable_get('tweetbutton_button', 'vertical'),
  303. 'tweet_text' => variable_get('tweetbutton_tweet_text'),
  304. 'language' => variable_get('tweetbutton_language'),
  305. 'account' => variable_get('tweetbutton_account'),
  306. 'rel_account' => variable_get('tweetbutton_rel_account_name'),
  307. 'rel_desc' => variable_get('tweetbutton_rel_account_description'),
  308. 'size' => variable_get('tweetbutton_size', 'medium')
  309. );
  310. if (empty($options['url'])) {
  311. $options['url'] = url($_GET['q'], array('absolute' => TRUE));
  312. }
  313. if (empty($options['count_url'])) {
  314. $options['count_url'] = $options['url'];
  315. }
  316. if (module_exists('shorten') && ($service = variable_get('tweetbutton_shorten_service'))) {
  317. $url = shorten_url($options['url'], $service);
  318. // if www => http:// replacement if enabled
  319. if (variable_get('shorten_www', TRUE)) {
  320. if (strpos($url, 'www.') === 0) {
  321. $url = drupal_substr($url, 4);
  322. $url = 'http://' . $url;
  323. }
  324. }
  325. $options['url'] = $url;
  326. }
  327. $options += $default_option;
  328. $attributes = array(
  329. 'data-size' => $options['size'],
  330. 'data-count' => $options['type'],
  331. 'data-via' => $options['account'],
  332. 'data-related' => $options['rel_account'] . ':' . $options['rel_desc'],
  333. 'data-text' => $options['tweet_text'],
  334. 'data-counturl'=> $options['count_url'],
  335. 'data-url' => $options['url'],
  336. 'data-lang' => $options['language'] == 'auto' ? $language->language : $options['language'],
  337. 'class' => 'twitter-share-button',
  338. );
  339. return $attributes;
  340. }
  341. function theme_tweetbutton_tweet_display($variables) {
  342. $script_url = url('http://platform.twitter.com/widgets.js', array('external' => TRUE, 'https' => TRUE));
  343. drupal_add_js($script_url, array('type' => 'external', 'scope' => 'footer'));
  344. $options = $variables['options'];
  345. $attributes = tweetbutton_tweet_get_attributes($options);
  346. $tweetbutton_label = isset($options['tweetbutton_label'])? $options['tweetbutton_label']: t('Tweet');
  347. $tweet_link = l($tweetbutton_label, 'http://twitter.com/share', array('attributes' => $attributes, 'external' => TRUE, 'https' => TRUE));
  348. $link = '<div class="tweetbutton-tweet tweetbutton">' . $tweet_link . '</div>';
  349. return $link;
  350. }
  351. function tweetbutton_follow_get_attributes($options) {
  352. global $language;
  353. $default_options = array(
  354. 'show_count' => variable_get('tweetbutton_follow_show_count', TRUE),
  355. 'show_screen_name' => variable_get('tweetbutton_follow_screen_name'),
  356. 'size' => variable_get('tweetbutton_follow_size'),
  357. 'language' => variable_get('tweetbutton_language'),
  358. );
  359. $options += $default_options;
  360. $attributes = array(
  361. 'data-show-count' => $options['show_count'],
  362. 'data-show-screen-name' => $options['show_screen_name'],
  363. 'data-size' => $options['size'],
  364. 'data-lang' => $options['language'] == 'auto' ? $language->language : $options['language'],
  365. 'class' => 'twitter-follow-button',
  366. );
  367. return $attributes;
  368. }
  369. function theme_tweetbutton_follow_display($variables) {
  370. $script_url = url('http://platform.twitter.com/widgets.js', array('external' => TRUE, 'https' => TRUE));
  371. drupal_add_js($script_url, array('type' => 'external', 'scope' => 'footer'));
  372. $options = $variables['options'];
  373. $screen_name = $variables['screen_name'];
  374. if (empty($screen_name)) {
  375. $screen_name = variable_get('tweetbutton_follow_screen_name');
  376. }
  377. $follow_user_text = isset($options['tweetbutton_label'])? $options['tweetbutton_label']: t('Follow !screen_name', array('!screen_name' => $screen_name));
  378. $twitter_account_link = 'http://twitter.com/' . $screen_name;
  379. $attributes = tweetbutton_follow_get_attributes($options);
  380. $follow_link = l($follow_user_text, $twitter_account_link,array('attributes' => $attributes, 'external' => TRUE, 'https' => TRUE));
  381. $link = '<div class="tweetbutton-follow tweetbutton">' . $follow_link . '</div>';
  382. return $link;
  383. }
  384. function tweetbutton_hashtag_get_attributes($options) {
  385. global $language;
  386. $default_options = array(
  387. 'rel_account' => variable_get('tweetbutton_account'),
  388. );
  389. $options += $default_options;
  390. $attributes = array(
  391. 'data-related' => $options['rel_account'],
  392. 'data-lang' => $options['language'] == 'auto'? $language->language: $options['language'],
  393. 'class' => 'twitter-hashtag-button',
  394. );
  395. }
  396. function theme_tweetbutton_hashtag_display($variables) {
  397. $script_url = url('http://platform.twitter.com/widgets.js', array('external' => TRUE));
  398. drupal_add_js($script_url, array('type' => 'external', 'scope' => 'footer'));
  399. $options = $variables['options'];
  400. $attributes = tweetbutton_hashtag_get_attributes($options);
  401. $query = array();
  402. if (!empty($options['tweet_text'])) {
  403. $query['tweet_text'] = $options['tweet_text'];
  404. }
  405. if (!empty($variables['hashtag'])) {
  406. $query['button_hashtag'] = $variables['hashtag'];
  407. }
  408. $hash_link = l($hash_text, 'http://twitter.com/intent/tweet', array('query' => $query, 'attributes' => $attributes, 'external' => TRUE, 'https' => TRUE));
  409. return '<div class="tweetbutton-hashtag tweetbutton">' . $hash_link . '</div>';
  410. }