-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_annotator.module
More file actions
261 lines (251 loc) · 7.93 KB
/
image_annotator.module
File metadata and controls
261 lines (251 loc) · 7.93 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
// $Id$
/**
* @file
*
*/
/**
* Implements hook_field_info()
*/
function image_annotator_field_info() {
return array(
'image_annotator' => array(
'label' => t('Image Annotator'),
'default_formatter' => 'image_annotator',
'default_widget' => 'image_annotator_hidden',
'instance_settings' => array(
'image_field' => '',
),
)
);
}
/**
* Implements hook_field_is_empty()
*/
function image_annotator_field_is_empty($item, $field) {
return empty($item['value']);
}
/**
* Implements hook_field_formatter_info()
*/
function image_annotator_field_formatter_info() {
return array(
'image_annotator' => array(
'label' => t('Default'),
'field types' => array('image_annotator'),
),
);
}
/**
* Implements hook_field_formatter_view()
*/
function image_annotator_field_formatter_view ($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
// support field collections
static $highest_delta = -1;
//figure out if this field is part of a collection, if so, figure out which delta it has.
$settings = $instance['settings'];
$value = array();
if (isset($items[0]['value'])) {
$value = drupal_json_decode('[' . $items[0]['value'] . ']');
$value = reset($value);
}
$number = empty($value) ? $highest_delta + 1 : $value['delta'];
if ($number > $highest_delta) {
$highest_delta = $number;
}
$settings = $instance['settings'];
$element = array();
if (!empty($items)) {
foreach ($items as $delta => $item) {
$text = '';
if (isset($item['value'])) {
$text = $item['value'];
}
$element[$delta]['image_annotator_coordinates'] = array(
'#type' => 'hidden',
'#value' => $text,
'#attributes' => array(
'id' => $field['field_name'] . '__' . $langcode . '__' . $delta . '__coordinates',
'class' => array('image-annotator-pointers'),
)
);
$rel = 'field-name-' . drupal_clean_css_identifier($settings['image_field']);
$element[$delta]['image_annotator_button'] = array(
'#type' => 'hidden',
'#value' => '',
'#attributes' => array(
'class' => array('image-annotator-button'),
'rel' => $rel,
'id' => $field['field_name'] . '__' . $langcode . '__' . $number . '__button',
),
);
}
$number++;
$js_settings = array(
'imageAnnotator' => array($field['field_name'] => array('imagefield' => $rel, 'edit' => FALSE)),
);
drupal_add_js($js_settings, 'setting');
libraries_load('json2');
drupal_add_js(drupal_get_path('module', 'image_annotator') . '/image_annotator.js');
drupal_add_css(drupal_get_path('module', 'image_annotator') . '/image_annotator.css');
}
return $element;
}
/**
* Implements hook_field_instance_settings_form()
*/
function image_annotator_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$fields = field_info_fields();
$options = drupal_map_assoc(array_keys(array_filter($fields, 'image_annotator_is_image_field')));
$form['image_field'] = array(
'#type' => 'select',
'#options' => $options,
'#required' => TRUE,
'#default_value' => $settings['image_field'],
'#description' => t('Choose the image field you want to annotate'),
'#title' => t('Image field'),
);
return $form;
}
/**
* Implements hook_form_FORM_ID_alter()
*/
function image_annotator_form_field_ui_field_edit_form_alter(&$form) {
if ($form['#field']['type'] == 'image_annotator') {
$form['field']['cardinality_markup'] = array(
'#type' => 'item',
'#markup' => 1,
'#description' => t('This field stores all coordinates in one field.'),
'#title' => $form['field']['cardinality']['#title'],
);
$form['field']['cardinality'] = array(
'#type' => 'value',
'#value' => 1,
);
}
}
/**
* Implements hook_field_widget_info()
*/
function image_annotator_field_widget_info() {
return array(
'image_annotator_hidden' => array(
'label' => t('Pointer'),
'field types' => array('image_annotator'),
),
'image_annotator_rectangle' => array(
'label' => t('Rectangle'),
'field types' => array('image_annotator'),
),
'image_annotator_pointer_draggable' => array(
'label' => t('Draggable Pointer'),
'field types' => array('image_annotator'),
),
'image_annotator_rectangle_draggable' => array(
'label' => t('Draggable & Resizable Rectangle'),
'field types' => array('image_annotator'),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function image_annotator_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// support field collections
$class = '';
$draggable = FALSE;
$resizable = FALSE;
switch ($instance['widget']['type']) {
case 'image_annotator_hidden':
$class = 'image-annotator-hidden';
break;
case 'image_annotator_rectangle':
$class = 'image-annotator-rectangle';
break;
case 'image_annotator_pointer_draggable':
$class = 'image-annotator-pointer-draggable';
$draggable = TRUE;
$element['#attached']['library'][] = array(
'system',
'ui.draggable'
);
break;
case 'image_annotator_rectangle_draggable':
$class = 'image-annotator-rectangle-draggable';
$draggable = TRUE;
$resizable = TRUE;
$element['#attached']['library'][] = array(
'system',
'ui.draggable',
);
$element['#attached']['library'][] = array(
'system',
'ui.resizable',
);
break;
}
static $highest_delta = -1;
//figure out if this field is part of a collection, if so, figure out which delta it has.
$settings = $instance['settings'];
$value = isset($items[$delta]['value']) ? reset(drupal_json_decode('[' . $items[$delta]['value'] . ']')) : array();
$number = empty($value) ? $highest_delta + 1 : $value['delta'];
if ($number > $highest_delta) {
$highest_delta = $number;
}
$element['value'] = array(
'#type' => 'hidden',
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
'#attributes' => array(
'id' => $field['field_name'] . '__' . $langcode . '__' . $number . '__coordinates',
'class' => array('image-annotator-pointers'),
)
);
// Field collection
if (isset($element['#entity_type']) && $element['#entity_type'] == 'field_collection_item') {
$parents = (empty($form['#field_parents']) ? '' : implode('-', $form['#field_parents']) . '-');
}
// Normal
else {
$parents = (empty($form['#parents']) ? '' : implode('-', $form['#parents']) . '-');
}
$rel = 'edit-' . drupal_clean_css_identifier($parents . $settings['image_field']);
$element['image_annotator_button'] = array(
'#type' => 'button',
'#value' => t('Place on image'),
'#attributes' => array(
'class' => array('image-annotator-button', $class),
'rel' => $rel,
'id' => $field['field_name'] . '__' . $langcode . '__' . $number . '__button',
),
);
$element['#attributes']['class'][] = 'field-annotator-' . $field['id'] . '-' . $instance['id'];
$element['#attached']['js'] = array(
drupal_get_path('module', 'image_annotator') . '/image_annotator.js',
array(
'data' => array(
'imageAnnotator' => array(
$field['field_name'] => array(
'imagefield' => $rel,
'edit' => TRUE,
'draggable' => $draggable,
'resizable' => $resizable,
)
),
),
'type' => 'setting',
),
);
$element['#attached']['css'] = array(
drupal_get_path('module', 'image_annotator') . '/image_annotator.css',
);
$element['#pre_render'] = array('image_annotator_load_json2');
return $element;
}
function image_annotator_load_json2($element) {
libraries_load('json2');
return $element;
}
function image_annotator_is_image_field($field) {
return ($field['type'] == 'image') || ($field['type'] == 'fixed_field');
}