Projet

Général

Profil

Révision 599a39cd

Ajouté par Assos Assos il y a environ 3 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/date/date_api/date_api_elements.inc
3 3
/**
4 4
 * @file
5 5
 * Date API elements themes and validation.
6
 *
6 7
 * This file is only included during the edit process to reduce memory usage.
7 8
 */
8 9

  
9 10
/**
10
 * Implements hook_element_info().
11
 * Wrapper for hook_element_info().
11 12
 *
12 13
 * Parameters for date form elements, designed to have sane defaults so any
13 14
 * or all can be omitted.
......
65 66
 */
66 67
function _date_api_element_info() {
67 68
  $date_base = array(
68
    '#input' => TRUE, '#tree' => TRUE,
69
    '#input' => TRUE,
70
    '#tree' => TRUE,
69 71
    '#date_timezone' => date_default_timezone(),
70 72
    '#date_flexible' => 0,
71 73
    '#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
......
88 90
    '#value_callback' => 'date_text_element_value_callback',
89 91
  ));
90 92
  $type['date_timezone'] = array(
91
    '#input' => TRUE, '#tree' => TRUE,
93
    '#input' => TRUE,
94
    '#tree' => TRUE,
92 95
    '#process' => array('date_timezone_element_process'),
93 96
    '#theme_wrappers' => array('date_text'),
94 97
    '#value_callback' => 'date_timezone_element_value_callback',
......
105 108
/**
106 109
 * Create a date object from a datetime string value.
107 110
 */
108
function date_default_date($element) {
111
function date_default_date(array $element) {
109 112
  $granularity = date_format_order($element['#date_format']);
110 113
  $default_value = $element['#default_value'];
111 114
  $format = DATE_FORMAT_DATETIME;
......
270 273
/**
271 274
 * Validation for timezone input.
272 275
 *
273
 *  Move the timezone value from the nested field back to the original field.
276
 * Move the timezone value from the nested field back to the original field.
274 277
 */
275 278
function date_timezone_validate($element, &$form_state) {
276 279
  if (date_hidden_element($element)) {
......
287 290
  $return = array('date' => '');
288 291
  $date = NULL;
289 292

  
290
  // Normal input from submitting the form element.
291
  // Check is_array() to skip the string input values created by Views pagers.
292
  // Those string values, if present, should be interpreted as empty input.
293
  if ($input != FALSE && is_array($input)) {
293
  // Normal input from submitting the form element. Check is_array() to skip
294
  // the string input values created by Views pagers. Those string values, if
295
  // present, should be interpreted as empty input.
296
  if ($input !== FALSE && is_array($input) && !is_null($input['date'])) {
294 297
    $return = $input;
295 298
    $date = date_text_input_date($element, $input);
296 299
  }
......
309 312
 *
310 313
 * Display all or part of a date in a single textfield.
311 314
 *
312
 * The exact parts displayed in the field are those in #date_granularity.
313
 * The display of each part comes from #date_format.
315
 * The exact parts displayed in the field are those in #date_granularity. The
316
 * display of each part comes from #date_format.
314 317
 */
315 318
function date_text_element_process($element, &$form_state, $form) {
316 319
  if (date_hidden_element($element)) {
......
326 329
  $now = date_example_date();
327 330
  $element['date']['#title'] = t('Date');
328 331
  $element['date']['#title_display'] = 'invisible';
329
  $element['date']['#description'] = ' ' . t('Format: @date', array(
330
    '@date' => date_format_date(date_example_date(), 'custom', $element['#date_format']
331
  )));
332
  $date_args = array(
333
    '@date' => date_format_date(date_example_date(), 'custom', $element['#date_format']),
334
  );
335
  $element['date']['#description'] = ' ' . t('Format: @date', $date_args);
332 336
  $element['date']['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
333 337

  
334 338
  // Make changes if instance is set to be rendered as a regular field.
......
338 342
    $element['date']['#required'] = $element['#required'];
339 343
  }
340 344

  
341
  // Keep the system from creating an error message for the sub-element.
342
  // We'll set our own message on the parent element.
343
  // $element['date']['#required'] = $element['#required'];
345
  // Keep the system from creating an error message for the sub-element. We'll
346
  // set our own message on the parent element.
344 347
  $element['date']['#theme'] = 'date_textfield_element';
345 348
  if (isset($element['#element_validate'])) {
346 349
    array_push($element['#element_validate'], 'date_text_validate');
......
363 366
/**
364 367
 * Validation for text input.
365 368
 *
366
 * When used as a Views widget, the validation step always gets triggered,
367
 * even with no form submission. Before form submission $element['#value']
368
 * contains a string, after submission it contains an array.
369
 * When used as a Views widget, the validation step always gets triggered, even
370
 * with no form submission. Before form submission $element['#value'] contains a
371
 * string, after submission it contains an array.
369 372
 */
370 373
function date_text_validate($element, &$form_state) {
371 374
  if (date_hidden_element($element)) {
......
388 391
  $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
389 392
  $date = date_text_input_date($element, $input);
390 393

  
391
  // If the field has errors, display them.
392
  // If something was input but there is no date, the date is invalid.
393
  // If the field is empty and required, set error message and return.
394
  // If the field has errors, display them. If something was input but there is
395
  // no date, the date is invalid. If the field is empty and required, set
396
  // error message and return.
394 397
  $error_field = implode('][', $element['#parents']);
395 398
  if (empty($date) || !empty($date->errors)) {
396 399
    if (is_object($date) && !empty($date->errors)) {
......
464 467
  );
465 468
  foreach ($granularity as $field) {
466 469
    if ($field != 'timezone') {
467
      $return[$field] = date_is_date($date) ? $date->format($formats[$field]) : '';
470
      if (date_is_date($date)) {
471
        $return[$field] = $date->format($formats[$field]);
472
      }
473
      else {
474
        $return = array();
475
      }
468 476
    }
469 477
  }
470 478
  return $return;
......
488 496
  $date = NULL;
489 497
  $granularity = date_format_order($element['#date_format']);
490 498

  
491
  if (is_array($element['#default_value'])) {
499
  if (array_key_exists('#default_value', $element) && is_array($element['#default_value'])) {
492 500
    $date = date_select_input_date($element, $element['#default_value']);
493 501
  }
494 502
  elseif (!empty($element['#default_value'])) {
......
536 544
/**
537 545
 * Creates form elements for one or more date parts.
538 546
 *
539
 * Get the order of date elements from the provided format.
540
 * If the format order omits any date parts in the granularity, alter the
541
 * granularity array to match the format, then flip the $order array
542
 * to get the position for each element. Then iterate through the
543
 * elements and create a sub-form for each part.
547
 * Get the order of date elements from the provided format. If the format order
548
 * omits any date parts in the granularity, alter the granularity array to match
549
 * the format, then flip the $order array to get the position for each element.
550
 * Then iterate through the elements and create a sub-form for each part.
544 551
 *
545 552
 * @param array $element
546 553
 *   The date element.
......
552 559
 * @return array
553 560
 *   The form array for the submitted date parts.
554 561
 */
555
function date_parts_element($element, $date, $format) {
562
function date_parts_element(array $element, $date, $format) {
556 563
  $granularity = date_format_order($format);
557 564
  $sub_element = array('#granularity' => $granularity);
558 565
  $order = array_flip($granularity);
559 566

  
560
  $hours_format  = strpos(strtolower($element['#date_format']), 'a') ? 'g' : 'G';
561
  $month_function  = strpos($element['#date_format'], 'F') !== FALSE ? 'date_month_names' : 'date_month_names_abbr';
567
  $hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g' : 'G';
568
  $month_function = strpos($element['#date_format'], 'F') !== FALSE ? 'date_month_names' : 'date_month_names_abbr';
562 569
  $count = 0;
563 570
  $increment = min(intval($element['#date_increment']), 1);
564 571

  
......
634 641
      if ($element['#date_label_position'] == 'within') {
635 642
        if (!empty($sub_element[$field]['#options']) && is_array($sub_element[$field]['#options'])) {
636 643
          $sub_element[$field]['#options'] = array(
637
            '-' . $label => '-' . $label) + $sub_element[$field]['#options'];
644
            '-' . $label => '-' . $label,
645
          ) + $sub_element[$field]['#options'];
638 646
        }
639 647
        if (empty($sub_element[$field]['#default_value'])) {
640 648
          $sub_element[$field]['#default_value'] = '-' . $label;
......
648 656
      $sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
649 657
      if ($element['#date_label_position'] == 'within') {
650 658
        $sub_element[$field]['#options'] = array(
651
          '' => '-' . $label) + $sub_element[$field]['#options'];
659
          '' => '-' . $label,
660
        ) + $sub_element[$field]['#options'];
652 661
      }
653 662
    }
654 663
  }
655 664

  
656
  // Views exposed filters are treated as submitted even if not,
657
  // so force the #default value in that case. Make sure we set
658
  // a default that is in the option list.
665
  // Views exposed filters are treated as submitted even if not, so force the
666
  // #default value in that case. Make sure we set a default that is in the
667
  // option list.
659 668
  if (!empty($element['#force_value'])) {
660 669
    $options = $sub_element[$field]['#options'];
661 670
    $default = !empty($sub_element[$field]['#default_value']) ? $sub_element[$field]['#default_value'] : array_shift($options);
......
686 695
/**
687 696
 * Validation function for date selector.
688 697
 *
689
 * When used as a Views widget, the validation step always gets triggered,
690
 * even with no form submission. Before form submission $element['#value']
691
 * contains a string, after submission it contains an array.
698
 * When used as a Views widget, the validation step always gets triggered, even
699
 * with no form submission. Before form submission $element['#value'] contains a
700
 * string, after submission it contains an array.
692 701
 */
693 702
function date_select_validate($element, &$form_state) {
694 703
  if (date_hidden_element($element)) {
......
755 764
 * Helper function for creating a date object out of user input.
756 765
 */
757 766
function date_select_input_date($element, $input) {
758

  
759 767
  // Was anything entered? If not, we have no date.
760 768
  if (!is_array($input)) {
761 769
    return NULL;
......
790 798
    }
791 799
    return $date;
792 800
  }
801

  
793 802
  return NULL;
794 803
}

Formats disponibles : Unified diff