You are here

function faqfield_field_widget_form in FAQ Field 7

Implements hook_field_widget_form().

File

./faqfield.module, line 555
FAQ Field Provides a field for frequently asked questions.

Code

function faqfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $base = $element;
  if ($instance['widget']['type'] === 'faqfield_textboxes') {

    // If the current form is the default value fieldset of the field settings
    // form, we have to save the default value separately (Issue #1526448).
    if (isset($form['#title']) && $form['#title'] == t('Default value')) {
      $form['#element_validate'][] = 'faqfield_field_widget_form_default_value_element';
    }

    // If the current value is empty we choose the default one.
    if (empty($items[$delta]) && isset($instance['default_values'])) {
      $items[$delta] = $instance['default_values'];
    }

    // Add textfield for question.
    $element['question'] = array(
      '#title' => check_plain($field['settings']['advanced']['question_title']),
      '#type' => @$field['settings']['advanced']['question_rows'] > 1 ? 'textarea' : 'textfield',
      '#default_value' => @$items[$delta]['question'],
      '#maxlength' => $field['settings']['advanced']['question_length'],
      '#delta' => $delta,
      '#weight' => 0,
      '#rows' => @$field['settings']['advanced']['question_rows'],
    ) + $base;

    // Add textarea / formatable textarea / textfield for answer.
    $element['answer'] = array(
      '#title' => check_plain($field['settings']['advanced']['answer_title']),
      '#type' => $field['settings']['answer_widget'],
      '#default_value' => @$items[$delta]['answer'],
      '#delta' => $delta,
      '#weight' => 1,
      // We choose the source output format depending on the input type.
      '#format' => $field['settings']['answer_widget'] == 'text_format' ? @$items[$delta]['answer_format'] : $field['settings']['format'],
      '#rows' => $field['settings']['advanced']['answer_rows'],
    ) + $base;
    $element['remove'] = array(
      '#value' => t('Remove'),
      '#type' => 'submit',
      '#prefix' => '<p class="faqfield-remove-button">',
      '#suffix' => '</p>',
      '#weight' => 2,
    ) + $base;
  }
  return $element;
}