You are here

function webform_component_edit_form in Webform 5.2

Same name and namespace in other branches
  1. 6.3 includes/webform.components.inc \webform_component_edit_form()
  2. 6.2 webform_components.inc \webform_component_edit_form()
  3. 7.4 includes/webform.components.inc \webform_component_edit_form()
  4. 7.3 includes/webform.components.inc \webform_component_edit_form()
1 string reference to 'webform_component_edit_form'
webform_components in ./webform.module
Menu callback for node/[nid]/components.

File

./webform_components.inc, line 271
Webform module components handling.

Code

function webform_component_edit_form($node, $component, $clone = FALSE) {
  drupal_set_title(t("Edit component: @name (@type)", array(
    '@name' => $component['name'],
    '@type' => t($component['type']),
  )));

  // Print the correct field type specification.
  // We always need: name and description.
  $form = array(
    '#tree' => TRUE,
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $component['type'],
  );
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $node->nid,
  );
  $form['cid'] = array(
    '#type' => 'value',
    '#value' => isset($component['cid']) ? $component['cid'] : NULL,
  );
  $form['clone'] = array(
    '#type' => 'value',
    '#value' => $clone,
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#default_value' => $component['name'],
    '#title' => t('Label'),
    '#description' => t('This is used as a descriptive label when displaying this form element.'),
    '#required' => TRUE,
    '#weight' => -2,
    '#maxlength' => 255,
  );
  $form['extra']['description'] = array(
    '#type' => 'textarea',
    '#default_value' => isset($component['extra']['description']) ? $component['extra']['description'] : '',
    '#title' => t('Description'),
    '#description' => t('A short description of the field used as help for the user when he/she uses the form.') . theme('webform_token_help'),
    '#weight' => -1,
  );
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => FALSE,
    'weight' => 20,
  );
  $form['advanced']['form_key'] = array(
    '#type' => 'textfield',
    '#default_value' => empty($component['form_key']) ? _webform_safe_name($component['name']) : $component['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. This value has no effect on the way data is saved, but may be helpful if using Additional Processing or Validation code.'),
    '#required' => TRUE,
    '#weight' => -1,
  );
  $form['advanced']['mandatory'] = array(
    '#type' => 'checkbox',
    '#title' => t('Mandatory'),
    '#default_value' => $component['mandatory'] == '1' ? TRUE : FALSE,
    '#description' => t('Check this option if the user must enter a value.'),
    '#weight' => 2,
    '#access' => !in_array($component['type'], array(
      'pagebreak',
      'fieldset',
    )),
  );
  $form['advanced']['email'] = array(
    '#type' => 'checkbox',
    '#title' => t("Include in e-mails"),
    '#default_value' => $component['email'] == '1' ? TRUE : FALSE,
    '#description' => t('If checked, submitted values from this component will be included in e-mails.'),
    '#weight' => 2,
    '#access' => !in_array($component['type'], array(
      'pagebreak',
      'fieldset',
      'markup',
    )),
  );
  if (variable_get('webform_enable_fieldset', true) && is_array($node->webform['components'])) {
    $options = array(
      '0' => t('Root'),
    );
    foreach ($node->webform['components'] as $existing_cid => $value) {
      if ($value['type'] == 'fieldset' && (!isset($component['cid']) || $existing_cid != $component['cid'])) {
        $options[$existing_cid] = $value['name'];
      }
    }
    $form['advanced']['pid'] = array(
      '#type' => 'select',
      '#title' => t('Parent Fieldset'),
      '#default_value' => $component['pid'],
      '#description' => t('Optional. You may organize your form by placing this component inside another fieldset.'),
      '#options' => $options,
      '#weight' => 3,
    );
  }
  $form['advanced']['weight'] = array(
    '#type' => 'weight',
    '#delta' => count($node->webform['components']) > 10 ? count($node->webform['components']) : 10,
    '#title' => t('Weight'),
    '#default_value' => $component['weight'],
    '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    '#weight' => 4,
  );

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

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

    // 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' => $component['type'],
    )));
  }

  // Merge the additional fields with the current fields:
  if (isset($additional_form_elements['extra'])) {
    $extra_fields_copy = $form['extra'];
    $form['extra'] = array_merge($extra_fields_copy, $additional_form_elements['extra']);
    unset($additional_form_elements['extra']);
  }
  if (isset($additional_form_elements['advanced'])) {
    $advanced_fields_copy = $form['advanced'];
    $form['advanced'] = array_merge($advanced_fields_copy, $additional_form_elements['advanced']);
    unset($additional_form_elements['advanced']);
  }
  $form = array_merge($form, $additional_form_elements);

  // Add the submit button.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 5,
  );
  return $form;
}