Projet

Général

Profil

Paste
Télécharger (8,26 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / addthis / addthis.module @ 87dbc3bf

1
<?php
2
/**
3
 * @file
4
 * Hook implementations for the AddThis-module. Most of the logic is defined
5
 * in a separate AddThis-class to keep the .module-file clean.
6
 */
7

    
8
if (module_exists('block')) {
9
  module_load_include('inc', 'addthis', 'includes/addthis.block');
10
}
11
module_load_include('inc', 'addthis', 'includes/addthis.field');
12

    
13
/**
14
 * Implements hook_hook_info().
15
 *
16
 * Define hook_addthis_display_markup in the addthis group so that
17
 * $module.addthis.inc files will be included. See addthis.addthis.inc for
18
 * examples of the hook implementation.
19
 */
20
function addthis_hook_info() {
21
  $hooks['addthis_display_markup'] = array(
22
    'group' => 'addthis',
23
  );
24
  return $hooks;
25
}
26

    
27
/**
28
 * Implements hook_help().
29
 */
30
function addthis_help($path, $arg) {
31
  switch ($path) {
32
    case 'admin/help#addthis':
33
      $output = '<h3>' . t('About') . '</h3>';
34
      $output .= '<p>';
35
      $output .= t('The AddThis module defines AddThis field type for the Field module. A AddThis field may contain a button, toolbox, sharecount or customized sharing tool using <a href="http://addthis.com/">AddThis.com</a>.');
36
      $output .= '</p>';
37
      return $output;
38
  }
39
}
40

    
41
/**
42
 * Implements hook_filter_format_update().
43
 */
44
function addthis_filter_format_update($format) {
45
  field_cache_clear();
46
}
47

    
48
/**
49
 * Implements hook_filter_format_disable().
50
 */
51
function addthis_filter_format_disable($format) {
52
  field_cache_clear();
53
}
54

    
55

    
56
/**
57
 * Implements hook_menu().
58
 */
59
function addthis_menu() {
60
  $menu_items['admin/config/user-interface/addthis'] = array(
61
    'title' => 'AddThis',
62
    'description' => 'Configure AddThis settings.',
63
    'page callback' => 'drupal_get_form',
64
    'page arguments' => array('addthis_admin_settings_form'),
65
    'access arguments' => array(AddThis::PERMISSION_ADMINISTER_ADDTHIS),
66
    'type' => MENU_NORMAL_ITEM,
67
    'file' => AddThis::ADMIN_INCLUDE_FILE,
68
    'weight' => 0,
69
  );
70
  $menu_items['admin/config/user-interface/addthis/basic'] = array(
71
    'title' => t('Basic settings'),
72
    'type' => MENU_DEFAULT_LOCAL_TASK,
73
  );
74
  $menu_items['admin/config/user-interface/addthis/advanced'] = array(
75
    'title' => t('Advanced settings'),
76
    'type' => MENU_LOCAL_TASK,
77
    'page callback' => 'drupal_get_form',
78
    'page arguments' => array('addthis_admin_settings_advanced_form'),
79
    'access arguments' => array(AddThis::PERMISSION_ADMINISTER_ADDTHIS),
80
    'file' => AddThis::ADMIN_INCLUDE_FILE,
81
    'weight' => 10
82
  );
83
  return $menu_items;
84
}
85

    
86
/**
87
 * Implements hook_permission().
88
 */
89
function addthis_permission() {
90
  return array(
91
    AddThis::PERMISSION_ADMINISTER_ADDTHIS => array(
92
      'title' => t('Administer AddThis'),
93
      'description' => t('Perform maintenance tasks for AddThis.'),
94
    ),
95
    AddThis::PERMISSION_ADMINISTER_ADVANCED_ADDTHIS => array(
96
      'title' => t('Administer advanced AddThis'),
97
      'description' => t('Perform advanced maintenance tasks for AddThis.'),
98
    ),
99
  );
100
}
101

    
102
/**
103
 * Implements hook_form_FORM_ID_alter().
104
 *
105
 * Hide the instance fieldset because there aren't any values per instance.
106
 */
107
function addthis_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
108
  if ($form['#field']['type'] == AddThis::FIELD_TYPE && $form['#field']['type'] == AddThis::MODULE_NAME) {
109
    $form['field']['#access'] = FALSE;
110
    $form['instance']['required']['#access'] = FALSE;
111
    $form['instance']['description']['#access'] = FALSE;
112
    $form['instance']['default_value_widget']['#access'] = FALSE;
113
  }
114
}
115

    
116
/**
117
 * Implements hook_rdf_namespaces().
118
 */
119
function addthis_rdf_namespaces() {
120
  if (AddThis::getInstance()->isFacebookLikeCountSupportEnabled()) {
121
    return array(
122
      'fb' => 'http://www.facebook.com/2008/fbml',
123
    );
124
  }
125
  return array();
126
}
127

    
128
/**
129
 * Implements hook_theme().
130
 */
131
function addthis_theme($existing, $type, $theme, $path) {
132
  return array(
133
    'addthis_wrapper' => array(
134
      'render element' => 'addthis_wrapper',
135
    ),
136
    'addthis_element' => array(
137
      'render element' => 'addthis_element',
138
    ),
139
    'addthis' => array(
140
      'render element' => 'addthis',
141
    ),
142
  );
143
}
144

    
145
/**
146
 * Implements hook_preprocess() for theme_addthis.
147
 */
148
function template_preprocess_addthis(&$variables) {
149
  if (isset($variables[0]) && count($variables) == 3) {
150
    $variables['#display'] = $variables[0];
151
    unset($variables[0]);
152
  }
153
}
154

    
155
function theme_addthis($variables) {
156
  $markup = AddThis::getInstance()->getDisplayMarkup($variables['#display']);
157
  return render($markup);
158
}
159

    
160
function theme_addthis_wrapper($variables) {
161
  $element = $variables['addthis_wrapper'];
162
  $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
163
  $children = element_children($element);
164

    
165
  if (count($children) > 0) {
166
    foreach ($children as $child) {
167
      $output .= render($element[$child]);
168
    }
169
  }
170

    
171
  $output .= '</' . $element['#tag'] . ">\n";
172
  return $output;
173
}
174

    
175
/**
176
 * Theme the elements that are created in the AddThis module.
177
 *
178
 * This is created with hook_addthis_element.
179
 */
180
function theme_addthis_element($variables) {
181
  $element = $variables['addthis_element'];
182

    
183
  if (!isset($element['#value'])) {
184
    return '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . " />\n";
185
  }
186

    
187
  $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
188
  if (isset($element['#value_prefix'])) {
189
    $output .= $element['#value_prefix'];
190
  }
191
  $output .= $element['#value'];
192
  if (isset($element['#value_suffix'])) {
193
    $output .= $element['#value_suffix'];
194
  }
195
  $output .= '</' . $element['#tag'] . ">\n";
196
  return $output;
197
}
198

    
199
/**
200
 * Create a addthis settings form.
201
 */
202
function _addthis_settings_form($form = array(), &$form_state, $display_type = NULL, $display_settings = NULL) {
203
  // Toggle variables
204
  $no_js = FALSE;
205

    
206

    
207
  // Default display type if not set in the parameters.
208
  $display_type = (isset($display_type)) ? $display_type : 'addthis_disabled';
209
  // Load setting type from the form_state.
210
  if (isset($form_state['values']['addthis_settings']['type'])) {
211
    $display_type = $form_state['values']['addthis_settings']['type'];
212
  }
213

    
214
  // Container used as the replace for ajax.
215
  $form['#type'] = 'container';
216
  $form['#attributes'] = array();
217
  $form['#id'] = 'addthis_settings';
218

    
219
  // The list of formatters.
220
  $formatter_options = AddThis::getInstance()->getDisplayTypes();
221
  $form['type'] = array(
222
    '#type' => 'select',
223
    '#title' => t('Formatter for @title', array('@title' => 'AddThis block')),
224
    '#title_display' => 'invisible',
225
    '#options' => $formatter_options,
226
    '#default_value' => $display_type,
227
    '#parents' => array('addthis_settings', 'type'),
228
    '#attributes' => array('class' => array('addthis-display-type')),
229
    '#ajax' => array(
230
      'callback' => '_addthis_settings_form_submit_callback',
231
      'wrapper' => 'addthis_settings',
232
      'name' => 'switch_display',
233
      'event' => 'change',
234
    )
235
  );
236

    
237
  if ($no_js) {
238
    $form['choose'] = array(
239
      '#type' => 'submit',
240
      '#name' => 'Switch display',
241
      '#value' => 'Switch display',
242
      '#op' => 'switch_display',
243
      '#id' => 'switch-display',
244
      '#submit' => array('_addthis_settings_form_submit'),
245
    );
246
  }
247

    
248
  // Formatter settings.
249

    
250
  // Check the currently selected formatter, and merge persisted values for
251
  // formatter settings.
252
  $formatter_type = $display_type;
253

    
254
  // Get the settings if set.
255
  $settings = $display_settings;
256
  $formatter_settings = field_info_formatter_settings($formatter_type);
257
  $settings += $formatter_settings;
258

    
259
  $field = array();
260
  $instance = array();
261
  $view_mode = 'block';
262

    
263
  $instance['display'][$view_mode]['type'] = $formatter_type;
264
  $formatter = field_info_formatter_types($formatter_type);
265
  $instance['display'][$view_mode]['module'] = $formatter['module'];
266
  $instance['display'][$view_mode]['settings'] = $settings;
267

    
268
  $settings_form = array();
269
  $function = $formatter['module'] . '_field_formatter_settings_form';
270
  if (function_exists($function)) {
271
    $settings_form = $function($field, $instance, $view_mode, $form, $form_state);
272
    $settings_form['#tree'] = TRUE;
273
    $settings_form['#parents'] = array('addthis_settings', 'settings');
274
  }
275

    
276
  $form['settings'] = $settings_form;
277

    
278
  return $form;
279
}
280

    
281
function _addthis_settings_form_submit($form, &$form_state) {
282
  $form_state['rebuild'] = TRUE;
283
}
284

    
285
function _addthis_settings_form_submit_callback($form, &$form_state) {
286
  $form_state['rebuild'] = TRUE;
287
  return $form['addthis_settings']['form'];
288
}