919ad3f08f8009c7cbd5db3a427332e9a4fb6ecc
[project/addthis.git] / includes / addthis.field.inc
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 * Field settings:
12 * - max_length: the maximum length for a varchar field.
13 *
14 * Instance settings:
15 * - AddThis::STYLE_KEY: The style of AddThis widget to render for this field.
16 */
17 function addthis_field_info() {
18 return array(
19 AddThis::FIELD_TYPE => array(
20 'label' => t('AddThis'),
21 'description' => t('This field stores addthis settings in the database.'),
22 'settings' => array('max_length' => 255),
23 'default_widget' => AddThis::WIDGET_TYPE,
24 'default_formatter' => AddThis::WIDGET_TYPE_DISABLED,
25 ),
26 );
27 }
28
29 /**
30 * Implements hook_field_is_empty().
31 */
32 function addthis_field_is_empty($item, $field) {
33 return empty($item['value']) && (string) $item['value'] !== '0';
34 }
35
36 /**
37 * Implements hook_field_formatter_info().
38 */
39 function addthis_field_formatter_info() {
40 $formatters = array();
41
42 // Create a formatter element for all the default style types we have.
43 foreach (AddThis::getInstance()->getDefaultFormatterTypes() as $key => $label) {
44 $formatters[$key] = array(
45 'label' => $label,
46 'field types' => array(AddThis::FIELD_TYPE),
47 );
48 }
49
50 return $formatters;
51 }
52
53 /**
54 * Implementation to retrieve formatters for a certain type of field.
55 *
56 * @todo make this a private function atleast by naming convention with a prefixed _ (underscore).
57 */
58 function _addthis_field_info_formatter_field_type($field_type = NULL) {
59 $formatters = field_info_formatter_types();
60 foreach ($formatters as $key => $formatter) {
61 if (!in_array((!isset($field_type) ? AddThis::FIELD_TYPE : $field_type), $formatter['field types'])) {
62 unset($formatters[$key]);
63 }
64 }
65 return $formatters;
66 }
67
68 /**
69 * Implements hook_field_formatter_view().
70 */
71 function addthis_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
72 return addthis_render_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
73 }
74
75 function addthis_render_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
76 // We have only one item to display, the share item.
77 // Get the markup for the share and add that to the $element[0] to display.
78 $element = array();
79 $display_type = $display['type'];
80
81 $options = array(
82 '#entity_type' => $entity_type,
83 '#entity' => $entity,
84 '#display' => $display,
85 );
86
87 $markup = AddThis::getInstance()->getDisplayMarkup($display_type, $options);
88 if (!isset($element[0])) {
89 $element[0] = $markup;
90 }
91 return $element;
92 }
93
94 /**
95 * Implementation of hook_field_prepare_view().
96 *
97 * This make sure we don't miss node that have no saved value on the AddThis
98 * field. We need the value to be able to render something.
99 */
100 function addthis_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
101 $dummy_value = "Don't mind this value. Is't a placeholder.";
102 foreach ($items as $key => $item) {
103 if (!isset($item[0]['value'])) {
104 $items[$key][0]['value'] = $dummy_value;
105 }
106 }
107 }
108
109
110 /**
111 * Implements hook_field_presave().
112 */
113 function addthis_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
114 $dummy_value = "Don't mind this value. Is't a placeholder.";
115 // We don't add items in the instance form therefore we check for a item
116 // if not there we (which is the case almost always) we add it. This item
117 // is need for the field_formatter in $items to attach a renderable item to.
118 if (count($items) == 0) {
119 $items += array(array('value' => $dummy_value));
120 }
121 else {
122 foreach ($items as $delta => $value) {
123 $items[$delta]['value'] = $dummy_value;
124 }
125 }
126 }
127
128 /**
129 * Implements hook_field_widget_info().
130 */
131 function addthis_field_widget_info() {
132 return array(
133 AddThis::WIDGET_TYPE => array(
134 'label' => t('AddThis button'),
135 'field types' => array(AddThis::FIELD_TYPE),
136 ),
137 );
138 }
139
140 /**
141 * Implements hook_field_widget_form().
142 */
143 function addthis_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
144
145
146 // The AddThis field is a placeholder field and has no widget settings.
147 // To suppress the form we set access to false here.
148 if ($instance['widget']['type'] == AddThis::WIDGET_TYPE) {
149 $main_widget = $element + array(
150 '#access' => FALSE,
151 );
152 $element['value'] = $main_widget;
153 }
154
155 return $element;
156 }
157
158 /**
159 * Implements hook_field_widget_error().
160 */
161 function addthis_field_widget_error($element, $error, $form, &$form_state) {
162 switch ($error['error']) {
163 default:
164 $error_element = $element[$element['#columns'][0]];
165 break;
166 }
167
168 form_error($error_element, $error['message']);
169 }