-
Notifications
You must be signed in to change notification settings - Fork 1
/
yashare_counters.module
186 lines (163 loc) · 4.64 KB
/
yashare_counters.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* @file
* Main file for the Yandex.Share with Counters module.
*/
// Include field hooks.
require_once __DIR__ . '/yashare_counters.fields.inc';
define('YASHARE_COUNTERS_SMALL', 1);
/**
* Implements hook_menu().
*/
function yashare_counters_menu() {
$items = array();
$items['admin/config/services/yashare_counters'] = array(
'title' => 'Yandex.Share with Counters',
'description' => 'Adjust Yandex.Share buttons options',
'page callback' => 'drupal_get_form',
'page arguments' => array('yashare_counters_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'yashare_counters.admin.inc',
);
return $items;
}
/**
* Implements hook_block_info().
*/
function yashare_counters_block_info() {
$blocks['yashare_counters'] = array(
'info' => t('Yandex.Share with Counters'),
'cache' => DRUPAL_NO_CACHE,
'status' => FALSE,
'weight' => 10,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function yashare_counters_block_configure($delta = '') {
$form = array();
switch ($delta) {
case 'yashare_counters':
$form['fixed_url'] = array(
'#type' => 'textfield',
'#title' => t('Use fixed URL for buttons on all pages'),
'#description' => t('Leave blank for relative URL. <front> is the front page.'),
'#default_value' => variable_get('yashare_counters_fixed_url'),
);
break;
}
return $form;
}
/**
* Implements hook_block_save().
*/
function yashare_counters_block_save($delta = '', $edit = array()) {
switch ($delta) {
case 'yashare_counters':
variable_set('yashare_counters_fixed_url', $edit['fixed_url']);
break;
}
}
/**
* Implements hook_block_view().
*/
function yashare_counters_block_view($delta = '') {
$block = array();
$link = variable_get('yashare_counters_fixed_url');
$title = NULL;
$description = NULL;
$image = NULL;
if ($delta == 'yashare_counters') {
$block['subject'] = t('Share');
$block['content'] = array(
'#markup' => yashare_counters_block_content($link, $title, $description, $image),
'#attached' => array(
'js' => array(
'https://yastatic.net/share2/share.js' => array(
'type' => 'external',
// See https://www.drupal.org/node/1664602.
'async' => 'async',
),
),
),
);
}
return $block;
}
/**
* Generates block content.
*/
function yashare_counters_block_content($link = NULL, $title = NULL, $description = NULL, $image = NULL) {
global $language_content;
$lang = $language_content->language;
$attributes = array();
$attributes['class'][] = 'ya-share2';
$attributes['data-lang'] = $lang;
if ($link) {
$attributes['data-url'] = url(drupal_get_path_alias($link), array('absolute' => TRUE));
}
if ($title) {
$attributes['data-title'] = $title;
}
if ($description) {
$attributes['data-description'] = $description;
}
if ($image) {
$attributes['data-image'] = $image;
}
$service_list = '';
foreach (yashare_counters_services_list() as $service_id => $service_name) {
if (variable_get('yashare_counters_service_' . $service_id . '_enabled', 1)) {
$service_list .= $service_id . ',';
}
}
$attributes['data-services'] = $service_list;
if (variable_get('yashare_counters_small_size', YASHARE_COUNTERS_SMALL)) {
$attributes['data-size'] = 's';
}
$attributes['data-counter'] = 'data-counter';
return '<div' . drupal_attributes($attributes) . '></div>';
}
/**
* Implements hook_theme().
*/
function yashare_counters_theme() {
return array(
'yashare_counters_admin_settings_services_table' => array(
'file' => 'yashare_counters.admin.inc',
'render element' => 'form',
),
);
}
/**
* Internal function.
*/
function yashare_counters_services_list() {
$services = &drupal_static(__FUNCTION__, NULL);
if (!isset($services)) {
$raw_services = array(
// Core providers.
'vkontakte' => t('Vkontakte'),
'facebook' => t('Facebook'),
'twitter' => t('Twitter'),
'odnoklassniki' => t('Odnoklassniki'),
'moimir' => t('Moi mir'),
'gplus' => t('Google Plus'),
'pinterest' => t('Pinterest'),
'linkedin' => t('LinkedIn'),
);
$services = array();
$weights = array();
foreach ($raw_services as $service_id => $service_name) {
$weights[$service_id] = variable_get('yashare_counters_service_' . $service_id . '_weight', 0);
}
asort($weights);
foreach ($weights as $service_id => $weight) {
$services[$service_id] = $raw_services[$service_id];
}
}
return $services;
}