Projet

Général

Profil

Paste
Télécharger (17,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.api.php @ 599a39cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Hooks provided by the Date module.
6
 */
7

    
8
/**
9
 * Alter the default value for a date argument.
10
 *
11
 * @param object $argument
12
 *   The argument object.
13
 * @param string $value
14
 *   The default value created by the argument handler.
15
 */
16
function hook_date_default_argument_alter(&$argument, &$value) {
17
  $style_options = $style_options = $argument->view->display_handler->get_option('style_options');
18
  if (!empty($style_options['track_date'])) {
19
    $default_date = date_now();
20
    $value = $default_date->format($argument->arg_format);
21
  }
22
}
23

    
24
/**
25
 * Alter the entity before formatting it.
26
 *
27
 * @param object $entity
28
 *   The entity object being viewed.
29
 * @param array $variables
30
 *   The variables passed to the formatter.
31
 *   - entity: The $entity object.
32
 *   - entity_type: The $entity_type.
33
 *   - field: The $field array.
34
 *   - instance: The $instance array.
35
 *   - langcode: The $langcode.
36
 *   - items: The $items array.
37
 *   - display: The $display array.
38
 *   - dates: The processed dates array, empty at this point.
39
 *   - attributes: The attributes array, empty at this point.
40
 *   - rdf_mapping: The RDF mapping array.
41
 *   - add_rdf: If module_exists('rdf').
42
 */
43
function hook_date_formatter_pre_view_alter(&$entity, array &$variables) {
44
  if (!empty($entity->view)) {
45
    $field = $variables['field'];
46
    $date_id = 'date_id_' . $field['field_name'];
47
    $date_delta = 'date_delta_' . $field['field_name'];
48
    $date_item = $entity->view->result[$entity->view->row_index];
49
    if (!empty($date_item->$date_id)) {
50
      $entity->date_id = 'date.' . $date_item->$date_id . '.' . $field['field_name'] . '.' . $date_item->$date_delta . '.0';
51
    }
52
  }
53
}
54

    
55
/**
56
 * Alter the dates array created by date_formatter_process().
57
 *
58
 * @param array $dates
59
 *   The $dates array created by the Date module.
60
 * @param array $context
61
 *   An associative array containing the following keys:
62
 *   - field: The $field array.
63
 *   - instance: The $instance array.
64
 *   - format: The string $format.
65
 *   - entity_type: The $entity_type.
66
 *   - entity: The $entity object.
67
 *   - langcode: The string $langcode.
68
 *   - item: The $item array.
69
 *   - display: The $display array.
70
 */
71
function hook_date_formatter_dates_alter(array &$dates, array $context) {
72
  $field = $context['field'];
73
  $instance = $context['instance'];
74
  $format = $context['format'];
75
  $entity_type = $context['entity_type'];
76
  $entity = $context['entity'];
77
  $date1 = $dates['value']['local']['object'];
78
  $date2 = $dates['value2']['local']['object'];
79

    
80
  $is_all_day = date_all_day_field($field, $instance, $date1, $date2);
81

    
82
  $all_day1 = '';
83
  $all_day2 = '';
84
  if ($format != 'format_interval' && $is_all_day) {
85
    $all_day1 = theme('date_all_day', array(
86
      'field' => $field,
87
      'instance' => $instance,
88
      'which' => 'date1',
89
      'date1' => $date1,
90
      'date2' => $date2,
91
      'format' => $format,
92
      'entity_type' => $entity_type,
93
      'entity' => $entity,
94
    ));
95
    $all_day2 = theme('date_all_day', array(
96
      'field' => $field,
97
      'instance' => $instance,
98
      'which' => 'date2',
99
      'date1' => $date1,
100
      'date2' => $date2,
101
      'format' => $format,
102
      'entity_type' => $entity_type,
103
      'entity' => $entity,
104
    ));
105
    $dates['value']['formatted_time'] = theme('date_all_day_label');
106
    $dates['value2']['formatted_time'] = theme('date_all_day_label');
107
    $dates['value']['formatted'] = $all_day1;
108
    $dates['value2']['formatted'] = $all_day2;
109
  }
110
}
111

    
112
/**
113
 * Alter the date_text element before the rest of the validation is run.
114
 *
115
 * @param array $element
116
 *   The $element array.
117
 * @param array $form_state
118
 *   A keyed array containing the current state of the form.
119
 * @param array $input
120
 *   The array of input values to be validated.
121
 */
122
function hook_date_text_pre_validate_alter(array &$element, array &$form_state, array &$input) {
123
  // Let Date module massage the format for all day values so they will pass
124
  // validation. The All day flag, if used, actually exists on the parent
125
  // element.
126
  date_all_day_value($element, $form_state);
127
}
128

    
129
/**
130
 * Alter the date_select element before the rest of the validation is run.
131
 *
132
 * @param array $element
133
 *   The $element array.
134
 * @param array $form_state
135
 *   A keyed array containing the current state of the form.
136
 * @param array $input
137
 *   The array of input values to be validated.
138
 */
139
function hook_date_select_pre_validate_alter(array &$element, array &$form_state, array &$input) {
140
  // Let Date module massage the format for all day values so they will pass
141
  // validation. The All day flag, if used, actually exists on the parent
142
  // element.
143
  date_all_day_value($element, $form_state);
144
}
145

    
146
/**
147
 * Alter the date_popup element before the rest of the validation is run.
148
 *
149
 * @param array $element
150
 *   The $element array.
151
 * @param array $form_state
152
 *   A keyed array containing the current state of the form.
153
 * @param array $input
154
 *   The array of input values to be validated.
155
 */
156
function hook_date_popup_pre_validate_alter(array &$element, array &$form_state, array &$input) {
157
  // Let Date module massage the format for all day values so they will pass
158
  // validation. The All day flag, if used, actually exists on the parent
159
  // element.
160
  date_all_day_value($element, $form_state);
161
}
162

    
163
/**
164
 * Alter the date_combo element before the rest of the validation is run.
165
 *
166
 * @param array $element
167
 *   The $element array.
168
 * @param array $form_state
169
 *   A keyed array containing the current state of the form.
170
 * @param array $context
171
 *   An associative array containing the following keys:
172
 *   - field: The $field array.
173
 *   - instance: The $instance array.
174
 *   - item: The $item array.
175
 *
176
 * @see date_combo_element_process()
177
 */
178
function hook_date_combo_pre_validate_alter(array &$element, array &$form_state, array $context) {
179
  if (!empty($context['item']['all_day'])) {
180
    $field = $context['field'];
181

    
182
    // If we have an all day flag on this date and the time is empty, change the
183
    // format to match the input value so we don't get validation errors.
184
    $element['#date_is_all_day'] = TRUE;
185
    $element['value']['#date_format'] = date_part_format('date', $element['value']['#date_format']);
186
    if (!empty($field['settings']['todate'])) {
187
      $element['value2']['#date_format'] = date_part_format('date', $element['value2']['#date_format']);
188
    }
189
  }
190
}
191

    
192
/**
193
 * Alter the local start date objects created by the date_combo validation.
194
 *
195
 * This is called before the objects are converted back to the database timezone
196
 * and stored.
197
 *
198
 * @param object $date
199
 *   The $date object.
200
 * @param array $form_state
201
 *   A keyed array containing the current state of the form.
202
 * @param array $context
203
 *   An associative array containing the following keys:
204
 *   - field: The $field array.
205
 *   - instance: The $instance array.
206
 *   - item: The $item array.
207
 *   - element: The $element array.
208
 */
209
function hook_date_combo_validate_date_start_alter(object &$date, array &$form_state, array $context) {
210
  // If this is an 'All day' value, set the time to midnight.
211
  if (!empty($context['element']['#date_is_all_day'])) {
212
    $date->setTime(0, 0, 0);
213
  }
214
}
215

    
216
/**
217
 * Alter the local end date objects created by the date_combo validation.
218
 *
219
 * This is called before the objects are converted back to the database timezone
220
 * and stored.
221
 *
222
 * @param object $date
223
 *   The $date object.
224
 * @param array $form_state
225
 *   A keyed array containing the current state of the form.
226
 * @param array $context
227
 *   An associative array containing the following keys:
228
 *   - field: The $field array.
229
 *   - instance: The $instance array.
230
 *   - item: The $item array.
231
 *   - element: The $element array.
232
 */
233
function hook_date_combo_validate_date_end_alter(object &$date, array &$form_state, array $context) {
234
  // If this is an 'All day' value, set the time to midnight.
235
  if (!empty($context['element']['#date_is_all_day'])) {
236
    $date->setTime(0, 0, 0);
237
  }
238
}
239

    
240
/**
241
 * Alter the date_text widget element.
242
 *
243
 * @param array $element
244
 *   An associative array containing the properties of the date_text element.
245
 * @param array $form_state
246
 *   A keyed array containing the current state of the form.
247
 * @param array $context
248
 *   An associative array containing the following keys:
249
 *   - form: Nested array of form elements that comprise the form.
250
 *
251
 * @see date_text_element_process()
252
 */
253
function hook_date_text_process_alter(array &$element, array &$form_state, array $context) {
254
  $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
255
  if ($all_day_id != '') {
256
    // All Day handling on text dates works only if the user leaves the time out
257
    // of the input value. There is no element to hide or show.
258
  }
259
}
260

    
261
/**
262
 * Alter the date_select widget element.
263
 *
264
 * @param array $element
265
 *   An associative array containing the properties of the date_select element.
266
 * @param array $form_state
267
 *   A keyed array containing the current state of the form.
268
 * @param array $context
269
 *   An associative array containing the following keys:
270
 *   - form: Nested array of form elements that comprise the form.
271
 *
272
 * @see date_select_element_process()
273
 */
274
function hook_date_select_process_alter(array &$element, array &$form_state, array $context) {
275
  // Hide or show the element in reaction to the all_day status for the element.
276
  $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
277
  if ($all_day_id != '') {
278
    foreach (array('hour', 'minute', 'second', 'ampm') as $field) {
279
      if (array_key_exists($field, $element)) {
280
        $element[$field]['#states'] = array(
281
          'visible' => array(
282
            'input[name="' . $all_day_id . '"]' => array('checked' => FALSE),
283
          ),
284
        );
285
      }
286
    }
287
  }
288
}
289

    
290
/**
291
 * Alter the date_popup widget element.
292
 *
293
 * @param array $element
294
 *   An associative array containing the properties of the date_popup element.
295
 * @param array $form_state
296
 *   A keyed array containing the current state of the form.
297
 * @param array $context
298
 *   An associative array containing the following keys:
299
 *   - form: Nested array of form elements that comprise the form.
300
 *
301
 * @see date_popup_element_process()
302
 */
303
function hook_date_popup_process_alter(array &$element, array &$form_state, array $context) {
304
  // Hide or show the element in reaction to the all_day status for the element.
305
  $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
306
  if ($all_day_id != '' && array_key_exists('time', $element)) {
307
    $element['time']['#states'] = array(
308
      'visible' => array(
309
        'input[name="' . $all_day_id . '"]' => array('checked' => FALSE),
310
      ),
311
    );
312
  }
313
}
314

    
315
/**
316
 * Alter the date_combo element after the Date module is finished with it.
317
 *
318
 * @param array $element
319
 *   The $element array.
320
 * @param array $form_state
321
 *   A keyed array containing the current state of the form.
322
 * @param array $context
323
 *   An associative array containing the following keys:
324
 *   - field: The $field array.
325
 *   - instance: The $instance array.
326
 *   - form: Nested array of form elements that comprise the form.
327
 */
328
function hook_date_combo_process_alter(array &$element, array &$form_state, array $context) {
329
  $field = $context['field'];
330
  $instance = $context['instance'];
331
  $field_name = $element['#field_name'];
332
  $delta = $element['#delta'];
333

    
334
  // Add a date repeat form element, if needed.
335
  // We delayed until this point so we don't bother adding it to hidden fields.
336
  if (date_is_repeat_field($field, $instance)) {
337
    $item = $element['#value'];
338
    $element['rrule'] = array(
339
      '#type' => 'date_repeat_rrule',
340
      '#theme_wrappers' => array('date_repeat_rrule'),
341
      '#default_value' => isset($item['rrule']) ? $item['rrule'] : '',
342
      '#date_timezone' => $element['#date_timezone'],
343
      '#date_format'      => date_limit_format(date_input_format($element, $field, $instance), $field['settings']['granularity']),
344
      '#date_text_parts'  => (array) $instance['widget']['settings']['text_parts'],
345
      '#date_increment'   => $instance['widget']['settings']['increment'],
346
      '#date_year_range'  => $instance['widget']['settings']['year_range'],
347
      '#date_label_position' => $instance['widget']['settings']['label_position'],
348
      '#date_repeat_widget' => str_replace('_repeat', '', $instance['widget']['type']),
349
      '#date_repeat_collapsed' => $instance['widget']['settings']['repeat_collapsed'],
350
      '#date_flexible' => 0,
351
      '#weight' => $instance['widget']['weight'] + .4,
352
    );
353
  }
354
}
355

    
356
/**
357
 * Alter the date_timezone widget element.
358
 *
359
 * @param array $element
360
 *   An associative array containing the properties of the date_select element.
361
 * @param array $form_state
362
 *   A keyed array containing the current state of the form.
363
 * @param array $context
364
 *   An associative array containing the following keys:
365
 *   - form: Nested array of form elements that comprise the form.
366
 *
367
 * @see date_timezone_element_process()
368
 */
369
function hook_date_timezone_process_alter(array &$element, array &$form_state, array $context) {
370
  // @todo.
371
}
372

    
373
/**
374
 * Alter the date_year_range widget element.
375
 *
376
 * @param array $element
377
 *   An associative array containing the properties of the date_select element.
378
 * @param array $form_state
379
 *   A keyed array containing the current state of the form.
380
 * @param array $context
381
 *   An associative array containing the following keys:
382
 *   - form: Nested array of form elements that comprise the form.
383
 *
384
 * @see date_year_range_element_process()
385
 */
386
function hook_date_year_range_process_alter(array &$element, array &$form_state, array $context) {
387
  // @todo.
388
}
389

    
390
/**
391
 * Alter a date field settings form.
392
 *
393
 * @param array $form
394
 *   Nested array of form elements that comprise the form.
395
 * @param array $context
396
 *   An associative array containing the following keys:
397
 *   - field: The $field array.
398
 *   - instance: The $instance array.
399
 *   - has_data: The value of $has_data.
400
 *
401
 * @see hook_field_settings_form()
402
 */
403
function hook_date_field_settings_form_alter(array &$form, array $context) {
404
  $field = $context['field'];
405
  $instance = $context['instance'];
406
  $has_data = $context['has_data'];
407

    
408
  $form['repeat'] = array(
409
    '#type' => 'select',
410
    '#title' => t('Repeating date'),
411
    '#default_value' => $field['settings']['repeat'],
412
    '#options' => array(0 => t('No'), 1 => t('Yes')),
413
    '#attributes' => array('class' => array('container-inline')),
414
    '#description' => t("Repeating dates use an 'Unlimited' number of values. Instead of the 'Add more' button, they include a form to select when and how often the date should repeat."),
415
    '#disabled' => $has_data,
416
  );
417
}
418

    
419
/**
420
 * Alter a date field instance settings form.
421
 *
422
 * @param array $form
423
 *   Nested array of form elements that comprise the form.
424
 * @param array $context
425
 *   An associative array containing the following keys:
426
 *   - field: The $field array.
427
 *   - instance: The $instance array.
428
 *
429
 * @see hook_field_instance_settings_form()
430
 */
431
function hook_date_field_instance_settings_form_alter(array &$form, array $context) {
432
  $field = $context['field'];
433
  $instance = $context['instance'];
434
  $form['new_setting'] = array(
435
    '#type' => 'textfield',
436
    '#default_value' => '',
437
    '#title' => t('My new setting'),
438
  );
439
}
440

    
441
/**
442
 * Alter a date field widget settings form.
443
 *
444
 * @param array $form
445
 *   Nested array of form elements that comprise the form.
446
 * @param array $context
447
 *   An associative array containing the following keys:
448
 *   - field: The $field array.
449
 *   - instance: The $instance array.
450
 *
451
 * @see hook_field_widget_settings_form()
452
 */
453
function hook_date_field_widget_settings_form_alter(array &$form, array $context) {
454
  $field = $context['field'];
455
  $instance = $context['instance'];
456
  $form['new_setting'] = array(
457
    '#type' => 'textfield',
458
    '#default_value' => '',
459
    '#title' => t('My new setting'),
460
  );
461
}
462

    
463
/**
464
 * Alter a date field formatter settings form.
465
 *
466
 * @param array $form
467
 *   Nested array of form elements that comprise the form.
468
 * @param array $form_state
469
 *   A keyed array containing the current state of the form.
470
 * @param array $context
471
 *   An associative array containing the following keys:
472
 *   - field: The $field array.
473
 *   - instance: The $instance array.
474
 *   - view_mode: The formatter view mode.
475
 *
476
 * @see hook_field_formatter_settings_form()
477
 */
478
function hook_date_field_formatter_settings_form_alter(array &$form, array &$form_state, array $context) {
479
  $field = $context['field'];
480
  $instance = $context['instance'];
481
  $view_mode = $context['view_mode'];
482
  $display = $instance['display'][$view_mode];
483
  $formatter = $display['type'];
484
  if ($formatter == 'date_default') {
485
    $form['show_repeat_rule'] = array(
486
      '#title' => t('Repeat rule:'),
487
      '#type' => 'select',
488
      '#options' => array(
489
        'show' => t('Show repeat rule'),
490
        'hide' => t('Hide repeat rule'),
491
      ),
492
      '#default_value' => $settings['show_repeat_rule'],
493
      '#access' => $field['settings']['repeat'],
494
      '#weight' => 5,
495
    );
496
  }
497
}
498

    
499
/**
500
 * Alter a date field formatter settings summary.
501
 *
502
 * @param array $summary
503
 *   An array of strings to be concatenated into a short summary of the
504
 *   formatter settings.
505
 * @param array $context
506
 *   An associative array containing the following keys:
507
 *   - field: The $field array.
508
 *   - instance: The $instance array.
509
 *   - view_mode: The formatter view mode.
510
 *
511
 * @see hook_field_formatter_settings_summary()
512
 */
513
function hook_date_field_formatter_settings_summary_alter(array &$summary, array $context) {
514
  $field = $context['field'];
515
  $instance = $context['instance'];
516
  $view_mode = $context['view_mode'];
517
  $display = $instance['display'][$view_mode];
518
  $formatter = $display['type'];
519
  $settings = $display['settings'];
520
  if (isset($settings['show_repeat_rule']) && !empty($field['settings']['repeat'])) {
521
    if ($settings['show_repeat_rule'] == 'show') {
522
      $summary[] = t('Show repeat rule');
523
    }
524
    else {
525
      $summary[] = t('Hide repeat rule');
526
    }
527
  }
528
}