You are here

function email_widget in Email Field 5

Same name and namespace in other branches
  1. 6.2 email.module \email_widget()
  2. 6 email.module \email_widget()

Implementation of hook_widget().

File

./email.module, line 133

Code

function email_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
        '#weight' => $field['widget']['weight'],
      );
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#title'] = t($field['widget']['label']);
        foreach (range(0, 2) as $delta) {
          $default_value = "";
          if (isset($field['widget']['default_value'][$delta]['email'])) {
            $default_value = $field['widget']['default_value'][$delta]['email'];
          }
          $form[$field['field_name']][$delta]['email'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#default_value' => isset($node_field[$delta]['email']) ? $node_field[$delta]['email'] : $default_value,
            '#required' => $field['required'] ? $field['required'] : FALSE,
            '#maxlength' => 255,
            '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
            '#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
          );
        }
      }
      else {
        $default_value = "";
        if (isset($field['widget']['default_value'][0]['email'])) {
          $default_value = $field['widget']['default_value'][0]['email'];
        }
        $form[$field['field_name']][0]['email'] = array(
          '#type' => 'textfield',
          '#title' => $field['widget']['label'],
          '#default_value' => isset($node_field[0]['email']) ? $node_field[0]['email'] : $default_value,
          '#required' => $field['required'] ? $field['required'] : FALSE,
          '#maxlength' => 255,
          '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
          '#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
        );
      }
      return $form;
    case 'validate':
      if (is_array($node_field)) {
        foreach ($node_field as $delta => $item) {
          if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
            form_set_error($field['field_name'], t('"%mail" is not a valid email address', array(
              '%mail' => $item['email'],
            )));
          }
        }
      }
      break;
  }
}