Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / date.api.php @ 87dbc3bf

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, &$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(&$dates, $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
    $all_day2 = theme('date_all_day', array(
95
      'field' => $field,
96
      'instance' => $instance,
97
      'which' => 'date2',
98
      'date1' => $date1,
99
      'date2' => $date2,
100
      'format' => $format,
101
      'entity_type' => $entity_type,
102
      'entity' => $entity));
103
    $dates['value']['formatted_time'] = theme('date_all_day_label');
104
    $dates['value2']['formatted_time'] = theme('date_all_day_label');
105
    $dates['value']['formatted'] = $all_day1;
106
    $dates['value2']['formatted'] = $all_day2;
107
  }
108
}
109

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

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

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

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

    
179
    $field = $context['field'];
180

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

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

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

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

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

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

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

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

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

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

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

    
407
  $form['repeat'] = array(
408
    '#type' => 'select',
409
    '#title' => t('Repeating date'),
410
    '#default_value' => $field['settings']['repeat'],
411
    '#options' => array(0 => t('No'), 1 => t('Yes')),
412
    '#attributes' => array('class' => array('container-inline')),
413
    '#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."),
414
    '#disabled' => $has_data,
415
  );
416
}
417

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

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

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

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