Using this with Drupal Commerce and set list to contain "Mr" and "Mrs" only.

In the ordering process the Title appears at the top of the address with "Please choose".
The field is NOT optional and in the list there is only "Mr" and "Mrs".

How can I make the field optional (no red asterisk) and/or accept something like a "-none-" value.
(i.e. at /admin/commerce/customer-profiles/types/billing/fields/commerce_customer_address)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jamescook created an issue. See original summary.

jamescook’s picture

Issue summary: View changes
jamescook’s picture

Issue summary: View changes
jamescook’s picture

Well it's hard-coded in plugins/format/title.inc

/**
 * Format callback.
 *
 * @see CALLBACK_addressfield_format_callback()
 */
function addressfield_format_title_generate(&$format, $address, $context = array()) {
  if ($context['mode'] == 'form') {
    // Create the list of prefixes.
    $prefixes = array();
    $prefixe_values = explode(' ', variable_get("addressfield_title_prefix", "Miss Mrs Ms Mr Dr Prof Rev Lady Lord Sir"));
    foreach ($prefixe_values as $prefixe_value) {
      $prefixes[$prefixe_value] = t($prefixe_value);
    }

    $format['person_title'] = array(
      '#type' => variable_get('addressfield_title_form_element', 'select'),
      '#empty_option' => t('Please choose'),
      '#options' => $prefixes,
      '#title' => t('Title'),
      '#required' => TRUE, 
      '#tag' => 'span',
      '#attributes' => array('class' => array('person-title')),
      '#default_value' => isset($address['person_title']) ? $address['person_title'] : '',
      '#weight' => -200,

Quick and dirty fix - In the above change #required to FALSE

Though really this should be an admin option in the GUI

attisan’s picture

if you do so, don't forget to fix_addressfield_title_render_address to this:

/**
 * Helper function of render address.
 */
function _addressfield_title_render_address(&$format) {
  $address = $format['#address'];

  if (isset($address['person_title'])) {
    $format['person_title'] = array(
      '#title' => t('Title'),
      '#tag' => 'span',
      '#weight' => '-200',
      '#children' => t(check_plain($address['person_title'])),
      '#attributes' => array('class' => array('person-title')),
    );
  }

  return $format;
}
saurabh.dhariwal’s picture

Added admin option for making the field optional in the GUI.

saurabh.dhariwal’s picture

Status: Active » Needs review
adam1’s picture

I used the above patch (thank you for that!) on latest stable & dev. While the patch works, there's a bunch of ugly arnings thrown out:

Warning: Missing argument 1 for addressfield_default_values(), called in mysite/sites/all/modules/contrib/addressfield_title/addressfield_title.module on line 45 and defined in addressfield_default_values() (Line 374 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Warning: Missing argument 2 for addressfield_default_values(), called in mysite/sites/all/modules/contrib/addressfield_title/addressfield_title.module on line 45 and defined in addressfield_default_values() (Line 374 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: field in addressfield_default_values() (Line 375 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (Line 375 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (Line 376 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: field in addressfield_default_values() (Line 408 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (Line 409 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Warning: Missing argument 1 for addressfield_default_values(), called in mysite/sites/all/modules/contrib/addressfield_title/addressfield_title.module on line 45 and defined in addressfield_default_values() (Line 374 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Warning: Missing argument 2 for addressfield_default_values(), called in mysite/sites/all/modules/contrib/addressfield_title/addressfield_title.module on line 45 and defined in addressfield_default_values() (Line 374 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: field in addressfield_default_values() (Line 375 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (Line 375 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (Line 376 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: field in addressfield_default_values() (Line 408 of mysite/sites/all/modules/contrib/addressfield/addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (Line 409 of mysite/sites/all/modules/contrib/addressfield/addressfield.module)

Maybe this issue is analogue to that one from its sister-module addressfield_phone:
https://www.drupal.org/node/2409695

Unfortunately I am not a developer, so I can't fix this for my own. Any help very much appreciated!