You are here

function webform_edit_field_form in Webform 5

2 string references to 'webform_edit_field_form'
webform_prepare in ./webform.module
Implementation of hook_prepare(). This function is called before the display of webform_form(). Rather than a typical usage of hook_prepare, in webform it is used to update the contents of the $node object after changes have been made to the node,…
webform_validate in ./webform.module
Implementation of hook_validate(). This function is called after the user clicks any button displayed in webform_form(). Rather than a typical usage of validation, in webform this is used to perform various actions without kicking the user out of the…

File

./webform.module, line 921

Code

function webform_edit_field_form(&$node) {

  // This is the information about the current field.
  $currfield = array();
  if ($_POST['op'] == t('Edit Selected')) {

    // Check to make sure a valid component id was selected.
    $cid = $node->selected_component;
    $component = $node->webformcomponents[$cid];
    if (empty($component)) {
      drupal_set_message(t('Component not found'), 'error');
      drupal_set_title("Webform Error");
      return array();
    }

    // We are editing a existing field.
    // Fetch all filed data into the $currfield object.
    $currfield['cid'] = $cid;
    $currfield['form_key'] = $component['form_key'] ? $component['form_key'] : $cid;
    $currfield['type'] = $component['type'];
    $currfield['name'] = $component['name'];
    $currfield['default'] = $component['value'];
    $currfield['parent'] = $component['parent'];
    $currfield['weight'] = $component['weight'];
    $currfield['mandatory'] = $component['mandatory'];
    $currfield['extra'] = $component['extra'];
    drupal_set_title(t("Edit component: @name (@type)", array(
      '@name' => $currfield['name'],
      '@type' => $currfield['type'],
    )));
  }
  else {

    // Check to make sure a valid component type was selected.
    $component_types = _webform_load_components();
    $new_component_type = $_POST['components']['webform_newfield_type'];
    if (empty($new_component_type) || !key_exists($new_component_type, $component_types)) {
      drupal_set_message(t('Unknown component type %component', array(
        '%component' => $new_component_type,
      )), 'error');
      drupal_set_title("Webform Error");
      print theme('page', "");
      return;
    }

    // We are editing a new field.
    $type = $_POST['components']['webform_newfield_type'];
    $currfield['cid'] = time();
    $currfield['type'] = $type;

    // Generate a resonable key and name.
    $delta = 0;
    foreach ((array) $node->webformcomponents as $field) {
      if ($field['type'] == $type) {
        $delta++;
      }
    }
    $currfield['form_key'] = $type . ($delta ? '_' . $delta : '');
    $currfield['name'] = drupal_ucfirst($type) . ($delta ? ' ' . $delta : '');
    drupal_set_title(t("Add new %type component", array(
      '%type' => t($currfield['type']),
    )));
  }
  $form = array();

  // Print the correct field type specification.
  // We always need: name and description.
  $form['field'] = array(
    '#type' => 'fieldset',
    '#title' => t('Field Details'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#weight' => 5,
    '#tree' => TRUE,
  );
  $form['field']['type'] = array(
    '#type' => 'hidden',
    '#value' => $currfield['type'],
  );
  $form['field']['cid'] = array(
    '#type' => 'hidden',
    '#value' => $currfield['cid'],
  );
  $form['field']['form_key'] = array(
    '#type' => 'textfield',
    '#default_value' => $currfield['form_key'],
    '#title' => t('Field Key'),
    '#description' => t('Enter a machine readable key for this form element. May contain only lowercase alphanumeric characters and underscores. This key will be used as the name attribute of the form element.'),
    '#required' => TRUE,
    '#weight' => -2,
  );
  $form['field']['name'] = array(
    '#type' => 'textfield',
    '#default_value' => $currfield['name'],
    '#title' => t("Label"),
    '#description' => t('This is used as a descriptive label when displaying this form element.'),
    '#required' => TRUE,
    '#weight' => -1,
  );
  $form['field']['extra']['description'] = array(
    '#type' => 'textfield',
    '#default_value' => $currfield['extra']['description'],
    '#title' => t("Description"),
    '#maxlength' => '512',
    '#description' => t('A short description of the field used as help for the user when he/she uses the form.') . '<br />' . webform_help('webform/helptext#variables'),
    '#weight' => -1,
  );
  $form['field']['mandatory'] = array(
    '#type' => 'checkbox',
    '#title' => t("Mandatory"),
    '#default_value' => $currfield['mandatory'] == '1' ? TRUE : FALSE,
    '#description' => t('Check this option if the user must enter a value.'),
    '#weight' => 2,
  );
  if (variable_get('webform_enable_fieldset', true) && is_array($node->webformcomponents)) {
    $options = array(
      '0' => t('Root'),
    );
    foreach ($node->webformcomponents as $thiscid => $value) {
      if ($value['type'] == 'fieldset' && $thiscid != $cid) {
        $options[$thiscid] = htmlspecialchars($value['name'], ENT_QUOTES);
      }
    }
    $form['field']['parent'] = array(
      '#type' => 'select',
      '#title' => t("Parent Fieldset"),
      '#default_value' => $currfield['parent'],
      '#description' => t('Optional. You may organize your form by placing this component inside inside another fieldset.'),
      '#options' => $options,
      '#weight' => 2,
    );
  }
  $form['field']['weight'] = array(
    '#type' => 'weight',
    '#delta' => count($node->webformcomponents) > 10 ? count($node->webformcomponents) : 10,
    '#title' => t("Weight"),
    '#default_value' => $currfield['weight'],
    '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    '#weight' => 2,
  );

  // Add the fields specific to this component type:
  _webform_load_components();

  // Load all component types.
  $edit_function = "_webform_edit_" . $currfield['type'];
  $additional_form_elements = array();
  if (function_exists($edit_function)) {
    $additional_form_elements = $edit_function($currfield);

    // Call the component render function.
  }
  else {
    drupal_set_message(t("The webform component of type @type does not have an edit function defined.", array(
      '@type' => $currfield['type'],
    )));
  }

  // Merge the additional fields with the current fields:
  $extra_fields_copy = $form['field']['extra'];
  $form['field'] = array_merge($form['field'], $additional_form_elements);
  $form['field']['extra'] = array_merge((array) $extra_fields_copy, (array) $additional_form_elements['extra']);

  // Add the submit button.
  $form['field']['submit'] = array(
    '#type' => 'submit',
    '#value' => t("Done"),
    '#weight' => 3,
  );

  // Create hidden form elements to restore all the settings on the node edit form.
  $form['node'] = array(
    '#tree' => TRUE,
  );

  // Recursively create hidden form elements for all fields on the node form.
  function _webform_edit_field_form_hiddens(&$form, $key, $value) {
    if (is_array($value)) {
      foreach ($value as $k => $v) {
        _webform_edit_field_form_hiddens($form[$key], $k, $v);
      }
    }
    else {
      $form[$key] = array(
        '#type' => 'hidden',
        '#value' => $value,
      );
    }
  }
  _webform_edit_field_form_hiddens($form, 'node', (array) $node);
  return $form;
}