sharer_toolbar.inc 2.58 KB
Newer Older
1
2
3
4
5
6
7
8
<?php

/**
 * @file sharer_toolbar.inc
 * Helper functions
 */

/**
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * List all available sharers by invoking:
 * - hook_sharer_toolbar_available_sharer()
 * - hook_sharer_toolbar_available_sharer_alter()
 */
function _sharer_toolbar_get_available_sharers() {
  $available_sharers = module_invoke_all('sharer_toolbar_available_sharer');
  drupal_alter('sharer_toolbar_available_sharer', $available_sharers);

  // And return them
  return $available_sharers;
}

/**
 * List all available sharers,
23
24
25
26
27
 * ordered by config form weight if available
 */
function _sharer_toolbar_get_ordered_available_sharers() {
  $ordered_sharers = array();

28
29
  // Get all available sharers
  $available_sharers = _sharer_toolbar_get_available_sharers();
30
31
32

  // Generate ordered list if sharers order is already defined
  $sharers_order = variable_get('sharer_toolbar_sharers_order', array());
33
  $existing_sharers = array();
34
  if (!empty($sharers_order)) {
35
36

    // List already ordered sharers
37
    foreach ($sharers_order as $weight => $sid) {
38
39
40
41
42
43
44
45
46
47
48
49
50
      if (isset($available_sharers[$sid])) {
        $ordered_sharers[$weight] = $available_sharers[$sid];
        $existing_sharers[] = $sid;
      }
    }

    // Add new sharers
    $weight++;
    foreach ($available_sharers as $sid => $sharer) {
      if (!in_array($sid, $existing_sharers)) {
        $ordered_sharers[$weight] = $sharer;
        $weight++;
      }
51
52
53
54
55
56
57
58
59
60
61
62
63
    }
  }

  // Else, keep default order and index sharers array numerically
  else {
    $ordered_sharers = array_values($available_sharers);
  }


  // Return ordered sharers
  ksort($ordered_sharers);
  return $ordered_sharers;
}
64
65
66
67
68
69


/**
 * Replace tokens with correct value in pattern URL
 */
function _sharer_toolbar_generate_sharer_url(&$sharer_url, $variables) {
70
  global $base_root;
71
72
73

  // URL replacement
  $node_url = url('node/' . $variables['nid'], array(
74
    'language' => (object) array('language' => $variables['language']),
75
  ));
76
  $sharer_url = str_replace('[:shared_url:]', $base_root . $node_url, $sharer_url);
77
78
79
80
81
82
83
84

  // Title replacement
  $sharer_url = str_replace('[:shared_title:]', $variables['title'], $sharer_url);

  // Source replacement
  $sharer_url = str_replace('[:shared_source:]', variable_get('site_name', ''), $sharer_url);

  // Summary replacement
85
86
87
88
  $truncate_summary = '';
  if (isset($variables['node']->field_catchline) && !empty($variables['node']->field_catchline)) {
    $truncate_summary = truncate_utf8(drupal_html_to_text($variables['node']->field_catchline[LANGUAGE_NONE][0]['safe_value']), 200, TRUE, $add_ellipsis = TRUE);
  }
89
  $sharer_url = str_replace('[:shared_summary:]', $truncate_summary, $sharer_url);
90
}