I have installed the 'phone' module, which enables a new field type called 'phone'. (It's available through the Field UI by selected 'Phone Number' as the field type).

How do I gain access to this field type in a programatically created form? I have a form, that calls it like this:

$form['phone'] = array(
    '#type' => 'phone',
    '#title' => t('Your phone number'),
    '#required' => TRUE,  
);

however, when I render the form, that field is not available on the form. The same thing applies with the 'email' module, if I set the type of that field type '#email'. How can I implement these two field types programatically?

phone appears to have some settings that you set when you create the field instance, but I can't figure out how to tell the $form to set these settings when declaring the field type (If that's even the issue)

Comments

Jaypan’s picture

I just took a look at the module, and it does not create a form element type that you can use, so you'll have to come up with something custom to mimic it.

jfurnas’s picture

Hmm, then how does the drupal field UI do it? When you use the field UI and select 'Phone Number' from the drop down list, it creates a field on the form for you (after you define the settings).

Is there no way to do this programatically as well? What part of the functionality makes it available to use as a field type?

Jaypan’s picture

The module uses hook_widget_info() to declare the widget, and uses a textfield for the input.

/**
 * Implements hook_field_widget_form().
 */
function phone_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element += array(
    '#type' => 'textfield',
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
    '#required' => $element['#required'],
    '#size' => 17,
    '#maxlength' => (
      $field['settings']['country'] == 'int' ?
        (isset($instance['settings']['phone_int_max_length']) ? $instance['settings']['phone_int_max_length'] : NULL)
        : NULL
    ),
  );
  return array('value' => $element);
}
jfurnas’s picture

Hmm, interesting. So what would declare it possible to use it as a 'phone'? Changing the #type to 'phone' ?

It looks like declaring it as a 'textfield' (without modifying the module) is the only way to do it. It does have a field validator to check if it's a valid phone number. Is it possible for me to use this validation callback on the custom $form['phone'] field that I make, or do I need to make a new validation handler for it?

Jaypan’s picture

Changing it to type phone won't do anything, as that element type has not been declared.

It's probably possible to re-use the validation handler from the module.

jfurnas’s picture

Yeah, I think that is what I'll do. What I was going to get at, was modifying the 'phone' module to allow me to use it as a field type, and give the solution back to the module (Although its not longer in development so no point really)