-
Notifications
You must be signed in to change notification settings - Fork 1
/
yashare_counters.fields.inc
211 lines (190 loc) · 5.92 KB
/
yashare_counters.fields.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* @file
* Field for Yandex.Share buttons with counters.
*/
/**
* Implements hook_field_info().
*/
function yashare_counters_field_info() {
return array(
'yashare_counters_buttons' => array(
'label' => t('Yandex.Share with Counters'),
'description' => t('Displays Yandex.Share buttons with counters.'),
'default_widget' => 'yashare_counters_default_widget',
'default_formatter' => 'yashare_counters_default_formatter',
),
);
}
/**
* Implements hook_field_is_empty().
*/
function yashare_counters_field_is_empty($item, $field) {
return FALSE;
}
/**
* Implements hook_field_formatter_info().
*/
function yashare_counters_field_formatter_info() {
return array(
'yashare_counters_default_formatter' => array(
'label' => t('Default'),
'field types' => array('yashare_counters_buttons'),
'settings' => array(
'image_field' => 'field_image',
'image_style' => 'thumbnail',
),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function yashare_counters_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$settings = $display['settings'];
$element = array();
$link = NULL;
$title = NULL;
$description = NULL;
$image = NULL;
if ($entity_type == 'node') {
$link = 'node/' . $entity->nid;
$title = $entity->title;
}
elseif ($entity_type == 'taxonomy_term') {
$link = 'taxonomy/term/' . $entity->tid;
$title = $entity->name;
}
if ($images = field_get_items($entity_type, $entity, $settings['image_field'])) {
if ($settings['image_style']) {
$image = image_style_url($settings['image_style'], $images[0]['uri']);
}
else {
$image = file_create_url($images[0]['uri']);
}
}
switch ($display['type']) {
case 'yashare_counters_default_formatter':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => yashare_counters_block_content($link, $title, $description, $image),
'#attached' => array(
'js' => array(
'https://yastatic.net/share2/share.js' => array(
'type' => 'external',
// See https://www.drupal.org/node/1664602.
'async' => 'async',
),
),
),
);
}
break;
}
return $element;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function yashare_counters_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$elements = array();
$options = array();
$fields_info = field_info_instances($instance['entity_type'], $instance['bundle']);
if (!empty($fields_info)) {
foreach ($fields_info as $field_name => $value) {
$field_info = field_info_field($field_name);
if (in_array($field_info['type'], array('image'))) {
$options[$field_name] = $fields_info[$field_name]['label'];
}
}
}
else {
// Fallback for using image field in views.
$fields_info = field_info_field_map();
foreach ($fields_info as $field_name => $field_info) {
if (in_array($field_info['type'], array('image'))) {
$options[$field_name] = $field_name;
}
}
}
$elements['image_field'] = array(
'#type' => 'select',
'#title' => t('Image field'),
'#description' => t('Choose image field to use as image to share.'),
'#default_value' => $settings['image_field'],
'#empty_option' => t('None'),
'#options' => $options,
);
$options = image_style_options(FALSE);
$elements['image_style'] = array(
'#type' => 'select',
'#title' => t('Image style'),
'#default_value' => $settings['image_style'],
'#empty_option' => t('None (original image)'),
'#options' => $options,
'#states' => array(
'invisible' => array(
':input[name="fields[' . $instance['field_name'] . '][settings_edit_form][settings][image_field]"]' => array('value' => ''),
),
),
);
return $elements;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function yashare_counters_field_formatter_settings_summary($field, $instance, $view_mode) {
$settings = $instance['display'][$view_mode]['settings'];
$output = array();
$field_instances = field_info_instances($instance['entity_type'], $instance['bundle']);
if (isset($field_instances[$settings['image_field']])) {
$output[] = t('Image field to share: @field', array('@field' => $field_instances[$settings['image_field']]['label']));
$image_styles = image_style_options(FALSE);
// Unset possible 'No defined styles' option.
unset($image_styles['']);
// Styles could be lost because of enabled/disabled modules that define
// their styles in code.
if (isset($image_styles[$settings['image_style']])) {
$output[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
}
else {
$output[] = t('Image style: @style', array('@style' => t('Original image')));
}
}
else {
$output[] = t('Image field to share: @field', array('@field' => t('None')));
}
return implode('<br />', $output);
}
/**
* Implements hook_field_widget_info().
*/
function yashare_counters_field_widget_info() {
return array(
'yashare_counters_default_widget' => array(
'label' => t('Default'),
'field types' => array('yashare_counters_buttons'),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function yashare_counters_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$widget = $element;
$widget['#delta'] = $delta;
switch ($instance['widget']['type']) {
case 'yashare_counters_default_widget':
$widget += array(
'#type' => 'value',
'#value' => '',
);
break;
}
$element['smth'] = $widget;
return $element;
}