forked from WhiteHouse/social_share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
social_share.module
584 lines (522 loc) · 21.6 KB
/
social_share.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<?php
/**
* @file
* Implements configurable social network share links to nodes
*/
/**
* Implements hook_menu().
*/
function social_share_menu() {
$items = array();
$items['admin/config/content/social-share'] = array(
'title' => 'Social Share',
'description' => 'Configure share link styling.',
'page callback' => 'drupal_get_form',
'page arguments' => array('social_share_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file path' => drupal_get_path('module', 'social_share') .'/include',
'file' => 'social_share.admin.inc',
);
$items['admin/config/content/social-share/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('social_share_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file path' => drupal_get_path('module', 'social_share') .'/include',
'file' => 'social_share.admin.inc',
);
$items['admin/config/content/social-share/networks'] = array(
'title' => 'Networks',
'page callback' => 'drupal_get_form',
'page arguments' => array('social_share_admin_networks'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file path' => drupal_get_path('module', 'social_share') .'/include',
'file' => 'social_share.networks.inc',
);
$items['admin/config/content/social-share/networks/new'] = array(
'title' => 'Networks',
'page callback' => 'drupal_get_form',
'page arguments' => array('social_share_admin_networks_network'),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
'file path' => drupal_get_path('module', 'social_share') .'/include',
'file' => 'social_share.networks.inc',
);
$items['admin/config/content/social-share/networks/%/edit'] = array(
'title' => 'Networks',
'page callback' => 'drupal_get_form',
'page arguments' => array('social_share_admin_networks_network', 5),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
'file path' => drupal_get_path('module', 'social_share') .'/include',
'file' => 'social_share.networks.inc',
);
$items['admin/config/content/social-share/networks/%/remove'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('social_share_admin_networks_remove', 5),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
'file path' => drupal_get_path('module', 'social_share') .'/include',
'file' => 'social_share.networks.inc',
);
return $items;
}
/**
* Implements hook_theme().
*/
function social_share_theme() {
return array(
'social_share_links' => array( // Wrapper to contain individual social share links
'file' => 'social_share.theme.inc',
'path' => drupal_get_path('module', 'social_share') .'/include',
'variables' => array(
'nid' => NULL,
'node' => NULL,
'block' => FALSE,
),
),
'social_share_link' => array( // Render a single share link (unstyled)
'file' => 'social_share.theme.inc',
'path' => drupal_get_path('module', 'social_share') .'/include',
'variables' => array(
'network' => NULL,
'node' => NULL,
),
),
'social_share_small_icon' => array( // Render a single share link (small icon)
'file' => 'social_share.theme.inc',
'path' => drupal_get_path('module', 'social_share') .'/include',
'variables' => array(
'network' => NULL,
'node' => NULL,
'block' => FALSE,
),
),
'social_share_large_icon' => array( // Render a single share link (larger icon)
'file' => 'social_share.theme.inc',
'path' => drupal_get_path('module', 'social_share') .'/include',
'variables' => array(
'network' => NULL,
'node' => NULL,
'block' => FALSE,
),
),
'social_share_admin_networks' => array(
'render element' => 'form',
'file' => 'social_share.theme.inc',
'path' => drupal_get_path('module', 'social_share') .'/include',
),
);
}
/**
* Implements hook_node_view().
*/
function social_share_node_view($node, $view_mode, $language) {
if (variable_get('social_share_enabled_' . $node->type, 0)) {
$node->content['social_share'] = theme('social_share_links', array('node' => $node));
}
}
/**
* Implements hook_preprocess_node().
*/
function social_share_preprocess_node(&$variables) {
// Apply a single listener to each facebook share link independently, overriding
// the previous share behavior.
$node_type = $variables['type'];
$node_url = $variables['node_url'];
$facebook_app_id = variable_get('social_share_fb_app_id', NULL);
$sharing_is_enabled = (bool) variable_get('social_share_enabled_' . $node_type, 0);
$social_networks = variable_get('social_share_networks_' . $node_type, array());
$facebook_link_is_enabled = in_array('facebook', $social_networks);
if (!empty($facebook_app_id) && $sharing_is_enabled && $facebook_link_is_enabled) {
$absolute_node_url = url(ltrim($node_url, '/'), array('absolute' => TRUE));
$selector = '.social-share-node-' . $variables['nid'] . ' .social-share-facebook';
$inline_js = _social_share_facebook_override_script($selector, $absolute_node_url);
drupal_add_js($inline_js, array('type' => 'inline'));
}
}
/**
* Implements hook_preprocess_page().
*/
function social_share_preprocess_page(&$variables) {
// Load the Facebook Javascript SDK if they have provided an App ID. It loads
// on every page, because it may be needed for blocks or nodes.
$facebook_app_id = variable_get('social_share_fb_app_id', NULL);
if (!empty($facebook_app_id)) {
drupal_add_js(array('socialShare' => array('appID' => $facebook_app_id)), 'setting');
drupal_add_js(drupal_get_path('module', 'social_share') . '/js/social_share_facebook_sdk.js', 'file');
}
}
/**
* Implements hook_block_info().
*/
function social_share_block_info() {
$blocks = array();
if (variable_get('social_share_block', 0)) {
$blocks['social_share'] = array(
'info' => t('Social Share'),
'visibility' => 1,
'status' => TRUE,
'region' => 'header',
'weight' => 0,
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function social_share_block_configure($delta = '') {
$form = array();
if ($delta == 'social_share') {
$settings = variable_get('social_share_block_settings', array());
$form['social_share'] = array(
'#type' => 'fieldset',
'#title' => t('Social Share Settings'),
'#description' => t('Configure site-wide social share settings to use in this block.'),
'#weight' => 8,
'#collapsible' => FALSE,
);
$form['social_share']['social_share_theme'] = array(
'#type' => 'select',
'#title' => t('Display Format'),
'#description' => t('Select the display format for social share links.<br /><strong>NOTE:</strong> If you want to provide your own icons, or prefer text links, select none and add css as needed to your theme.'),
'#default_value' => isset($settings['social_share_theme']) ? $settings['social_share_theme'] : 'social_share_link_block',
'#options' => array(
'social_share_link' => t('Unstyled Links'),
'social_share_small_icon' => t('Small Icons (16px)'),
'social_share_large_icon' => t('Large Icons (32px)'),
),
);
$form['social_share']['social_share_show_label'] = array(
'#type' => 'checkbox',
'#title' => t('Display share label'),
'#description' => t('If checked, the share label !config_page_link will be displayed before any enabled social network share links when displaying nodes of this type.',
array('!config_page_link' => l('as configured here', 'admin/config/content/social-share'))),
'#default_value' => isset($settings['social_share_show_label']) ? $settings['social_share_show_label'] : 0,
);
$form['social_share']['social_share_replacements'] = array(
'#type' => 'fieldset',
'#title' => t('Replacements'),
'#description' => t('Specify the values to use for placeholders in the social share provider urls'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
);
$form['social_share']['social_share_replacements']['social_share_title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('Select the value to use for the title when sharing from this block.'),
'#default_value' => isset($settings['social_share_title']) ? $settings['social_share_title'] : '[current-page:title]',
'#required' => TRUE,
);
$form['social_share']['social_share_replacements']['social_share_url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#description' => t('Select the value to use for the URL when sharing from this block.'),
'#default_value' => isset($settings['social_share_url']) ? $settings['social_share_url'] : '[current-page:url:absolute]',
'#required' => TRUE,
);
$form['social_share']['social_share_replacements']['social_share_description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('Select the value to use for the description when sharing from this block. <br><strong>Note:</strong> Most social networks ignore this value.'),
'#default_value' => isset($settings['social_share_description']) ? $settings['social_share_description'] : '',
);
$form['social_share']['social_share_replacements']['social_share_image'] = array(
'#type' => 'textfield',
'#title' => t('Image'),
'#description' => t('Select the value to use for the image when sharing from this block. <br><strong>Note:</strong> Very few social networks accept this value, most automatically grab images from the page markup. Facebook, for example, uses og:image metatag values for image options.'),
'#default_value' => isset($settings['social_share_image']) ? $settings['social_share_image'] : '',
);
$form['social_share']['social_share_replacements']['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('page', 'node'),
);
$networks = isset($settings['social_share_networks']) ? $settings['social_share_networks'] : array();
//$networks = variable_get('social_share_networks_' . $form['#node_type']->type, array());
$form['social_share']['social_share_networks'] = array(
'#type' => 'fieldset',
'#title' => t('Social Networks'),
'#description' => t('Specify the social network(s) to enable in this block.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$available_networks = social_share_available_networks();
foreach ($available_networks as $network) {
$form['social_share']['social_share_networks'][$network['machine_name']] = array(
'#type' => 'checkbox',
'#title' => $network['human_name'],
'#default_value' => ($networks[$network['machine_name']]) ? 1 : 0,
);
}
}
return $form;
}
/**
* Implements hook_block_save().
*/
function social_share_block_save($delta = '', $edit = array()) {
$settings = array();
foreach ($edit as $key => $val) {
if (substr($key, 0, 13) == 'social_share_') {
$settings[$key] = $val;
}
}
variable_set('social_share_block_settings', $settings);
}
/**
* Implements hook_block_view().
*/
function social_share_block_view($delta = '') {
$block = array();
if (user_access('access content') && variable_get('social_share_block', 0)) {
$block['subject'] = t('Social Share');
$block['content'] = theme('social_share_links', array('block' => TRUE));
}
// If the App ID is provided, attach the listener to this block.
// See: https://api.drupal.org/comment/49223#comment-49223.
$facebook_app_id = variable_get('social_share_fb_app_id', NULL);
if (!empty($facebook_app_id)) {
$selector = '.social-share-block .social-share-facebook';
$inline_js = _social_share_facebook_override_script($selector);
$block['content']['#attached'] = array(
'js' => array(
$inline_js => array('type' => 'inline')
)
);
}
return $block;
}
/**
* Implements hook_field_extra_fields().
*/
function social_share_field_extra_fields() {
$items = array();
$node_types = array_keys(node_type_get_types());
foreach ($node_types as $type) {
if (variable_get('social_share_enabled_' . $type, 0)) {
$items['node'][$type]['display'] = array(
'social_share' => array(
'label' => t('Social Share'),
'description' => t('Social Share Links'),
'weight' => 10,
),
);
}
}
return $items;
}
/**
* Implements hook_form_FORM_ID_alter() for the node type form.
*/
function social_share_form_node_type_form_alter(&$form, &$form_state) {
$form['social_share'] = array(
'#type' => 'fieldset',
'#title' => t('Social Share'),
'#weight' => 8,
'#collapsible' => TRUE,
'#group' => 'additional_settings',
'#attached' => array(
'js' => array(
'social-share' => drupal_get_path('module', 'social_share') . '/js/social_share.js',
),
),
);
$form['social_share']['social_share_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Social Share'),
'#description' => t('When checked, <em>Social Share</em> will be active for this node type (with settings as specified below)'),
'#default_value' => variable_get('social_share_enabled_' . $form['#node_type']->type, 0),
);
$form['social_share']['wrapper'] = array(
'#prefix' => '<div class="social-share-settings-wrapper">',
'#suffix' => '</div>',
'#tree' => FALSE,
);
$form['social_share']['wrapper']['social_share_theme'] = array(
'#type' => 'select',
'#title' => t('Display Format'),
'#description' => t('Select the display format for social share links.<br /><strong>NOTE:</strong> If you want to provide your own icons, or prefer text links, select none and add css as needed to your theme.'),
'#default_value' => variable_get('social_share_theme_' . $form['#node_type']->type, 'social_share_link'),
'#options' => array(
'social_share_link' => t('Unstyled Links'),
'social_share_small_icon' => t('Small Icons (16px)'),
'social_share_large_icon' => t('Large Icons (32px)'),
),
);
$form['social_share']['wrapper']['social_share_show_label'] = array(
'#type' => 'checkbox',
'#title' => t('Display share label'),
'#description' => t('If checked, the share label !config_page_link will be displayed before any enabled social network share links when displaying nodes of this type.',
array('!config_page_link' => l('as configured here', 'admin/config/content/social-share'))),
'#default_value' => variable_get('social_share_show_label_' . $form['#node_type']->type, '0'),
);
$replacements = variable_get('social_share_replacements_' . $form['#node_type']->type, array(
'[node:title]',
'[node:url:absolute]',
'[node:body]',
'',
));
$form['social_share']['wrapper']['social_share_replacements'] = array(
'#type' => 'fieldset',
'#title' => t('Replacements'),
'#description' => t('Specify the values to use for placeholders in the social share provider urls'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
);
$form['social_share']['wrapper']['social_share_replacements']['social_share_title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('Select the value to use for the title when sharing nodes of this type.'),
'#default_value' => variable_get('social_share_title_' . $form['#node_type']->type, '[node:title]'),
'#required' => TRUE,
);
$form['social_share']['wrapper']['social_share_replacements']['social_share_url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#description' => t('Select the value to use for the URL when sharing nodes of this type.'),
'#default_value' => variable_get('social_share_url_' . $form['#node_type']->type, '[node:url:absolute]'),
'#required' => TRUE,
);
$form['social_share']['wrapper']['social_share_replacements']['social_share_description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('Select the value to use for the description when sharing nodes of this type. <br><strong>Note:</strong> Most social networks ignore this value.'),
'#default_value' => variable_get('social_share_description_' . $form['#node_type']->type, '[node:body]'),
);
$form['social_share']['wrapper']['social_share_replacements']['social_share_image'] = array(
'#type' => 'textfield',
'#title' => t('Image'),
'#description' => t('Select the value to use for the image when sharing nodes of this type. <br><strong>Note:</strong> Very few social networks accept this value, most automatically grab images from the page markup. Facebook, for example, uses og:image metatag values for image options.'),
'#default_value' => variable_get('social_share_image_' . $form['#node_type']->type, ''),
);
$form['social_share']['wrapper']['social_share_replacements']['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('node'),
);
$networks = variable_get('social_share_networks_' . $form['#node_type']->type, array());
$form['social_share']['wrapper']['social_share_networks'] = array(
'#type' => 'fieldset',
'#title' => t('Social Networks'),
'#description' => t('Specify the social network(s) to enable for this node type.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$available_networks = social_share_available_networks();
foreach ($available_networks as $network) {
$form['social_share']['wrapper']['social_share_networks'][$network['machine_name']] = array(
'#type' => 'checkbox',
'#title' => $network['human_name'],
'#default_value' => (in_array($network['machine_name'], $networks)) ? 1 : 0,
);
}
}
/**
* Fetch all networks from the database.
* @return Array Configurations of all social networks
*/
function social_share_available_networks() {
$networks = &drupal_static(__FUNCTION__, array());
if (empty($networks)) {
$result = db_select('social_share_networks', 's')
->fields('s')
->orderBy('weight', 'ASC')
->orderBy('human_name', 'ASC')
->execute();
while ($network = $result->fetchAssoc()) {
$networks[] = $network;
}
}
return $networks;
}
/**
* Fetch a single network from the database.
* @param String $network Social network machine name
* @return Array Social network configuration
*/
function social_share_get_network($network) {
$available_networks = social_share_available_networks();
foreach($available_networks as $available_network) {
if ($available_network['machine_name'] == $network) {
return $available_network;
}
}
return FALSE;
}
/**
* Implements hook_node_type_update().
*/
function social_share_node_type_update($info) {
if (!empty($info->old_type) && $info->type != $info->old_type) {
$settings = array(
'social_share_enabled',
'social_share_title',
'social_share_url',
'social_share_description',
'social_share_image',
'social_share_networks',
'social_share_show_label',
'social_share_theme',
);
foreach ($settings as $setting) {
if ($var = variable_get($setting .'_'. $info->old_type, NULL)) {
variable_set($setting .'_'. $info->type, $var);
}
variable_del($setting .'_'. $info->old_type);
}
}
}
/**
* Implements hook_node_type_delete().
*/
function social_share_node_type_delete($info) {
$settings = array(
'social_share_enabled',
'social_share_title',
'social_share_url',
'social_share_description',
'social_share_image',
'social_share_networks',
'social_share_show_label',
'social_share_theme',
);
foreach ($settings as $setting) {
variable_del($setting .'_'. $info->type);
}
}
/**
* This listener overrides the default FB share url, using Share Dialog instead.
*
* @param string $selector
* A DOM selector for targeting the facebook link.
* @param string $abs_url
* The absolute URL you want to share via Facebook
*
* @return string
* A string of inline javascript for the override.
*/
function _social_share_facebook_override_script($selector, $abs_url = NULL) {
// If no URL is passed, we use the current URL (as determed by Javascript's
// document.URL).
if ($abs_url) {
$abs_url = '"' . $abs_url . '"';
}
else {
$abs_url = 'document.URL';
}
$listener = 'jQuery(document).ready(function() { '
. ' jQuery("' . $selector . '").click(function(event) { '
. ' event.preventDefault(); '
. ' FB.ui({method: "share", href: ' . $abs_url . '}, function(response){}); '
. ' }); '
. '}); ';
return $listener;
}