You are here

function _webform_client_form_add_component in Webform 7.4

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.3 webform.module \_webform_client_form_add_component()
  4. 6.2 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

object $node: The current webform node.

$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.

$input_values: All the values for this form, keyed by the component IDs. This may be pulled from $form_state['values']['submitted'] or $submission->data. These values are used to check if the component should be displayed conditionally.

$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.

$page_num: The page number. Defaults to 0.

$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()

3 calls to _webform_client_form_add_component()
webform_client_form in ./webform.module
Client form generation function.
webform_handler_field_submission_data::render in views/webform_handler_field_submission_data.inc
Render the field using the loaded submissions from pre_render().
webform_submission_render in includes/webform.submissions.inc
Prepare a Webform submission for display on a page or in an e-mail.

File

./webform.module, line 2853
This module provides a simple way to create forms and questionnaires.

Code

function _webform_client_form_add_component($node, $component, $component_value, &$parent_fieldset, &$form, $input_values, $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($input_values[$cid]) ? NULL : $input_values[$cid];
    if ($display_element = webform_component_invoke($component['type'], 'display', $component, $data, $format, $form['#submission'])) {

      // Set access based on the private property.
      $display_element += array(
        '#access' => TRUE,
      );
      $display_element['#access'] = $display_element['#access'] && $component_access;

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

      // Add custom CSS classes to the field and wrapper.
      _webform_component_classes($display_element, $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'] = drupal_clean_css_identifier('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).
    if ($element = webform_component_invoke($component['type'], 'render', $component, $component_value, $filter, $form['#submission'])) {

      // Set access based on the private property.
      $element += array(
        '#access' => TRUE,
      );
      $element['#access'] = $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'];
      }

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

      // Add custom CSS classes to the field and wrapper.
      _webform_component_classes($element, $component);

      // 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;
    }
    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'])) {
    $sorter = webform_get_conditional_sorter($node);
    foreach ($component['children'] as $scid => $subcomponent) {
      $subcomponent_value = isset($input_values[$scid]) ? $input_values[$scid] : NULL;

      // Include if always shown, or for forms, also if currently hidden but
      // might be shown due to conditionals.
      $visibility = $sorter
        ->componentVisibility($scid, $subcomponent['page_num']);
      if ($visibility == WebformConditionals::componentShown || $format == 'form' && $visibility || !$filter) {
        _webform_client_form_add_component($node, $subcomponent, $subcomponent_value, $parent_fieldset[$component['form_key']], $form, $input_values, $format, $page_num, $filter);
      }
    }
  }
}