Project

General

Profile

Paste
Download (9.12 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / addthis / addthis.module @ 2c8c2b87

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

    
84
  return $menu_items;
85
}
86

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

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

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

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

    
146
/**
147
 * Implements hook_page_build().
148
 */
149
function addthis_page_build(&$page) {
150
  // Include the addthis.js as a library on every page. Widget js will be loaded
151
  // according to the settings.
152
  $addthis_js_path = drupal_get_path('module', 'addthis') . '/addthis.js';
153
  $page['content']['#attached']['js'][$addthis_js_path] = array(
154
    'type' => 'file',
155
    'scope' => 'footer',
156
  );
157

    
158
  // Include the widget js when we include on all but admin pages.
159
  if (!path_is_admin(current_path()) && AddThis::getInstance()->getWidgetJsInclude() == AddThis::WIDGET_JS_INCLUDE_PAGE) {
160
    $manager = AddThisScriptManager::getInstance();
161
    $manager->attachJsToElement($page['content']);
162
  }
163
}
164

    
165
/**
166
 * Implements hook_preprocess() for theme_addthis.
167
 */
168
function template_preprocess_addthis(&$variables) {
169
  if (isset($variables[0]) && count($variables) == 3) {
170
    $variables['#display'] = $variables[0];
171
    unset($variables[0]);
172
  }
173
}
174

    
175
function theme_addthis($variables) {
176
  $markup = AddThis::getInstance()->getDisplayMarkup($variables['#display']);
177
  return render($markup);
178
}
179

    
180
function theme_addthis_wrapper($variables) {
181
  $element = $variables['addthis_wrapper'];
182
  $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
183
  $children = element_children($element);
184

    
185
  if (count($children) > 0) {
186
    foreach ($children as $child) {
187
      $output .= render($element[$child]);
188
    }
189
  }
190

    
191
  $output .= '</' . $element['#tag'] . ">\n";
192
  return $output;
193
}
194

    
195
/**
196
 * Theme the elements that are created in the AddThis module.
197
 *
198
 * This is created with hook_addthis_element.
199
 */
200
function theme_addthis_element($variables) {
201
  $element = $variables['addthis_element'];
202

    
203
  if (!isset($element['#value'])) {
204
    return '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . " />\n";
205
  }
206

    
207
  $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
208
  if (isset($element['#value_prefix'])) {
209
    $output .= $element['#value_prefix'];
210
  }
211
  $output .= $element['#value'];
212
  if (isset($element['#value_suffix'])) {
213
    $output .= $element['#value_suffix'];
214
  }
215
  $output .= '</' . $element['#tag'] . ">\n";
216
  return $output;
217
}
218

    
219
/**
220
 * Create a addthis settings form.
221
 */
222
function _addthis_settings_form($form = array(), &$form_state = array(), $display_type = NULL, $display_settings = array()) {
223

    
224
  // Check display setting to be not null.
225
  if (!isset($display_settings) || (isset($display_settings) && !is_array($display_settings))) {
226
    $display_settings = array();
227
  }
228

    
229
  // Toggle variables.
230
  $no_js = FALSE;
231

    
232
  // Default display type if not set in the parameters.
233
  $display_type = (isset($display_type)) ? $display_type : 'addthis_disabled';
234
  // Load setting type from the form_state.
235
  if (isset($form_state['values']['addthis_settings']['type'])) {
236
    $display_type = $form_state['values']['addthis_settings']['type'];
237
  }
238

    
239
  // Container used as the replace for ajax.
240
  $form['#type'] = 'container';
241
  $form['#attributes'] = array();
242
  $form['#id'] = 'addthis_settings';
243

    
244
  // The list of formatters.
245
  $formatter_options = AddThis::getInstance()->getDisplayTypes();
246
  $form['type'] = array(
247
    '#type' => 'select',
248
    '#title' => t('Formatter for @title', array('@title' => 'AddThis block')),
249
    '#title_display' => 'invisible',
250
    '#options' => $formatter_options,
251
    '#default_value' => $display_type,
252
    '#parents' => array('addthis_settings', 'type'),
253
    '#attributes' => array('class' => array('addthis-display-type')),
254
    '#ajax' => array(
255
      'callback' => '_addthis_settings_form_submit_callback',
256
      'wrapper' => 'addthis_settings',
257
      'name' => 'switch_display',
258
      'event' => 'change',
259
    )
260
  );
261

    
262
  if ($no_js) {
263
    $form['choose'] = array(
264
      '#type' => 'submit',
265
      '#name' => 'Switch display',
266
      '#value' => 'Switch display',
267
      '#op' => 'switch_display',
268
      '#id' => 'switch-display',
269
      '#submit' => array('_addthis_settings_form_submit'),
270
    );
271
  }
272

    
273
  // Formatter settings.
274

    
275
  // Check the currently selected formatter, and merge persisted values for
276
  // formatter settings.
277
  $formatter_type = $display_type;
278

    
279
  // Get the settings if set.
280
  $settings = $display_settings;
281
  $formatter_settings = field_info_formatter_settings($formatter_type);
282
  $settings += $formatter_settings;
283

    
284
  $field = array();
285
  $instance = array();
286
  $view_mode = 'block';
287

    
288
  $instance['display'][$view_mode]['type'] = $formatter_type;
289
  $formatter = field_info_formatter_types($formatter_type);
290
  $instance['display'][$view_mode]['module'] = $formatter['module'];
291
  $instance['display'][$view_mode]['settings'] = $settings;
292

    
293
  $settings_form = array();
294
  $function = $formatter['module'] . '_field_formatter_settings_form';
295
  if (function_exists($function)) {
296
    $settings_form = $function($field, $instance, $view_mode, $form, $form_state);
297
    $settings_form['#tree'] = TRUE;
298
    $settings_form['#parents'] = array('addthis_settings', 'settings');
299
  }
300

    
301
  $form['settings'] = $settings_form;
302

    
303
  return $form;
304
}
305

    
306
function _addthis_settings_form_submit($form, &$form_state) {
307
  $form_state['rebuild'] = TRUE;
308
}
309

    
310
function _addthis_settings_form_submit_callback($form, &$form_state) {
311
  $form_state['rebuild'] = TRUE;
312
  return $form['addthis_settings']['form'];
313
}