You are here

function _webform_client_form_add_component in Webform 6.3

Same name and namespace in other branches
  1. 5.2 webform.module \_webform_client_form_add_component()
  2. 5 webform.module \_webform_client_form_add_component()
  3. 6.2 webform.module \_webform_client_form_add_component()
  4. 7.4 webform.module \_webform_client_form_add_component()
  5. 7.3 webform.module \_webform_client_form_add_component()

Add a component to a renderable array. Called recursively for fieldsets.

This function assists in the building of the client form, as well as the display of results, and the text of e-mails.

Parameters

$component: The component to be added to the form.

$component_value: The components current value if known.

$parent_fieldset: The fieldset to which this element will be added.

$form: The entire form array.

$form_state: The form state.

$submission: The Webform submission as retrieved from the database.

$format: The format the form should be displayed as. May be one of the following:

  • form: Show as an editable form.
  • html: Show as HTML results.
  • text: Show as plain text.

$filter: Whether the form element properties should be filtered. Only set to FALSE if needing the raw properties for editing.

See also

webform_client_form()

webform_submission_render()

2 calls to _webform_client_form_add_component()
webform_client_form in ./webform.module
Client form generation function. If this is displaying an existing submission, pass in the $submission variable with the contents of the submission to be displayed.
webform_submission_render in includes/webform.submissions.inc
Print a Webform submission for display on a page or in an e-mail.

File

./webform.module, line 2008

Code

function _webform_client_form_add_component($node, $component, $component_value, &$parent_fieldset, &$form, $form_state, $submission, $format = 'form', $page_num = 0, $filter = TRUE) {
  $cid = $component['cid'];
  $component_access = empty($component['extra']['private']) || webform_results_access($node);

  // Load with submission information if necessary.
  if ($format != 'form') {

    // This component is display only.
    $data = empty($submission->data[$cid]['value']) ? NULL : $submission->data[$cid]['value'];
    if ($display_element = webform_component_invoke($component['type'], 'display', $component, $data, $format)) {

      // Set access based on the private property.
      $element['#access'] = $component_access;

      // Ensure the component is added as a property.
      $display_element['#webform_component'] = $component;

      // Allow modules to modify a "display only" webform component.
      drupal_alter('webform_component_display', $display_element, $component);

      // The form_builder() function usually adds #parents and #id for us, but
      // because these are not marked for #input, we need to add them manually.
      if (!isset($display_element['#parents'])) {
        $parents = isset($parent_fieldset['#parents']) ? $parent_fieldset['#parents'] : array(
          'submitted',
        );
        $parents[] = $component['form_key'];
        $display_element['#parents'] = $parents;
      }
      if (!isset($display_element['#id'])) {
        $display_element['#id'] = form_clean_id('edit-' . implode('-', $display_element['#parents']));
      }

      // Add the element into the proper parent in the display.
      $parent_fieldset[$component['form_key']] = $display_element;
    }
  }
  elseif ($component['page_num'] == $page_num || $filter == FALSE) {

    // Add this user-defined field to the form (with all the values that are always available).
    $data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL;
    if ($element = webform_component_invoke($component['type'], 'render', $component, $data, $filter)) {

      // Set access based on the private property.
      $element['#access'] = $component_access;

      // Ensure the component is added as a property.
      $element['#webform_component'] = $component;

      // The 'private' option is in most components, but it's not a real
      // property. Add it for Form Builder compatibility.
      if (webform_component_feature($component['type'], 'private')) {
        $element['#webform_private'] = $component['extra']['private'];
      }

      // Allow modules to modify a webform component that is going to be render in a form.
      drupal_alter('webform_component_render', $element, $component);

      // Add the element into the proper parent in the form.
      $parent_fieldset[$component['form_key']] = $element;

      // Override the value if one already exists in the form state.
      if (isset($component_value)) {
        $parent_fieldset[$component['form_key']]['#default_value'] = $component_value;
        if (is_array($component_value)) {
          foreach ($component_value as $key => $value) {
            if (isset($parent_fieldset[$component['form_key']][$key])) {
              $parent_fieldset[$component['form_key']][$key]['#default_value'] = $value;
            }
          }
        }
      }
    }
    else {
      drupal_set_message(t('The webform component @type is not able to be displayed', array(
        '@type' => $component['type'],
      )));
    }
  }

  // Disable validation initially on all elements. We manually validate
  // all webform elements in webform_client_form_validate().
  if (isset($parent_fieldset[$component['form_key']])) {
    $parent_fieldset[$component['form_key']]['#validated'] = TRUE;
    $parent_fieldset[$component['form_key']]['#webform_validated'] = FALSE;
  }
  if (isset($component['children']) && is_array($component['children'])) {
    foreach ($component['children'] as $scid => $subcomponent) {
      $subcomponent_value = isset($form_state['values']['submitted'][$scid]) ? $form_state['values']['submitted'][$scid] : NULL;
      if (_webform_client_form_rule_check($node, $subcomponent, $page_num, $form_state, $submission)) {
        _webform_client_form_add_component($node, $subcomponent, $subcomponent_value, $parent_fieldset[$component['form_key']], $form, $form_state, $submission, $format, $page_num, $filter);
      }
    }
  }
}