-
Notifications
You must be signed in to change notification settings - Fork 1
/
tweetbutton.module
137 lines (119 loc) · 4.48 KB
/
tweetbutton.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
// $Id: tweetbutton.module,v 1.1.2.2 2010/08/13 11:53:09 jasonleon Exp $
/**
* Implementation of hook_help()
*/
function tweetbutton_help($path, $arg) {
$output = NULL;
switch($path) {
case 'admin/help#tweetbutton':
$output = '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.';
}
}
/**
* Implementation of hook_menu()
*/
function tweetbutton_menu() {
$items = array();
$items['admin/config/tweetbutton'] = array(
'title' => 'Tweet Button',
'description' => 'Tweet Button Settings',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer tweetbutton'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
'position' => 'right',
);
$items['admin/config/tweetbutton/settings'] = array(
'title' => 'Tweet Button Settings',
'description' => 'Configure tweet button settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('tweetbutton_admin_settings'),
'access arguments' => array('administer tweetbutton'),
'file' => 'tweetbutton.admin.inc',
'weight' => 0,
);
$items['admin/config/tweetbutton/node'] = array(
'title' => 'Tweet Button Node Settings',
'description' => 'Configure tweet button for nodes',
'page callback' => 'drupal_get_form',
'page arguments' => array('tweetbutton_node_settings'),
'access arguments' => array('administer tweetbutton'),
'file' => 'tweetbutton.admin.inc',
'weight' => 1,
);
return $items;
}
/**
* Implementation of hook_theme()
*/
function tweetbutton_theme() {
return array(
'tweetbutton_display' => array(
'variables' => array('tokens' => NULL),
),
);
}
/**
* Implementation of hook_permission()
*/
function tweetbutton_permission() {
return array(
'administer tweetbutton' => array(
'title' => t('Administer Tweet Button'),
),
'access tweetbutton' => array(
'title' => t('Access Tweet Button'),
),
);
}
/**
* Implementation of hook_node_view()
*/
function tweetbutton_node_view($node, $view_mode) {
if ((in_array($node->type, variable_get('tweetbutton_node_types', array('article')), true)
&& in_array($view_mode, variable_get('tweetbutton_node_location', array('full')))
&& user_access('access tweetbutton')) || $view_mode != 'foo') {
drupal_add_js('http://platform.twitter.com/widgets.js');
drupal_add_css(drupal_get_path('module', 'tweetbutton').'/tweetbutton.css');
// @todo: Add support for token replacements
$token_replacements = array(
'!title' => $node->title,
'!author_name' => $node->name,
'!node_type' => $node->type,
'!node_url' => url('node/'.$node->nid, array('absolute' => TRUE)),
);
$node->content['tweetbutton'] = array(
'#tokens' => $token_replacements,
'#weight' => variable_get('tweetbutton_node_weight', -5),
'#theme' => 'tweetbutton_display',
);
}
}
function tweetbutton_get_attributes($tokens = array()) {
// If data-url is not set then use node url, in teaser mode we want to set this parameter
// to fetch different counters for each node
$node_url = isset($tokens['!node_url'])? $tokens['!node_url']: '';
$button_type = variable_get('tweetbutton_button', 'vertical');
$tweet_text = variable_get('tweetbutton_tweet_text');
$tweet_url = variable_get('tweetbutton_tweet_url');
$language = variable_get('tweetbutton_language');
$account = variable_get('tweetbutton_account');
$rel_account = variable_get('tweetbutton_rel_account_name');
$rel_desc = variable_get('tweetbutton_rel_account_description');
$attributes = array(
'data-count' => $button_type,
'data-via' => $account,
'data-related' => "$rel_account:$rel_desc",
'data-text' => strtr($tweet_text, $tokens),
'data-url' => $tweet_url? $tweet_url: $node_url,
'data-lang' => $language,
);
return $attributes;
}
function theme_tweetbutton_display($variables) {
$attributes = tweetbutton_get_attributes($variables['tokens']);
$link = '<div class="tweetbutton"><a href="http://twitter.com/share" class="twitter-share-button" ' .
drupal_attributes($attributes). '>Tweet</a></div>';
return $link;
}