-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.module
120 lines (110 loc) · 2.89 KB
/
twitter.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* @file twitter.module
* Twitter API for Drupal
*
* @author Raymond Jelierse
*/
/**
* Implements hook_init()
*/
function twitter_init() {
drupal_set_html_head('<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>');
}
/**
* Implements hook_menu()
*/
function twitter_menu() {
return array(
'admin/settings/twitter' => array(
'title' => 'Twitter',
'description' => 'Manage Twitter settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('twitter_settings_form'),
'access arguments' => array('administer twitter'),
),
);
}
/**
* Implements hook_perm()
*/
function twitter_perm() {
return array('administer twitter');
}
/**
* Implements hook_theme()
*/
function twitter_theme() {
return array(
'twitter_share' => array(
'arguments' => array('node' => NULL),
),
);
}
/**
* Implements hook_nodeapi()
*/
function twitter_nodeapi(&$node, $operation, $data1, $data2) {
switch ($operation) {
case 'view':
$node->content['twitter_share'] = array(
'#value' => theme('twitter_share', $node),
);
$node->twitter_share_rendered = drupal_render($node->content['twitter_share']);
break;
}
}
/**
* Theme the Twitter Share button
*
* @return
* The HTML for a Twitter Share button
*/
function theme_twitter_share($node) {
$share_string = variable_get('twitter_share_string', '');
if (!empty($share_string)) {
$share_string = token_replace_multiple($share_string, array('global' => NULL, 'node' => $node));
}
else {
$share_string = drupal_get_title();
}
$query = array(
'text' => $share_string,
'url' => url($node->path, array('absolute' => TRUE)),
'via' => variable_get('twitter_site_account', ''),
'count' => 'horizontal',
);
$path = 'https://www.twitter.com/share';
return sprintf('<a href="%s" class="twitter-share-button">Tweet</a>', url($path, array('query' => $query)));
}
/**
* Twitter settings
*
* @return
* The Twitter settings form.
*/
function twitter_settings_form() {
return system_settings_form(array(
'twitter_site_account' => array(
'#type' => 'textfield',
'#title' => t('Site wide Twitter account'),
'#default_value' => variable_get('twitter_site_account', ''),
),
'twitter_share_string' => array(
'#type' => 'textfield',
'#title' => t('Text for sharing a node via Twitter'),
'#default_value' => variable_get('twitter_share_string', ''),
),
'twitter_share_string_help' => array(
'#type' => 'fieldset',
'#title' => t('Replacement patterns'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Use -raw replacements for text to avoid problems with HTML entities.'),
'help' => array(
'#type' => 'markup',
'#value' => theme('token_tree', array('node'), TRUE, TRUE),
),
),
));
}