You are here

function field_nif_field_widget_form in Field NIF 7

Implements hook_field_widget_form().

File

./field_nif.module, line 44

Code

function field_nif_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($instance['widget']['type'] == 'nif_data') {

    // Add a virtual element that handles the complete nif number.
    $element['nif'] = array(
      '#type' => 'textfield',
      '#title' => $element['#title'],
      '#description' => $element['#description'],
      '#default_value' => isset($items[$delta]['nif']) ? $items[$delta]['nif'] : '',
      '#required' => $instance['required'] && ($delta == 0 || $field['cardinality'] > 0),
      '#size' => 10,
    );

    // Add the elements stored in db.
    $element['number'] = array(
      '#type' => 'hidden',
    );
    $element['first_letter'] = array(
      '#type' => 'hidden',
    );
    $element['last_letter'] = array(
      '#type' => 'hidden',
    );
    $element['type'] = array(
      '#type' => 'hidden',
    );
    if (module_exists('i18n_field')) {
      $instance_current = i18n_string_object_translate('field_instance', $instance);

      // Translate field title and description if set and it is the default one.
      if (!empty($instance_current['label']) && $instance_current['label'] != $instance['label']) {
        $element['nif']['#title'] = $instance_current['label'];
      }
      if (!empty($instance_current['description']) && $instance_current['description'] != $instance['description']) {
        $element['nif']['#description'] = $instance_current['description'];
      }
    }
  }

  // Add a validation function
  $element['#element_validate'][] = 'field_nif_validate_nif_number';

  // Expose the instance settings to the validator.
  $form_state['instance'] = $instance;
  return $element;
}