Projet

Général

Profil

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

root / drupal7 / sites / all / modules / addthis / includes / addthis.field.inc @ 13755f8d

1
<?php
2

    
3
/**
4
 * @file
5
 * Field related hook implementations for the AddThis-module.
6
 */
7

    
8
/**
9
 * Implements hook_field_info().
10
 */
11
function addthis_field_info() {
12
  return array(
13
    AddThis::FIELD_TYPE => array(
14
      'label' => t('AddThis'),
15
      'description' => t('This field stores addthis settings in the database.'),
16
      'settings' => array('max_length' => 255),
17
      'default_widget' => AddThis::WIDGET_TYPE,
18
      'default_formatter' => AddThis::WIDGET_TYPE_DISABLED,
19
    ),
20
  );
21
}
22

    
23
/**
24
 * Implements hook_field_is_empty().
25
 */
26
function addthis_field_is_empty($item, $field) {
27
  return empty($item['value']) && (string) $item['value'] !== '0';
28
}
29

    
30
/**
31
 * Implements hook_field_formatter_info().
32
 *
33
 * Creates a formatter element for all the default formatter types.
34
 */
35
function addthis_field_formatter_info() {
36
  $formatters = array();
37

    
38
  foreach (AddThis::getInstance()->getDefaultFormatterTypes() as $key => $label) {
39
    $formatters[$key] = array(
40
      'label' => $label,
41
      'field types' => array(AddThis::FIELD_TYPE),
42
    );
43
  }
44

    
45
  return $formatters;
46
}
47

    
48
/**
49
 * Implementation to retrieve formatters for a given type of field.
50
 */
51
function _addthis_field_info_formatter_field_type($field_type = NULL) {
52
  $formatters = field_info_formatter_types();
53
  foreach ($formatters as $key => $formatter) {
54
    if (!in_array((!isset($field_type) ? AddThis::FIELD_TYPE : $field_type), $formatter['field types'])) {
55
      unset($formatters[$key]);
56
    }
57
  }
58
  return $formatters;
59
}
60

    
61
/**
62
 * Implements hook_field_formatter_view().
63
 */
64
function addthis_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
65
  return addthis_render_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
66
}
67

    
68
// There is only one item to display, the share item.
69
// Get the markup for the display type and add that to the $element[0] to display.
70
function addthis_render_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
71

    
72
  $element = array();
73
  $display_type = $display['type'];
74

    
75
  $options = array(
76
    '#entity_type' => $entity_type,
77
    '#entity' => $entity,
78
    '#display' => $display,
79
  );
80

    
81
  $markup = AddThis::getInstance()->getDisplayMarkup($display_type, $options);
82
  if (!isset($element[0])) {
83
    $element[0] = $markup;
84
  }
85
  return $element;
86
}
87

    
88
/**
89
 * Implements hook_field_prepare_view().
90
 *
91
 * Adds a dummy value in AddThis field for nodes that do not have any value for
92
 * the field to make rendering possible.
93
 */
94
function addthis_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
95
  foreach ($items as $key => $item) {
96
    if (!isset($item[0]['value'])) {
97
      $items[$key][0]['value'] = 'Dummy value';
98
    }
99
  }
100
}
101

    
102
/**
103
 * Implements hook_field_presave().
104
 *
105
 * Adds a dummy value to $items to make rendering possible.
106
 */
107
function addthis_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
108
  $dummy_value = 'Dummy value';
109

    
110
  if (count($items) == 0) {
111
    $items += array(array('value' => $dummy_value));
112
  }
113
  else {
114
    foreach ($items as $delta => $value) {
115
      $items[$delta]['value'] = $dummy_value;
116
    }
117
  }
118
}
119

    
120
/**
121
 * Implements hook_field_widget_info().
122
 */
123
function addthis_field_widget_info() {
124
  return array(
125
    AddThis::WIDGET_TYPE => array(
126
      'label' => t('AddThis button'),
127
      'field types' => array(AddThis::FIELD_TYPE),
128
    ),
129
  );
130
}
131

    
132
/**
133
 * Implements hook_field_widget_form().
134
 *
135
 * The AddThis field is a placeholder field and has no widget settings.
136
 * To suppress the form, #access is set to FALSE.
137
 */
138
function addthis_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
139

    
140
  if ($instance['widget']['type'] == AddThis::WIDGET_TYPE) {
141
    $main_widget = $element + array(
142
      '#access' => FALSE,
143
    );
144
    $element['value'] = $main_widget;
145
  }
146

    
147
  return $element;
148
}
149

    
150
/**
151
 * Implements hook_field_widget_error().
152
 */
153
function addthis_field_widget_error($element, $error, $form, &$form_state) {
154
  switch ($error['error']) {
155
    default:
156
      $error_element = $element[$element['#columns'][0]];
157
      break;
158
  }
159

    
160
  form_error($error_element, $error['message']);
161
}