|
eads@0
|
1 <?php |
|
eads@0
|
2 // $Id; |
|
eads@0
|
3 /** |
|
eads@0
|
4 * @file |
|
eads@0
|
5 * Defines a special textarea, with drag and drop media driven by Scald and |
|
eads@0
|
6 * dnd.module when rich text editing is enabled on the textarea via the |
|
eads@0
|
7 * WYSIWYG API. |
|
eads@0
|
8 */ |
|
eads@0
|
9 |
|
eads@0
|
10 /** |
|
eads@0
|
11 * Implementation of hook_theme(). |
|
eads@0
|
12 */ |
|
eads@0
|
13 function mee_theme() { |
|
eads@0
|
14 $theme = array( |
|
eads@0
|
15 'mee_textarea' => array( |
|
eads@0
|
16 'arguments' => array('element' => NULL), |
|
eads@0
|
17 ), |
|
eads@0
|
18 'mee_formatter_default' => array( |
|
eads@0
|
19 'arguments' => array('element' => NULL), |
|
eads@0
|
20 ), |
|
eads@0
|
21 ); |
|
tom@2
|
22 |
|
tom@2
|
23 $scald_config = variable_get('scald_config', 0); |
|
tom@2
|
24 foreach ($scald_config->contexts as $context => $details) { |
|
tom@2
|
25 $theme['mee_formatter_'. $context] = array( |
|
tom@2
|
26 'arguments' => array('element' => NULL), |
|
tom@2
|
27 'function' => 'theme_mee_context_formatter', |
|
tom@2
|
28 ); |
|
tom@2
|
29 } |
|
eads@0
|
30 return $theme; |
|
eads@0
|
31 } |
|
eads@0
|
32 |
|
eads@0
|
33 /** |
|
eads@0
|
34 * Implementation of hook_field_info(). |
|
eads@0
|
35 */ |
|
eads@0
|
36 function mee_field_info() { |
|
eads@0
|
37 return array( |
|
eads@0
|
38 'multimedia_editorial_element' => array( |
|
eads@0
|
39 'label' => t('Multimedia Editorial Element (MEE)'), |
|
eads@0
|
40 'description' => t('MEE combines Scald, WYSIWYG, and DnD to create a multimedia enabled text field.'), |
|
eads@0
|
41 ), |
|
eads@0
|
42 ); |
|
eads@0
|
43 } |
|
eads@0
|
44 |
|
eads@0
|
45 /** |
|
eads@0
|
46 * Implementation of hook_field_settings(). |
|
eads@0
|
47 */ |
|
eads@0
|
48 function mee_field_settings($op, $field) { |
|
eads@0
|
49 switch ($op) { |
|
eads@0
|
50 case 'form': |
|
eads@0
|
51 $form = array(); |
|
eads@0
|
52 $options = array(0 => t('Plain text'), 1 => t('Filtered text (user selects input format)')); |
|
eads@0
|
53 $form['mee_processing'] = array( |
|
eads@0
|
54 '#type' => 'radios', |
|
eads@0
|
55 '#title' => t('Text processing'), |
|
eads@0
|
56 '#default_value' => is_numeric($field['mee_processing']) ? $field['mee_processing'] : 1, |
|
eads@0
|
57 '#options' => $options, |
|
eads@0
|
58 '#description' => t('Filtered text, with a WYSIWYG editor defined on one or more input formats, is strongly recommended.'), |
|
eads@0
|
59 ); |
|
eads@0
|
60 // @TODO Ask Drupal about available libraries |
|
eads@0
|
61 $form['mee_dnd_callback_url'] = array( |
|
eads@0
|
62 '#type' => 'textfield', |
|
eads@0
|
63 '#title' => t('Library callback URL'), |
|
eads@0
|
64 '#default_value' => url($field['mee_dnd_callback_url']) ? $field['mee_dnd_callback_url'] : '', |
|
eads@0
|
65 '#description' => t('The absolute URL or relative path of a callback URL that provides proper JSON to the drag and drop library.'), |
|
eads@0
|
66 ); |
|
eads@0
|
67 return $form; |
|
eads@0
|
68 |
|
eads@0
|
69 case 'save': |
|
eads@0
|
70 return array('mee_processing', 'mee_dnd_callback_url'); |
|
eads@0
|
71 |
|
eads@0
|
72 case 'database columns': |
|
eads@0
|
73 $columns['value'] = array('type' => 'text', 'size' => 'big', 'not null' => FALSE, 'sortable' => TRUE); |
|
eads@0
|
74 $columns['dnd_callback_url'] = array('type' => 'text', 'size' => 'small', 'not null' => FALSE); |
|
eads@0
|
75 if (!empty($field['mee_processing'])) { |
|
eads@0
|
76 $columns['format'] = array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE); |
|
eads@0
|
77 } |
|
eads@0
|
78 return $columns; |
|
eads@0
|
79 |
|
eads@0
|
80 case 'views data': |
|
eads@0
|
81 return content_views_field_views_data($field); |
|
eads@0
|
82 } |
|
eads@0
|
83 } |
|
eads@0
|
84 |
|
eads@0
|
85 /** |
|
eads@0
|
86 * Implementation of hook_field(). |
|
eads@0
|
87 */ |
|
eads@0
|
88 function mee_field($op, &$node, $field, &$items, $teaser, $page) { |
|
eads@0
|
89 switch ($op) { |
|
tom@2
|
90 case 'presave': |
|
tom@2
|
91 // @@@TODO: parse the field & replace editor reps with SAS |
|
tom@2
|
92 break; // end 'submit' |
|
tom@2
|
93 |
|
tom@2
|
94 case 'insert': |
|
tom@2
|
95 // @@@TODO: register a new Atom based on this textarea |
|
tom@2
|
96 foreach ($items as $delta => $item) { |
|
tom@2
|
97 $temp_atom = new stdClass; |
|
tom@2
|
98 $temp_atom->type = 'composite'; |
|
tom@2
|
99 $temp_atom->provider = 'mee'; |
|
tom@2
|
100 $temp_atom->base_id = $node->nid . ':' . $delta; |
|
tom@2
|
101 $temp_atom->uid = $node->uid; |
|
tom@2
|
102 $temp_atom->title = $node->title; |
|
tom@2
|
103 $temp_atom->authors = array(scald_uid_to_aid($node->uid)); |
|
tom@2
|
104 $scald_included = scald_included($node->body); |
|
tom@2
|
105 $temp_atom->relationships = empty($scald_included) ? array() : array('includes' => $scald_included); |
|
tom@2
|
106 $sid = scald_register_atom($temp_atom); |
|
tom@2
|
107 } |
|
tom@2
|
108 break; // end 'insert' |
|
tom@2
|
109 |
|
tom@2
|
110 case 'update': |
|
tom@2
|
111 foreach ($items as $delta => $item) { |
|
tom@2
|
112 |
|
tom@2
|
113 } |
|
tom@2
|
114 // @@@TODO: Update the existing Atom |
|
tom@2
|
115 break; // end 'update' |
|
tom@2
|
116 |
|
tom@2
|
117 case 'delete': |
|
tom@2
|
118 // @@@TODO: Remove the Atom based on this instance of the field |
|
tom@2
|
119 break; // end 'delete' |
|
tom@2
|
120 |
|
eads@0
|
121 case 'sanitize': |
|
eads@0
|
122 foreach ($items as $delta => $item) { |
|
eads@0
|
123 if (!empty($field['mee_processing'])) { |
|
eads@0
|
124 $check = is_null($node) || (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW); |
|
eads@0
|
125 $text = isset($item['value']) ? check_markup($item['value'], $item['format'], $check) : ''; |
|
eads@0
|
126 } |
|
eads@0
|
127 else { |
|
eads@0
|
128 $text = check_plain($item['value']); |
|
eads@0
|
129 } |
|
eads@0
|
130 $items[$delta]['safe'] = $text; |
|
eads@0
|
131 } |
|
tom@2
|
132 break; // end 'sanitize' |
|
eads@0
|
133 } |
|
eads@0
|
134 } |
|
eads@0
|
135 |
|
eads@0
|
136 /** |
|
eads@0
|
137 * Implementation of hook_content_is_empty(). |
|
eads@0
|
138 */ |
|
eads@0
|
139 function mee_content_is_empty($item, $field) { |
|
eads@0
|
140 if (empty($item['value']) && (string)$item['value'] !== '0') { |
|
eads@0
|
141 return TRUE; |
|
eads@0
|
142 } |
|
eads@0
|
143 return FALSE; |
|
eads@0
|
144 } |
|
eads@0
|
145 |
|
eads@0
|
146 /** |
|
eads@0
|
147 * Implementation of hook_field_formatter_info(). |
|
eads@0
|
148 */ |
|
eads@0
|
149 function mee_field_formatter_info() { |
|
eads@0
|
150 $formatters = array( |
|
eads@0
|
151 'default' => array( |
|
eads@0
|
152 'label' => t('Filtered text'), |
|
eads@0
|
153 'field types' => array('multimedia_editorial_element'), |
|
eads@0
|
154 'multiple values' => CONTENT_HANDLE_CORE, |
|
eads@0
|
155 ), |
|
eads@0
|
156 'plain' => array( |
|
eads@0
|
157 'label' => t('Plain text'), |
|
eads@0
|
158 'field types' => array('multimedia_editorial_element'), |
|
eads@0
|
159 'multiple values' => CONTENT_HANDLE_CORE, |
|
eads@0
|
160 ), |
|
eads@0
|
161 ); |
|
eads@0
|
162 //@TODO generate context processor based field formatters |
|
eads@0
|
163 //foreach (scald_contexts() as $context) { |
|
eads@0
|
164 // $formatters[$context] = array( |
|
eads@0
|
165 // 'label' => t('Scald context processor: @context', array('@context' => $context), |
|
eads@0
|
166 // 'field types' => 'mee', |
|
eads@0
|
167 // ); |
|
eads@0
|
168 //} |
|
eads@0
|
169 return $formatters; |
|
eads@0
|
170 } |
|
eads@0
|
171 |
|
eads@0
|
172 function theme_mee_formatter_default($element) { |
|
eads@0
|
173 return $element['#item']['safe']; |
|
eads@0
|
174 } |
|
eads@0
|
175 |
|
eads@0
|
176 /** |
|
eads@0
|
177 * Theme function for 'plain' text field formatter. |
|
eads@0
|
178 */ |
|
eads@0
|
179 function theme_mee_formatter_plain($element) { |
|
eads@0
|
180 return strip_tags($element['#item']['safe']); |
|
eads@0
|
181 } |
|
eads@0
|
182 |
|
eads@0
|
183 function theme_mee_context_formatter($element) { |
|
eads@0
|
184 return 'foo'; |
|
eads@0
|
185 } |
|
eads@0
|
186 |
|
eads@0
|
187 /** |
|
eads@0
|
188 * Implementation of hook_widget_info(). |
|
eads@0
|
189 */ |
|
eads@0
|
190 function mee_widget_info() { |
|
eads@0
|
191 return array( |
|
eads@0
|
192 'mee_textarea' => array( |
|
eads@0
|
193 'label' => t('MEE Textarea'), |
|
eads@0
|
194 'field types' => array('multimedia_editorial_element'), |
|
eads@0
|
195 'multiple values' => CONTENT_HANDLE_CORE, |
|
eads@0
|
196 ), |
|
eads@0
|
197 ); |
|
eads@0
|
198 } |
|
eads@0
|
199 |
|
eads@0
|
200 /** |
|
eads@0
|
201 * Implementation of FAPI hook_elements(). |
|
eads@0
|
202 * |
|
eads@0
|
203 * Any FAPI callbacks needed for individual widgets can be declared here, |
|
eads@0
|
204 * and the element will be passed to those callbacks for processing. |
|
eads@0
|
205 * |
|
eads@0
|
206 * Drupal will automatically theme the element using a theme with |
|
eads@0
|
207 * the same name as the hook_elements key. |
|
eads@0
|
208 */ |
|
eads@0
|
209 function mee_elements() { |
|
eads@0
|
210 return array( |
|
eads@0
|
211 'mee_textarea' => array( |
|
eads@0
|
212 '#input' => TRUE, |
|
eads@0
|
213 '#columns' => array('value', 'format'), '#delta' => 0, |
|
eads@0
|
214 '#process' => array('mee_textarea_process', 'dnd_process_textarea'), |
|
eads@0
|
215 '#filter_value' => FILTER_FORMAT_DEFAULT, |
|
eads@0
|
216 ), |
|
eads@0
|
217 ); |
|
eads@0
|
218 } |
|
eads@0
|
219 |
|
eads@0
|
220 /** |
|
eads@0
|
221 * Implementation of hook_widget_settings(). |
|
eads@0
|
222 */ |
|
eads@0
|
223 function mee_widget_settings($op, $widget) { |
|
eads@0
|
224 switch ($op) { |
|
eads@0
|
225 case 'form': |
|
eads@0
|
226 $form = array(); |
|
eads@0
|
227 $rows = (isset($widget['rows']) && is_numeric($widget['rows'])) ? $widget['rows'] : 5; |
|
eads@0
|
228 $size = (isset($widget['size']) && is_numeric($widget['size'])) ? $widget['size'] : 60; |
|
eads@0
|
229 $form['rows'] = array( |
|
eads@0
|
230 '#type' => 'textfield', |
|
eads@0
|
231 '#title' => t('Rows'), |
|
eads@0
|
232 '#default_value' => $rows, |
|
eads@0
|
233 '#element_validate' => array('_mee_widget_settings_row_validate'), |
|
eads@0
|
234 '#required' => TRUE, |
|
eads@0
|
235 ); |
|
eads@0
|
236 $form['size'] = array('#type' => 'hidden', '#value' => $size); |
|
eads@0
|
237 return $form; |
|
eads@0
|
238 |
|
eads@0
|
239 case 'save': |
|
eads@0
|
240 return array('rows', 'size'); |
|
eads@0
|
241 } |
|
eads@0
|
242 } |
|
eads@0
|
243 |
|
eads@0
|
244 function _mee_widget_settings_row_validate($element, &$form_state) { |
|
eads@0
|
245 $value = $form_state['values']['rows']; |
|
eads@0
|
246 if (!is_numeric($value) || intval($value) != $value || $value <= 0) { |
|
eads@0
|
247 form_error($element, t('"Rows" must be a positive integer.')); |
|
eads@0
|
248 } |
|
eads@0
|
249 } |
|
eads@0
|
250 |
|
eads@0
|
251 function _mee_widget_settings_size_validate($element, &$form_state) { |
|
eads@0
|
252 $value = $form_state['values']['size']; |
|
eads@0
|
253 if (!is_numeric($value) || intval($value) != $value || $value <= 0) { |
|
eads@0
|
254 form_error($element, t('"Size" must be a positive integer.')); |
|
eads@0
|
255 } |
|
eads@0
|
256 } |
|
eads@0
|
257 |
|
eads@0
|
258 /** |
|
eads@0
|
259 * Implementation of hook_widget(). |
|
eads@0
|
260 * |
|
eads@0
|
261 * Attach a single form element to the form. It will be built out and |
|
eads@0
|
262 * validated in the callback(s) listed in hook_elements. We build it |
|
eads@0
|
263 * out in the callbacks rather than here in hook_widget so it can be |
|
eads@0
|
264 * plugged into any module that can provide it with valid |
|
eads@0
|
265 * $field information. |
|
eads@0
|
266 * |
|
eads@0
|
267 * Content module will set the weight, field name and delta values |
|
eads@0
|
268 * for each form element. This is a change from earlier CCK versions |
|
eads@0
|
269 * where the widget managed its own multiple values. |
|
eads@0
|
270 * |
|
eads@0
|
271 * If there are multiple values for this field, the content module will |
|
eads@0
|
272 * call this function as many times as needed. |
|
eads@0
|
273 * |
|
eads@0
|
274 * @param $form |
|
eads@0
|
275 * the entire form array, $form['#node'] holds node information |
|
eads@0
|
276 * @param $form_state |
|
eads@0
|
277 * the form_state, $form_state['values'][$field['field_name']] |
|
eads@0
|
278 * holds the field's form values. |
|
eads@0
|
279 * @param $field |
|
eads@0
|
280 * the field array |
|
eads@0
|
281 * @param $items |
|
eads@0
|
282 * array of default values for this field |
|
eads@0
|
283 * @param $delta |
|
eads@0
|
284 * the order of this item in the array of subelements (0, 1, 2, etc) |
|
eads@0
|
285 * |
|
eads@0
|
286 * @return |
|
eads@0
|
287 * the form item for a single element for this field |
|
eads@0
|
288 */ |
|
eads@0
|
289 function mee_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
|
eads@0
|
290 $element = array( |
|
eads@0
|
291 '#type' => $field['widget']['type'], |
|
eads@0
|
292 '#default_value' => isset($items[$delta]) ? $items[$delta] : '', |
|
eads@0
|
293 ); |
|
eads@0
|
294 return $element; |
|
eads@0
|
295 } |
|
eads@0
|
296 |
|
eads@0
|
297 /** |
|
eads@0
|
298 * Process an individual element. |
|
eads@0
|
299 * |
|
eads@0
|
300 * Build the form element. When creating a form using FAPI #process, |
|
eads@0
|
301 * note that $element['#value'] is already set. |
|
eads@0
|
302 * |
|
eads@0
|
303 * The $fields array is in $form['#field_info'][$element['#field_name']]. |
|
eads@0
|
304 */ |
|
eads@0
|
305 function mee_textarea_process($element, $edit, $form_state, $form) { |
|
eads@0
|
306 drupal_add_css(drupal_get_path('module', 'mee') .'/css/mee.css'); |
|
eads@0
|
307 |
|
eads@0
|
308 $field = $form['#field_info'][$element['#field_name']]; |
|
eads@0
|
309 $field_key = $element['#columns'][0]; |
|
eads@0
|
310 $element[$field_key] = array( |
|
eads@0
|
311 '#type' => 'textarea', |
|
eads@0
|
312 '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL, |
|
eads@0
|
313 '#rows' => !empty($field['widget']['rows']) ? $field['widget']['rows'] : 10, |
|
eads@0
|
314 '#weight' => 0, |
|
eads@0
|
315 // The following values were set by the content module and need |
|
eads@0
|
316 // to be passed down to the nested element. |
|
eads@0
|
317 '#title' => $element['#title'], |
|
eads@0
|
318 '#description' => $element['#description'], |
|
eads@0
|
319 '#required' => $element['#required'], |
|
eads@0
|
320 '#field_name' => $element['#field_name'], |
|
eads@0
|
321 '#type_name' => $element['#type_name'], |
|
eads@0
|
322 '#delta' => $element['#delta'], |
|
eads@0
|
323 '#columns' => $element['#columns'], |
|
eads@0
|
324 '#prefix' => '<div class="mee-wrap-editor-library">', |
|
eads@0
|
325 '#suffix' => '</div>', |
|
eads@0
|
326 '#dnd-enabled' => TRUE, |
|
eads@0
|
327 '#dnd-settings' => array( |
|
eads@0
|
328 'drop_selector' => '#'. $element['#id'] .' .drop', |
|
eads@0
|
329 'url' => $field['mee_dnd_callback_url'], |
|
eads@0
|
330 ), |
|
eads@0
|
331 ); |
|
eads@0
|
332 |
|
eads@0
|
333 if (!empty($field['mee_processing'])) { |
|
eads@0
|
334 $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format'; |
|
eads@0
|
335 $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT; |
|
eads@0
|
336 $parents = array_merge($element['#parents'] , array($filter_key)); |
|
eads@0
|
337 $element[$filter_key] = filter_form($format, 1, $parents); |
|
eads@0
|
338 } |
|
eads@0
|
339 |
|
eads@0
|
340 // Used so that hook_field('validate') knows where to flag an error. |
|
eads@0
|
341 $element['_error_element'] = array( |
|
eads@0
|
342 '#type' => 'value', |
|
eads@0
|
343 '#value' => implode('][', array_merge($element['#parents'], array($field_key))), |
|
eads@0
|
344 ); |
|
eads@0
|
345 |
|
eads@0
|
346 |
|
eads@0
|
347 return $element; |
|
eads@0
|
348 } |
|
eads@0
|
349 |
|
eads@0
|
350 /** |
|
eads@0
|
351 * FAPI theme for an individual text elements. |
|
eads@0
|
352 * |
|
eads@0
|
353 * The textfield or textarea is already rendered by the |
|
eads@0
|
354 * textfield or textarea themes and the html output |
|
eads@0
|
355 * lives in $element['#children']. Override this theme to |
|
eads@0
|
356 * make custom changes to the output. |
|
eads@0
|
357 * |
|
eads@0
|
358 * $element['#field_name'] contains the field name |
|
eads@0
|
359 * $element['#delta] is the position of this element in the group |
|
eads@0
|
360 */ |
|
eads@0
|
361 function theme_mee_textarea($element) { |
|
eads@0
|
362 return $element['#children']; |
|
eads@0
|
363 } |
|
tom@2
|
364 |
|
tom@2
|
365 |
|
tom@2
|
366 |
|
tom@2
|
367 |
|
tom@2
|
368 |
|
tom@2
|
369 |
|
tom@2
|
370 /******************************************************************************* |
|
tom@2
|
371 * SCALD HOOK IMPLEMENTATIONS |
|
tom@2
|
372 ******************************************************************************/ |
|
tom@2
|
373 |
|
tom@2
|
374 /** |
|
tom@2
|
375 * Implementation of hook_scald_provider(). |
|
tom@2
|
376 */ |
|
tom@2
|
377 function mee_scald_provider() { |
|
tom@2
|
378 return array( |
|
tom@2
|
379 'atoms' => array( |
|
tom@2
|
380 'composite' => t('The MEE CCK field.'), |
|
tom@2
|
381 ), |
|
tom@2
|
382 ); |
|
tom@2
|
383 } |
|
tom@2
|
384 |
|
tom@2
|
385 |
|
tom@2
|
386 |
|
tom@2
|
387 /** |
|
tom@2
|
388 * Implementation of hook_scald_register_atom(). |
|
tom@2
|
389 */ |
|
tom@2
|
390 function mee_scald_register_atom($atom, $mode) { |
|
tom@2
|
391 |
|
tom@2
|
392 } // end mee_scald_register_atom() |
|
tom@2
|
393 |
|
tom@2
|
394 |
|
tom@2
|
395 |
|
tom@2
|
396 /** |
|
tom@2
|
397 * Implementation of hook_scald_update_atom(). |
|
tom@2
|
398 */ |
|
tom@2
|
399 function mee_scald_update_atom($atom, $mode) { |
|
tom@2
|
400 |
|
tom@2
|
401 } // end mee_scald_update_atom() |
|
tom@2
|
402 |
|
tom@2
|
403 |
|
tom@2
|
404 |
|
tom@2
|
405 /** |
|
tom@2
|
406 * Implementation of hook_scald_unregister_atom(). |
|
tom@2
|
407 */ |
|
tom@2
|
408 function mee_scald_unregister_atom($atom, $mode) { |
|
tom@2
|
409 |
|
tom@2
|
410 } // end mee_scald_unregister_atom() |
|
tom@2
|
411 |
|
tom@2
|
412 |
|
tom@2
|
413 |
|
tom@2
|
414 /** |
|
tom@2
|
415 * Implementation of hook_scald_fetch(). |
|
tom@2
|
416 */ |
|
tom@2
|
417 function mee_scald_fetch(&$atom) { |
|
tom@2
|
418 list($nid, $delta) = split(':', $atom->base_id); |
|
tom@2
|
419 $node = node_load($nid); |
|
tom@2
|
420 $atom->title = $node->title . '(' . $delta . ')'; |
|
tom@2
|
421 $atom->description = $atom->title; // @@@TODO: Maybe make this a better description? |
|
tom@2
|
422 $atom->thumbnail_source = drupal_get_path('module', 'scald_composites') . '/assets/thumbnail_composite.png'; |
|
tom@2
|
423 } // end mee_scald_fetch() |
|
tom@2
|
424 |
|
tom@2
|
425 |
|
tom@2
|
426 |
|
tom@2
|
427 /** |
|
tom@2
|
428 * Implementation of hook_scald_prerender(). |
|
tom@2
|
429 */ |
|
tom@2
|
430 function mee_scald_prerender(&$atom, $mode) { |
|
tom@2
|
431 |
|
tom@2
|
432 } // end mee_scald_prerender() |
|
tom@2
|
433 |