919ad3f08f8009c7cbd5db3a427332e9a4fb6ecc
5 * Field related hook implementations for the AddThis-module.
9 * Implements hook_field_info().
12 * - max_length: the maximum length for a varchar field.
15 * - AddThis::STYLE_KEY: The style of AddThis widget to render for this field.
17 function addthis_field_info() {
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
,
30 * Implements hook_field_is_empty().
32 function addthis_field_is_empty($item, $field) {
33 return empty($item['value']) && (string) $item['value'] !== '0';
37 * Implements hook_field_formatter_info().
39 function addthis_field_formatter_info() {
40 $formatters = array();
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(
46 'field types' => array(AddThis
::FIELD_TYPE
),
54 * Implementation to retrieve formatters for a certain type of field.
56 * @todo make this a private function atleast by naming convention with a prefixed _ (underscore).
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]);
69 * Implements hook_field_formatter_view().
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);
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.
79 $display_type = $display['type'];
82 '#entity_type' => $entity_type,
84 '#display' => $display,
87 $markup = AddThis
::getInstance()->getDisplayMarkup($display_type, $options);
88 if (!isset($element[0])) {
89 $element[0] = $markup;
95 * Implementation of hook_field_prepare_view().
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.
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;
111 * Implements hook_field_presave().
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));
122 foreach ($items as
$delta => $value) {
123 $items[$delta]['value'] = $dummy_value;
129 * Implements hook_field_widget_info().
131 function addthis_field_widget_info() {
133 AddThis
::WIDGET_TYPE
=> array(
134 'label' => t('AddThis button'),
135 'field types' => array(AddThis
::FIELD_TYPE
),
141 * Implements hook_field_widget_form().
143 function addthis_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
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(
152 $element['value'] = $main_widget;
159 * Implements hook_field_widget_error().
161 function addthis_field_widget_error($element, $error, $form, &$form_state) {
162 switch ($error['error']) {
164 $error_element = $element[$element['#columns'][0]];
168 form_error($error_element, $error['message']);