Projet

Général

Profil

Paste
Télécharger (4,91 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / addthis / addthis_displays / addthis_displays.field.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines field implementations.
6
 */
7

    
8
/**
9
 * Implements hook_field_formatter_info().
10
 */
11
function addthis_displays_field_formatter_info() {
12
  $formatters = array();
13

    
14
  $formatters['addthis_basic_toolbox'] = array(
15
    'label' => t('Basic Toolbox'),
16
    'field types' => array(AddThis::FIELD_TYPE),
17
    'settings' => array(
18
      'share_services' => 'facebook,twitter',
19
      'buttons_size' => 'addthis_16x16_style',
20
      'counter_orientation' => 'horizontal',
21
      'extra_css' => '',
22
    ),
23
  );
24
  $formatters['addthis_basic_button'] = array(
25
    'label' => t('Basic Button'),
26
    'field types' => array(AddThis::FIELD_TYPE),
27
    'settings' => array(
28
      'button_size' => 'small',
29
      'extra_css' => '',
30
    ),
31
  );
32
  return $formatters;
33
}
34

    
35
/**
36
 * Implementss hook_field_formatter_settings_form().
37
 */
38
function addthis_displays_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
39
  $display = $instance['display'][$view_mode];
40
  $settings = $display['settings'];
41
  $element = array();
42

    
43
  switch ($display['type']) {
44
    case 'addthis_basic_toolbox':
45
      $element['share_services'] = array(
46
        '#title' => t('Services'),
47
        '#type' => 'textfield',
48
        '#size' => 80,
49
        '#default_value' => $settings['share_services'],
50
        '#required' => TRUE,
51
        '#element_validate' => array('_addthis_display_element_validate_services'),
52
        '#description' => t('Specify the names of the sharing services and seperate them with a , (comma). <a href="http://www.addthis.com/services/list">The names on this list are valid.</a>'),
53
      );
54
      $element['buttons_size'] = array(
55
        '#title' => t('Buttons size'),
56
        '#type' => 'select',
57
        '#default_value' => $settings['buttons_size'],
58
        '#options' => array(
59
          AddThis::CSS_16x16 => t('Small (16x16)'),
60
          AddThis::CSS_32x32 => t('Big (32x32)'),
61
        ),
62
      );
63
      $element['counter_orientation'] = array(
64
        '#title' => t('Counter orientation'),
65
        '#description' => t('Specify the way service counters are oriented.'),
66
        '#type' => 'select',
67
        '#default_value' => $settings['counter_orientation'],
68
        '#options' => array(
69
          'horizontal' => t('Horizontal'),
70
          'vertical' => t('Vertical'),
71
        )
72
      );
73
      $element['extra_css'] = array(
74
        '#title' => t('Extra CSS declaration'),
75
        '#type' => 'textfield',
76
        '#size' => 40,
77
        '#default_value' => $settings['extra_css'],
78
        '#description' => t('Specify extra CSS classes to apply to the toolbox'),
79
      );
80
      break;
81
    case 'addthis_basic_button':
82
      $element['button_size'] = array(
83
        '#title' => t('Image'),
84
        '#type' => 'select',
85
        '#default_value' => $settings['button_size'],
86
        '#options' => array(
87
          'small' => t('Small'),
88
          'big' => t('Big'),
89
        ),
90
      );
91
      $element['extra_css'] = array(
92
        '#title' => t('Extra CSS declaration'),
93
        '#type' => 'textfield',
94
        '#size' => 40,
95
        '#default_value' => $settings['extra_css'],
96
        '#description' => t('Specify extra CSS classes to apply to the button'),
97
      );
98
      break;
99
  }
100

    
101
  return $element;
102
}
103

    
104
/**
105
 * Validate the services text field to validate if services are correct.
106
 */
107
function _addthis_display_element_validate_services($element, &$form_state, $form) {
108
  $bad = FALSE;
109

    
110
  $services = trim($element['#value']);
111
  $services = str_replace(' ', '', $services);
112

    
113
  if (!preg_match('/^[a-z\_\,]+$/', $services)) {
114
    $bad = TRUE;
115
  }
116
  // @todo Validate the service names against AddThis.com. Give a notice when there are bad names.
117

    
118
  // Return error.
119
  if ($bad) {
120
    form_error($element, t('The declared services are incorrect or nonexistent.'));
121
  }
122
}
123

    
124
/**
125
 * Implements hook_field_formatter_settings_summary().
126
 */
127
function addthis_displays_field_formatter_settings_summary($field, $instance, $view_mode) {
128
  $view = $instance['display'][$view_mode];
129
  $info = '';
130
  switch ($view['type']) {
131
    case 'addthis_basic_toolbox':
132
      $settings = $view['settings'];
133

    
134
      $services = trim($settings['share_services']);
135
      $services = str_replace(' ', '', $services);
136
      $services = '<b>' . implode(', ', explode(',', $services)) . '</b>';
137

    
138
      $info =  t('Toolbox with the following services: !services', array('!services' => $services));
139
      break;
140

    
141
    case 'addthis_basic_button':
142
      $settings = $view['settings'];
143

    
144
      $size = '<b>' . $settings['button_size'] . '</b>';
145
      $info = t('Basic button size: !size', array('!size' => $size));
146
      break;
147
  }
148

    
149
  return $info;
150
}
151

    
152
/**
153
 * Implements hook_field_formatter_view().
154
 */
155
function addthis_displays_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
156
  // We use the helper function addthis_render_formatter_view with literally all the arguments.
157
  return addthis_render_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
158
}