Projet

Général

Profil

Révision 9d13637e

Ajouté par Assos Assos il y a plus de 9 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/addressfield/addressfield.module
181 181
    ctools_plugin_load_function('addressfield', 'format', $handler, 'format callback');
182 182
  }
183 183

  
184
  _addressfield_process_format_form($format, $format['#address'], $format['#required']);
184
  _addressfield_process_format_form($format, $format['#address']);
185 185
  return $format;
186 186
}
187 187

  
188
function _addressfield_process_format_form(&$format, $address, $required) {
188
function _addressfield_process_format_form(&$format, $address) {
189 189
  foreach (element_children($format) as $key) {
190 190
    $child = &$format[$key];
191 191

  
......
211 211
        }
212 212
      }
213 213

  
214
      if (!$required) {
215
        unset($child['#required']);
216
      }
217

  
218 214
      if (isset($address[$key])) {
219 215
        $child['#default_value'] = $address[$key];
220 216
      }
221 217
    }
222 218

  
223 219
    // Recurse through the element's children if it has any.
224
    _addressfield_process_format_form($child, $address, $required);
220
    _addressfield_process_format_form($child, $address);
225 221
  }
226 222
}
227 223

  
......
365 361

  
366 362
/**
367 363
 * Returns an array of default values for the addressfield form elements.
368
 */
369
function addressfield_default_values($available_countries = NULL) {
370
  if (!isset($available_countries)) {
371
    $available_countries = _addressfield_country_options_list();
372
  }
373

  
374
  // Use the default country of the site if possible.
375
  $default_country = variable_get('site_default_country', NULL);
376

  
377
  // If the default country is undefined or not in the list of available countries,
378
  // just fallback to the first country in the list.
379
  if (!$default_country || !isset($available_countries[$default_country])) {
364
 *
365
 * @param $field
366
 *   The field array.
367
 * @param $instance
368
 *   The instance array.
369
 * @param $address
370
 *   The current address values, if known. Allows for per-country defaults.
371
 *
372
 * @return
373
 *   An array of default values.
374
 */
375
function addressfield_default_values($field, $instance, array $address = array()) {
376
  $available_countries = _addressfield_country_options_list($field, $instance);
377
  $default_country = $instance['widget']['settings']['default_country'];
378
  // If the default country is not in the list of available countries,
379
  // fallback to the first country in the list.
380
  if ($default_country && !isset($available_countries[$default_country])) {
380 381
    $default_country = key($available_countries);
381 382
  }
382 383

  
383
  return array(
384
  $default_values = array(
384 385
    'country' => $default_country,
385 386
    'name_line' => '',
386 387
    'first_name' => '',
......
396 397
    'sub_premise' => '',
397 398
    'data' => '',
398 399
  );
400

  
401
  // Allow other modules to alter the default values.
402
  $context = array(
403
    'field' => $field,
404
    'instance' => $instance,
405
    'address' => $address,
406
  );
407
  drupal_alter('addressfield_default_values', $default_values, $context);
408

  
409
  return $default_values;
399 410
}
400 411

  
401 412
/**
......
447 458
    'field types' => array('addressfield'),
448 459
    'settings' => array(
449 460
      'available_countries' => array(),
461
      'default_country' => '',
450 462
      'format_handlers' => array('address'),
451 463
    ),
452 464
  );
......
472 484
      '#options' => _addressfield_country_options_list(),
473 485
      '#default_value' => $settings['available_countries'],
474 486
    );
475

  
487
    $form['default_country'] = array(
488
      '#type' => 'select',
489
      '#title' => t('Default country'),
490
      '#options' => _addressfield_country_options_list(),
491
      '#default_value' => $settings['default_country'],
492
      '#empty_value' => '',
493
    );
476 494
    $form['format_handlers'] = array(
477 495
      '#type' => 'checkboxes',
478 496
      '#title' => t('Format handlers'),
......
484 502
  return $form;
485 503
}
486 504

  
505
/**
506
 * Implements hook_form_BASE_FORM_ID_alter().
507
 *
508
 * Removes the default values form from the field settings page.
509
 * Allows the module to implement its own, more predictable default value
510
 * handling, getting around #1253820 and other bugs.
511
 */
512
function addressfield_form_field_ui_field_edit_form_alter(&$form, $form_state) {
513
  if ($form['#field']['type'] == 'addressfield') {
514
    $form['instance']['default_value_widget']['#access'] = FALSE;
515
  }
516
}
517

  
487 518
/**
488 519
 * Implements hook_field_widget_form()
489 520
 */
......
512 543
  }
513 544

  
514 545
  // Merge in default values.
515
  // Skip the country default if the field is optional, in which case it might
516
  // not have a country selected, causing the whole field to be treated as empty.
517
  // The same logic applies if the field allows multiple values, in which case
518
  // only the first delta is required.
519
  $default_values = addressfield_default_values($countries);
520
  if (empty($instance['required']) || $delta > 0) {
521
    $default_values['country'] = '';
522
  }
523
  $address += $default_values;
546
  $address += addressfield_default_values($field, $instance, $address);
524 547

  
525 548
  // Add the form elements for the standard widget, which includes a country
526 549
  // select list at the top that reloads the available address elements when the
......
533 556

  
534 557
    if (!empty($instance['description'])) {
535 558
      // Checkout panes convert the fieldset into a container, causing
536
      // #description to not be rendered. Hence, a real element is added.
559
      // #description to not be rendered. Hence, a real element is added and
560
      // the old #description is removed.
561
      $element['#description'] = '';
537 562
      $element['element_description'] = array(
538 563
        '#markup' =>  $instance['description'],
539 564
        '#prefix' => '<div class="fieldset-description">',
......
552 577
    );
553 578
    $element += addressfield_generate($address, $settings['format_handlers'], $context);
554 579

  
555
    // If #required is set to FALSE, _addressfield_process_format_form() strips
556
    // it from all child elements. This is only needed on the field edit form.
557
    $element['#required'] = ($form_state['build_info']['form_id'] != 'field_ui_field_edit_form');
580
    // Remove any already saved default value.
581
    // See addressfield_form_field_ui_field_edit_form_alter() for the reasoning.
582
    if ($form_state['build_info']['form_id'] == 'field_ui_field_edit_form') {
583
      $element['#address'] = array('country' => '');
584
    }
558 585
  }
559 586

  
560 587
  return $element;
......
763 790
 *
764 791
 * @see addressfield_property_info_callback()
765 792
 */
766
function addressfield_auto_creation() {
767
  // We can't call addressfield_default_values() directly, because it has an
768
  // optional array argument that will receive an invalid value when Entity API
769
  // tries to call it directly with its usual $property_name and $context
770
  // arguments.
771
  return addressfield_default_values();
793
function addressfield_auto_creation($property_name, $context) {
794
  return addressfield_default_values($context['field'], $context['instance']);
772 795
}
773 796

  
774 797
/**
......
833 856
}
834 857

  
835 858
/**
836
 * Wraps country_get_list() for use as an Entity API options list.
859
 * Returns the country list in a format suitable for use as an options list.
837 860
 */
838 861
function _addressfield_country_options_list($field = NULL, $instance = NULL) {
839
  // Necessary for country_get_list().
840
  require_once DRUPAL_ROOT . '/includes/locale.inc';
841

  
842
  $countries = country_get_list();
862
  if (module_exists('countries')) {
863
    $countries = countries_get_countries('name', array('enabled' => COUNTRIES_ENABLED));
864
  }
865
  else {
866
    require_once DRUPAL_ROOT . '/includes/locale.inc';
867
    $countries = country_get_list();
868
  }
843 869

  
844 870
  if (isset($field)) {
845 871
    // If the instance is not specified, loop against all the instances of the field.
......
858 884
    foreach ($instances as $instance) {
859 885
      if (!empty($instance['widget']['settings']['available_countries'])) {
860 886
        $countries = array_intersect_key($countries, $instance['widget']['settings']['available_countries']);
861
      }
862
      else {
863
        // This instance allow all the countries.
864
        $countries = country_get_list();
865 887
        break;
866 888
      }
867 889
    }

Formats disponibles : Unified diff