Hi there,

Could you add the ability to make a phone number a required field? It would be great if the module would allow you to set a checkbox so that either the primary number or any of the three phone fields would be required for entry by the user. Thanks for your consideration.

Cheers,

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kvoltz’s picture

Any progress with this? This would be a great feature to add.

svouthi’s picture

Issue summary: View changes

I am interested in this basic functionality also.

sketman’s picture

I would welcome this feature too...

svouthi’s picture

I went ahead and installed Field Group and Commerce Fieldgroup Panes (since my use case is with Commerce), and created fields as mentioned in this comment, making the phone number field required. Granted, this field has no special formatting, but it is adequate for my needs at present.

jcisio’s picture

FWIW it's quite easy to make phone number required with something like this:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'commerce_checkout_form_checkout') {
    $form['customer_profile_shipping']['commerce_customer_address'][LANGUAGE_NONE][0]['phone_block']['phone_number']['phone_number']['#required'] = TRUE;
  }
}

or even better, in some case:

function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {
  if (!empty($element['#addressfield'])) {
    $element['phone_block']['phone_number']['phone_number']['#required'] = TRUE;
  }
}
Jawi’s picture

Issue tags: +phone number, +required

Thanks jcisio for your input. Do you place this adjustment in a custom module or in the Address Field Phone module?

jcisio’s picture

Status: Active » Closed (won't fix)

Finally I think it does not worth adding this feature that will make the UI having too many options.

millionleaves’s picture

Thanks to jcisio for the code in #5; this gave me what I wanted. A couple of additional observations for someone looking to use it:

  • The first snippet of code is just for Drupal Commerce
  • There is no UI that allows you to change the way it works. You need to modify the code (see the next two points).
  • It only affects the phone number in the Shipping address profile. You can make a copy of that line in your module and change "shipping" to "billing".
  • The Address Field Phone module allows different phone number types. This snippet only covers the main Phone field. Again, you could edit or extend the code provided to cover the other types of phone number supported by Address Field Phone.

It's a shame this functionality won't be added to the Address Field Phone module, but it's clear the UI would get pretty messy once the scenarios outlined in the 3rd and 4th points are taken into account.

tucho’s picture

Version: 7.x-1.0 » 7.x-1.x-dev
Status: Closed (won't fix) » Needs review
FileSize
4.6 KB

I attached a patch that add this feature to the module.

It replaces each checkbox for the available fields with a select with 3 states:

  • Hidden
  • Optional
  • Required

I keep the default behavior, so if all the fields are hidden, the four fields are shown in the address form.

I have merged the patch from #2481897: Undefined index in addressfield_format_phone_generate() to avoid some warnings when a field is not selected.

SocialNicheGuru’s picture

jcisio’s picture

Status: Needs review » Needs work

The other issue is fixed, patch needs reroll.

DamienMcKenna’s picture

Status: Needs work » Needs review
FileSize
4.36 KB

Rerolled.

DamienMcKenna’s picture

FileSize
4.37 KB

FYI the other issue didn't completely fix the problem with missing variables, so this should cover it.

SocialNicheGuru’s picture

Aporie’s picture

Version: 7.x-1.x-dev » 7.x-1.3

So,

None of the solutions above work for me :

1) Patch apply to 7.x-1.3 version and not dev version with the following command (I'm saying it because took me a while to figure it out) : patch -p1 < thepatch.patch with the patch in the root folder of addressfield_phone.
2) Patch even if patching process worked did nothing .... (didn't see any change, none select list for the checkbox as described in #9)
3)#5 also didn't work with the folowing :

function addressfield_phone_field_widget_form_alter(&$element, &$form_state, $context) {
  if (!empty($element['#addressfield'])) {
    $element['phone_block']['phone_number']['phone_number']['#required'] = TRUE;
  }
}

4) Probably all of that come from me because I'm not a php dev, but still, here are my feedbacks.

tucho’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
FileSize
4.36 KB

Patch re rolled for current 7.x-1.x dev version!

Aporie’s picture

Is there something I have to know, like this patch apply to new fields or whatever ?

Because I'm sorry but still no change ... I'm sorry to annoy you with that.

[edit] So it bother me so much that I directly edited the module files :

In phone.inc, just remove conditionnal directives on the field you want to make required :

'#required' => (!empty($settings['extension']) && $settings['extension'] == 'required'),

Into :

        '#required' => 'required',
mbreden’s picture

#5 didn't work for me, however this did.

/**
 * Implements hook_ctools_plugin_pre_alter().
 *
 * Changes the plugin callback for the phone address formatter so that we can
 * override some values.
 */
function mymodule_ctools_plugin_pre_alter(&$plugin, &$info) {
  if (isset($plugin['format callback']) && $plugin['format callback'] == 'addressfield_format_phone_generate') {
    $plugin['format callback'] = 'mymodule_format_phone_generate';
  }
}

/**
 * Overrides the phone plugin formatter.
 *
 * This overrides the plugin callback for the phone formatter and sets the
 * phone block #required to TRUE.
 */
function mymodule_format_phone_generate(&$format, $address, $context = array()) {
  if (is_callable('addressfield_format_phone_generate')) {
    addressfield_format_phone_generate($format, $address, $context);
  }
  if (isset($format['phone_block']['phone_number']['phone_number'])) {
    $format['phone_block']['phone_number']['phone_number']['#required'] = TRUE;
  }
}

StijnBousard’s picture

Patch #16 is exactly what I need! Thanks!

philsward’s picture

Status: Needs review » Reviewed & tested by the community

#16 worked beautifully against 1.4. Unless there's a reason to change how the UX works, (I think it's fine for intended purposes) I'd say let's get this committed.

jcisio’s picture

Status: Reviewed & tested by the community » Fixed

Patch looks good. The UI is less clumsy than I thought. Thanks all. Committed and pushed.

  • jcisio committed ef5e080 on 7.x-1.x authored by tucho
    Issue #1870974 by DamienMcKenna, tucho: Add checkbox to make phone...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

No Sssweat’s picture

This still isn't available in the latest stable version.

Time to release a new version? 7.x-1.5 ?