You are here

form_builder_webform.components.inc in Form Builder 7.2

Default webform component callbacks for functionality related to the Form Builder.

File

modules/webform/form_builder_webform.components.inc
View source
<?php

/**
 * @file
 *   Default webform component callbacks for functionality related to the Form Builder.
 */
use Drupal\form_builder\Loader;
use Drupal\form_builder_webform\HiddenElement;

/**
 * Configuration form for the "unique" property.
 */
function form_builder_webform_property_unique_form($component, $edit, &$form_state, $property) {
  $form['unique'] = array(
    '#form_builder' => array(
      'property_group' => 'validation',
    ),
  ) + $edit['validation']['unique'];
  return $form;
}

/**
 * @defgroup form-builder-webform-date-callbacks Callbacks for the Date component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_date() {
  $fields = array();
  $fields['date'] = array(
    'title' => t('Date'),
    'weight' => -2,
  );
  $component['name'] = t('New date');
  $fields['date']['default'] = _form_builder_webform_default('date', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_date() {
  return array(
    'form_builder_type' => 'date',
    'properties' => array(
      'timezone' => array(
        'form_parents' => array(
          'default',
          'timezone',
        ),
        'storage_parents' => array(
          'extra',
          'timezone',
        ),
      ),
      'start_date' => array(
        'form_parents' => array(
          'validation',
          'start_date',
        ),
        'storage_parents' => array(
          'extra',
          'start_date',
        ),
      ),
      'end_date' => array(
        'form_parents' => array(
          'validation',
          'end_date',
        ),
        'storage_parents' => array(
          'extra',
          'end_date',
        ),
      ),
      'year_textfield' => array(
        'form_parents' => array(
          'display',
          'year_textfield',
        ),
        'storage_parents' => array(
          'extra',
          'year_textfield',
        ),
      ),
      'datepicker' => array(
        'form_parents' => array(
          'display',
          'datepicker',
        ),
        'storage_parents' => array(
          'extra',
          'datepicker',
        ),
      ),
    ),
  );
}

/**
 * @} End of "defgroup form-builder-webform-date-callbacks"
 */

/**
 * @defgroup form-builder-webform-email-callbacks Callbacks for the Email component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_email() {
  $fields = array();
  $fields['email'] = array(
    'title' => t('E-mail'),
    'weight' => -18,
  );
  $component['name'] = t('New e-mail');
  $fields['email']['default'] = _form_builder_webform_default('email', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_email() {
  return array(
    'form_builder_type' => 'email',
    'properties' => array(
      'size' => array(
        'storage_parents' => array(
          'extra',
          'width',
        ),
      ),
      'disabled' => array(
        'storage_parents' => array(
          'extra',
          'disabled',
        ),
      ),
      'unique' => array(
        'storage_parents' => array(
          'extra',
          'unique',
        ),
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_properties_component().
 */
function _form_builder_webform_form_builder_properties_email() {
  return array(
    'unique' => array(
      'form' => 'form_builder_webform_property_unique_form',
    ),
  );
}

/**
 * @} End of "defgroup form-builder-webform-email-callbacks"
 */

/**
 * @defgroup form-builder-webform-fieldset-callbacks Callbacks for the Fieldset component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_fieldset() {
  $fields = array();
  $fields['fieldset'] = array(
    'title' => t('Fieldset'),
    'weight' => 19,
  );
  $component['name'] = t('New fieldset');
  $fields['fieldset']['default'] = _form_builder_webform_default('fieldset', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_fieldset() {
  return array(
    'form_builder_type' => 'fieldset',
    'properties' => array(
      'collapsible' => array(
        'storage_parents' => array(
          'extra',
          'collapsible',
        ),
      ),
      'collapsed' => array(
        'storage_parents' => array(
          'extra',
          'collapsed',
        ),
      ),
    ),
  );
}

/**
 * @} End of "defgroup form-builder-webform-fieldset-callbacks"
 */

/**
 * @defgroup form-builder-webform-file-callbacks Callbacks for the File component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_file() {
  $fields = array();
  $fields['file'] = array(
    'title' => t('File'),
    'weight' => 6,
  );
  $component['name'] = t('New file');
  $fields['file']['default'] = _form_builder_webform_default('file', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_file() {
  return array(
    'form_builder_type' => 'file',
    'properties' => array(
      // Webform 3.16 and earlier file filtering.
      'webform_file_filtering' => array(
        'form_parents' => array(
          'validation',
          'filtering',
        ),
        'storage_parents' => array(
          'extra',
          'filtering',
        ),
      ),
      // Webform 3.17 and later file filtering.
      'webform_file_extensions' => array(
        'form_parents' => array(
          'validation',
          'extensions',
        ),
        'storage_parents' => array(
          'extra',
          'filtering',
        ),
      ),
      'webform_file_size' => array(
        'form_parents' => array(
          'validation',
          'size',
        ),
        'storage_parents' => array(
          'extra',
          'filtering',
          'size',
        ),
      ),
      'webform_file_directory' => array(
        'form_parents' => array(
          'extra',
          'directory',
        ),
        'storage_parents' => array(
          'extra',
          'directory',
        ),
      ),
      'webform_file_scheme' => array(
        'form_parents' => array(
          'extra',
          'scheme',
        ),
        'storage_parents' => array(
          'extra',
          'scheme',
        ),
      ),
      'webform_file_progress_indicator' => array(
        'form_parents' => array(
          'display',
          'progress_indicator',
        ),
        'storage_parents' => array(
          'extra',
          'progress_indicator',
        ),
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_preview_alter_component().
 */
function _form_builder_webform_form_builder_preview_alter_file($form_element) {

  // Remove AJAX wrappers.
  $form_element['#prefix'] = '';
  $form_element['#suffix'] = '';
  $form_element['upload_button']['#disabled'] = TRUE;
  return $form_element;
}

/**
 * @} End of "defgroup form-builder-webform-file-callbacks"
 */

/**
 * @defgroup form-builder-webform-grid-callbacks Callbacks for the Grid component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_grid() {
  $fields = array();
  $fields['grid'] = array(
    'title' => t('Grid'),
    'properties' => array(
      // Grid-specific properties.
      'grid_options',
      'grid_questions',
      'custom_grid_option_keys',
      'custom_grid_question_keys',
    ),
    'weight' => -4,
  );
  $component['name'] = t('New grid');
  $extra['options'] = _webform_select_options_to_text(array(
    t('poor'),
    t('average'),
    t('good'),
    t('excellent'),
  ));
  $extra['questions'] = _webform_select_options_to_text(array(
    t('question one'),
    t('question two'),
    t('question three'),
  ));
  $fields['grid']['default'] = _form_builder_webform_default('grid', $extra, $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_grid() {
  return array(
    'form_builder_type' => 'grid',
    'properties' => array(
      'custom_grid_option_keys' => array(
        'storage_parents' => array(
          'extra',
          'custom_option_keys',
        ),
      ),
      'custom_grid_question_keys' => array(
        'storage_parents' => array(
          'extra',
          'custom_question_keys',
        ),
      ),
      'grid_options' => array(
        'storage_parents' => [
          'extra',
          'options',
        ],
        'form_parents' => [
          'options',
        ],
        'property_group' => 'options',
      ),
      'grid_questions' => array(
        'storage_parents' => [
          'extra',
          'questions',
        ],
        'form_parents' => [
          'questions',
        ],
        'property_group' => 'options',
      ),
      'optrand' => array(
        'form_parents' => array(
          'display',
          'optrand',
        ),
        'storage_parents' => array(
          'extra',
          'optrand',
        ),
      ),
      'qrand' => array(
        'form_parents' => array(
          'display',
          'qrand',
        ),
        'storage_parents' => array(
          'extra',
          'qrand',
        ),
      ),
      'unique' => array(
        'storage_parents' => array(
          'extra',
          'unique',
        ),
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_properties_component().
 */
function _form_builder_webform_form_builder_properties_grid() {
  return array(
    'grid_options' => array(
      'submit' => array(
        'form_builder_webform_property_grid_form_submit',
      ),
    ),
    'grid_questions' => array(
      'submit' => array(
        'form_builder_webform_property_grid_form_submit',
      ),
    ),
    'unique' => array(
      'form' => 'form_builder_webform_property_unique_form',
    ),
  );
}

/**
 * Submit handler for the "grid_options" and "grid_questions" properties.
 */
function form_builder_webform_property_grid_form_submit(&$form, &$form_state) {

  // _webform_edit_validate_options() hardcodes the #parents for values.
  if (empty($form_state['values']['grid_options'])) {
    $form_state['values']['grid_options'] = $form_state['values']['extra']['grid_options'];
    $form_state['values']['custom_grid_option_keys'] = $form_state['values']['extra']['custom_grid_option_keys'];
  }
  if (empty($form_state['values']['grid_questions'])) {
    $form_state['values']['grid_questions'] = $form_state['values']['extra']['grid_questions'];
    $form_state['values']['custom_grid_question_keys'] = $form_state['values']['extra']['custom_grid_question_keys'];
  }
}

/**
 * @} End of "defgroup form-builder-webform-time-callbacks"
 */

/**
 * @defgroup form-builder-webform-hidden-callbacks Callbacks for the Hidden component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_hidden() {
  $fields = array();
  $fields['hidden'] = array(
    'title' => t('Hidden'),
    'weight' => 8,
    'class' => HiddenElement::class,
  );
  $component['name'] = t('New hidden');
  $fields['hidden']['default'] = _form_builder_webform_default('hidden', array(
    'hidden_type' => 'hidden',
  ), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_preview_alter_component().
 */
function _form_builder_webform_form_builder_preview_alter_hidden($form_element) {

  // Convert the hidden element to a markup element before displaying it.
  $form_element['#type'] = 'markup';
  $form_element['#form_builder']['element_type'] = 'markup';
  unset($form_element['#theme']);
  $c = $form_element['#webform_component'];
  if ($c['extra']['hidden_type'] == 'value') {
    array_unshift($form_element['#theme_wrappers'], 'webform_element');
  }
  else {
    unset($form_element['#wrapper_attributes']);
  }

  // Display the title of the hidden field as regular markup.
  $v['@title'] = $c['name'];
  $v['@type'] = $c['extra']['hidden_type'] == 'value' ? 'secure' : 'insecure - editable by JavaScript';
  $form_element['#children'] = t('@title - <em>hidden field (@type)</em>', $v);
  return $form_element;
}

/**
 * @} End of "defgroup form-builder-webform-hidden-callbacks"
 */

/**
 * @defgroup form-builder-webform-markup-callbacks Callbacks for the Markup component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_markup() {
  $fields = array();
  $default = '';
  $default .= '<p><strong>' . t('New HTML Markup') . '</strong></p>' . "\n\n";
  $default .= '<p>' . t('Enter any text supported by available text formats.') . '</p>';
  $fields['markup'] = array(
    'title' => t('Markup'),
    'properties' => array(
      'markup',
      'format',
    ),
    'weight' => 17,
  );
  $component['name'] = t('New markup');
  $component['value'] = $default;
  $extras['format'] = filter_default_format($GLOBALS['user']);
  $fields['markup']['default'] = _form_builder_webform_default('markup', $extras, $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_markup() {
  return array(
    'form_builder_type' => 'markup',
    'properties' => array(
      'markup' => array(
        'storage_parents' => array(
          'value',
        ),
      ),
      'format' => array(
        'storage_parents' => array(
          'extra',
          'format',
        ),
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_preview_alter_component().
 */
function _form_builder_webform_form_builder_preview_alter_markup($form_element) {

  // Filter the markup with the appropriate text format before displaying it.
  $form_element['#markup'] = isset($form_element['#markup']) ? $form_element['#markup'] : '';
  $format = isset($form_element['#format']) ? $form_element['#format'] : filter_default_format();
  $form_element['#markup'] = check_markup(_webform_filter_values($form_element['#markup'], NULL, NULL, NULL, FALSE), $format, '', TRUE);
  if (strlen(trim($form_element['#markup'])) == 0) {
    $form_element['#markup'] = t('Empty markup field');
  }

  // This runs after drupal_pre_render_markup so the changed markup
  // needs to be copied to #children.
  $form_element['#children'] = $form_element['#markup'];

  // Do not show the title, since it will not display in the final webform
  // either.
  $form_element['#title_display'] = 'none';
  return $form_element;
}

/**
 * @} End of "defgroup form-builder-webform-markup-callbacks"
 */

/**
 * @defgroup form-builder-webform-number-callbacks Callbacks for the Number component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_number() {
  $fields = array();
  $fields['number'] = array(
    'title' => t('Number'),
    'weight' => -17,
  );
  $component['name'] = t('New number');
  $fields['number']['default'] = _form_builder_webform_default('number', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_number() {
  return array(
    'form_builder_type' => 'number',
    'properties' => array(
      'field_prefix' => array(
        'storage_parents' => array(
          'extra',
          'field_prefix',
        ),
      ),
      'field_suffix' => array(
        'storage_parents' => array(
          'extra',
          'field_suffix',
        ),
      ),
      // TODO: Add this option when the live preview can support it.

      //'number_type' => array(

      //  'form_parents' => array('display', 'type'),
      //  'storage_parents' => array('extra', 'type'),

      //),
      'min' => array(
        'form_parents' => array(
          'validation',
          'min',
        ),
        'storage_parents' => array(
          'extra',
          'min',
        ),
      ),
      'max' => array(
        'form_parents' => array(
          'validation',
          'max',
        ),
        'storage_parents' => array(
          'extra',
          'max',
        ),
      ),
      'step' => array(
        'form_parents' => array(
          'validation',
          'step',
        ),
        'storage_parents' => array(
          'extra',
          'step',
        ),
      ),
      'decimals' => array(
        'form_parents' => array(
          'display',
          'decimals',
        ),
        'storage_parents' => array(
          'extra',
          'decimals',
        ),
      ),
      'point' => array(
        'form_parents' => array(
          'display',
          'point',
        ),
        'storage_parents' => array(
          'extra',
          'point',
        ),
      ),
      'separator' => array(
        'form_parents' => array(
          'display',
          'separator',
        ),
        'storage_parents' => array(
          'extra',
          'separator',
        ),
      ),
      'integer' => array(
        'form_parents' => array(
          'validation',
          'integer',
        ),
        'storage_parents' => array(
          'extra',
          'integer',
        ),
      ),
      'excludezero' => array(
        'form_parents' => array(
          'analysis',
          'excludezero',
        ),
        'storage_parents' => array(
          'extra',
          'excludezero',
        ),
      ),
      'unique' => array(
        'storage_parents' => array(
          'extra',
          'unique',
        ),
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_properties_component().
 */
function _form_builder_webform_form_builder_properties_number() {
  return array(
    'unique' => array(
      'form' => 'form_builder_webform_property_unique_form',
    ),
  );
}

/**
 * @} End of "defgroup form-builder-webform-number-callbacks"
 */

/**
 * @defgroup form-builder-webform-pagebreak-callbacks Callbacks for the Pagebreak component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_pagebreak() {
  $fields = array();
  $component['name'] = t('Page break');
  $fields['pagebreak'] = array(
    'title' => t('Page break'),
    'default' => _form_builder_webform_default('pagebreak', array(), $component),
    'weight' => 20,
  );
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_save_component().
 *
 * @todo make this actually work.
 */
function _form_builder_webform_form_builder_save_pagebreak($component, $form_element) {

  // Ensure pagebreaks are saved at the root level.
  if ($component['pid'] !== 0) {
    drupal_set_message(t('Page breaks may not be nested inside fieldsets. Each pagebreak has been moved outside of fieldsets.'), 'status', FALSE);
    $parent = Loader::instance()
      ->fromCache('webform', $form_element['#form_builder']['form_id'])
      ->getElementArray($form_element['#form_builder']['parent_id']);
    $component['weight'] = $parent['#weight'] + 1;
    $component['pid'] = 0;
  }
  return $component;
}

/**
 * Implements _form_builder_webform_form_builder_preview_alter_component().
 */
function _form_builder_webform_form_builder_preview_alter_pagebreak($form_element) {

  // Convert the pagebreak element to a markup element before displaying it.
  $form_element['#type'] = 'markup';
  $form_element['#form_builder']['element_type'] = 'markup';
  unset($form_element['#theme']);

  // Add the 'webform_element' theme wrapper at the beginning, where it would
  // normally be.
  array_unshift($form_element['#theme_wrappers'], 'webform_element');

  // Display the title of the pagebreak field as regular markup.
  $form_element['#children'] = '--- ' . check_plain($form_element['#webform_component']['name']) . ' ---';
  $form_element['#children'] .= '<div class="description">' . t('(Fields below will be displayed on a new page)') . '</div>';
  $form_element['#title_display'] = 'none';

  // Give the element a wrapper class so that themers can recognize it
  // represents a pagebreak element.
  $form_element['#attributes']['class'][] = 'form-builder-preview-pagebreak-webform-element';
  array_unshift($form_element['#theme_wrappers'], 'container');
  return $form_element;
}

/**
 * @} End of "defgroup form-builder-webform-pagebreak-callbacks"
 */

/**
 * @defgroup form-builder-webform-select-callbacks Callbacks for the Select component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_select() {
  $fields = array();
  $fields['select'] = array(
    'title' => t('Select list'),
    'properties' => array(
      'default_value',
      'options',
      'multiple',
    ),
    'weight' => -5,
  );
  $items['items'] = "1|" . t('one') . "\n2|" . t('two') . "\n3|" . t('three');
  $fields['select']['default'] = _form_builder_webform_default('select', array(
    'aslist' => TRUE,
    'multiple' => FALSE,
    'value' => '1',
  ) + $items, array(
    'name' => t('New select'),
  ));
  $fields['checkboxes'] = array(
    'title' => t('Checkboxes'),
    'properties' => array(
      'default_value',
      'options',
      'multiple',
    ),
    'weight' => -6,
  );
  $fields['checkboxes']['default'] = _form_builder_webform_default('select', array(
    'aslist' => FALSE,
    'multiple' => TRUE,
    'value' => '1,2',
  ) + $items, array(
    'name' => t('New checkboxes'),
  ));
  $fields['radios'] = array(
    'title' => t('Radios'),
    'properties' => array(
      'default_value',
      'options',
    ),
    'weight' => -7,
  );
  $fields['radios']['default'] = _form_builder_webform_default('select', array(
    'aslist' => FALSE,
    'multiple' => FALSE,
    'value' => '1',
  ) + $items, array(
    'name' => t('New radios'),
  ));
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_select() {
  return array(
    'properties' => array(
      'key_type_toggled' => array(
        'storage_parents' => array(
          'extra',
          'custom_keys',
        ),
      ),
      'other_option' => array(
        'form_parents' => array(
          'items',
          'options',
          'option_settings',
          'other_option',
        ),
        'storage_parents' => array(
          'extra',
          'other_option',
        ),
      ),
      'other_text' => array(
        'form_parents' => array(
          'items',
          'options',
          'option_settings',
          'other_text',
        ),
        'storage_parents' => array(
          'extra',
          'other_text',
        ),
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_properties_component().
 */
function _form_builder_webform_form_builder_properties_select() {
  return array(
    'options' => array(
      'form' => 'form_builder_webform_property_select_options_form',
      'submit' => array(
        'form_builder_property_options_form_submit',
      ),
      'storage_parents' => array(
        'extra',
        'items',
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_load_component().
 */
function _form_builder_webform_form_builder_load_select($form_element) {

  // Properties that are only used by Form Builder and not in the normal form.
  $form_element['#key_type_toggled'] = !empty($form_element['#webform_component']['extra']['custom_keys']);
  return $form_element;
}

/**
 * Configuration form for the "options" property.
 */
function form_builder_webform_property_select_options_form(&$form_state, $form_type, $element) {

  // Use the default options form, but enhance to allow Webform tokens.
  $form = form_builder_property_options_form($form_state, $form_type, $element, 'options');
  $form['options']['#default_value_pattern'] = '^%.+\\[.+\\]$';
  return $form;
}

/**
 * @} End of "defgroup form-builder-webform-select-callbacks"
 */

/**
 * @defgroup form-builder-webform-textarea-callbacks Callbacks for the Textarea component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_textarea() {
  $fields = array();
  $fields['textarea'] = array(
    'title' => t('Textarea'),
    'weight' => -19,
  );
  $component['name'] = t('New textarea');
  $fields['textarea']['default'] = _form_builder_webform_default('textarea', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_textarea() {
  return array(
    'form_builder_type' => 'textarea',
    'properties' => array(
      'cols' => array(
        'storage_parents' => array(
          'extra',
          'cols',
        ),
      ),
      'rows' => array(
        'storage_parents' => array(
          'extra',
          'rows',
        ),
      ),
      'disabled' => array(
        'storage_parents' => array(
          'extra',
          'disabled',
        ),
      ),
    ),
  );
}

/**
 * @} End of "defgroup form-builder-webform-textarea-callbacks"
 */

/**
 * @defgroup form-builder-webform-textfield-callbacks Callbacks for the Textfield component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_textfield() {
  $fields = array();
  $fields['textfield'] = array(
    'title' => t('Textfield'),
    'weight' => -20,
  );
  $component['name'] = t('New textfield');
  $fields['textfield']['default'] = _form_builder_webform_default('textfield', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_textfield() {
  return array(
    'form_builder_type' => 'textfield',
    'properties' => array(
      'size' => array(
        'storage_parents' => array(
          'extra',
          'width',
        ),
      ),
      'maxlength' => array(
        'storage_parents' => array(
          'extra',
          'maxlength',
        ),
      ),
      'field_prefix' => array(
        'storage_parents' => array(
          'extra',
          'field_prefix',
        ),
      ),
      'field_suffix' => array(
        'storage_parents' => array(
          'extra',
          'field_suffix',
        ),
      ),
      'disabled' => array(
        'storage_parents' => array(
          'extra',
          'disabled',
        ),
      ),
      'unique' => array(
        'storage_parents' => array(
          'extra',
          'unique',
        ),
      ),
    ),
  );
}

/**
 * Implements _form_builder_webform_form_builder_properties_component().
 */
function _form_builder_webform_form_builder_properties_textfield() {
  return array(
    'unique' => array(
      'form' => 'form_builder_webform_property_unique_form',
    ),
  );
}

/**
 * @} End of "defgroup form-builder-webform-textfield-callbacks"
 */

/**
 * @defgroup form-builder-webform-time-callbacks Callbacks for the Time component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */
function _form_builder_webform_form_builder_types_time() {
  $fields = array();
  $fields['time'] = array(
    'title' => t('Time'),
    'weight' => -1,
  );
  $component['name'] = t('New time');
  $fields['time']['default'] = _form_builder_webform_default('time', array(), $component);
  return $fields;
}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */
function _form_builder_webform_form_builder_map_time() {
  return array(
    'form_builder_type' => 'time',
    'properties' => array(
      'timezone' => array(
        'form_parents' => array(
          'extra',
          'timezone',
        ),
        'storage_parents' => array(
          'extra',
          'timezone',
        ),
      ),
      'hourformat' => array(
        'form_parents' => array(
          'display',
          'hourformat',
        ),
        'storage_parents' => array(
          'extra',
          'hourformat',
        ),
      ),
    ),
  );
}

/**
 * @} End of "defgroup form-builder-webform-time-callbacks"
 */

/**
 * Helper function; Alter a form element upon load based on a map.
 */
function _form_builder_webform_set_mapped_type($element) {
  if ($map = _form_builder_webform_property_map($element['#webform_component']['type'])) {

    // Keep the internal type of this element as the component type. For example
    // this may match an $element['#type'] of 'webform_date' and set the
    // $element['#form_builder']['element_type'] to simply 'date'.
    if (isset($map['form_builder_type'])) {
      $element['#form_builder']['element_type'] = $map['form_builder_type'];
    }
  }
  return $element;
}

/**
 * Helper function; Generate a default FAPI element for any component type.
 */
function _form_builder_webform_default($component_type, $merge_extras = array(), $merge_component = array()) {
  module_load_include('inc', 'webform', 'includes/webform.components');
  $component = array(
    'type' => $component_type,
    'extra' => array(),
  );
  webform_component_defaults($component);
  $component['extra'] = array_merge($component['extra'], $merge_extras);
  $component = array_merge($component, $merge_component);
  $default_element['#webform_component'] = $component;

  // Call the loading function to make sure that the default element gets the
  // same treatment as an existing one.
  $default_element = _form_builder_webform_set_mapped_type($default_element);
  if ($element = form_builder_webform_component_invoke($component_type, 'form_builder_load', $default_element)) {
    $default_element = $element;
  }
  return $default_element;
}

/**
 * Helper function; Generate a configuration form based on a map.
 *
 * The returned form is derived from a subcomponent of the component form
 * provided by the Webform module.
 *
 * @param $component
 *   The webform component to be edited.
 * @param $edit
 *   The webform component edit form as produced by webform itself.
 * @param $property
 *   The property of $element which stores the state of portions of the webform
 *   component that this form is responsible for configuring. The property
 *   should be passed in without the leading "#".
 * @param array $map
 *   The mapping data for this webform component. Including:
 *   - form_parents: An array of nested keys representing the location of the
 *     subcomponent of the _webform_edit_[component]() form that this
 *     configuration form will be taken from. For example ['display', 'with'] to
 *     get $form['display']['with'] fromm _webform_edit_[component]().
 *   - property_group: Controls in which tab of the configuration form the
 *     property will be shown.
 *   - weight: Override the #weight of the property form element.
 *
 * @return array
 *   Part of the webform component edit form as specified in
 *   `$map['form_parents']` prepared for being used in a form_builder configure
 *   form.
 */
function _form_builder_webform_mapped_form($component, $edit, &$form_state, $property) {
  if (!(($component_type = $component['type']) && ($map = _form_builder_webform_property_map($component_type)))) {
    return [];
  }
  $map = $map['properties'][$property];
  $map += [
    'property_group' => reset($map['form_parents']),
  ];
  $form = drupal_array_get_nested_value($edit, $map['form_parents']);

  // Force the form to have a consistent #tree structure so it will appear in
  // $form_state['values'] the way we want.
  _form_builder_webform_force_tree($form);

  // Indicate the Form Builder property group that this form will be displayed
  // in.
  if (!empty($map['property_group'])) {
    $form['#form_builder']['property_group'] = $map['property_group'];
  }

  // Allow overriding the weight via the map.
  if (isset($map['weight'])) {
    $form['#weight'] = $map['weight'];
  }

  // Return the form, keyed by the name of the property that is being
  // configured.
  return [
    $property => $form,
  ];
}

/**
 * Helper function; replaces custom form #parents with a #tree structure.
 *
 * This is a helper function to force #tree = TRUE on all parts of a form,
 * regardless of any custom #parents that were originally defined as part of
 * the form. It is used to ensure a consistent structure within
 * $form_state['values'] when the form is submitted.
 *
 * @param $form
 *   The form array to modify.
 */
function _form_builder_webform_force_tree(&$form) {
  unset($form['#parents']);
  $form['#tree'] = TRUE;
  foreach (element_children($form) as $key) {
    _form_builder_webform_force_tree($form[$key]);
  }
}

Functions

Namesort descending Description
form_builder_webform_property_grid_form_submit Submit handler for the "grid_options" and "grid_questions" properties.
form_builder_webform_property_select_options_form Configuration form for the "options" property.
form_builder_webform_property_unique_form Configuration form for the "unique" property.
_form_builder_webform_default Helper function; Generate a default FAPI element for any component type.
_form_builder_webform_force_tree Helper function; replaces custom form #parents with a #tree structure.
_form_builder_webform_form_builder_load_select Implements _form_builder_webform_form_builder_load_component().
_form_builder_webform_form_builder_map_date Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_email Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_fieldset Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_file Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_grid Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_markup Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_number Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_select Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_textarea Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_textfield Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_map_time Implements _form_builder_webform_form_builder_map_component().
_form_builder_webform_form_builder_preview_alter_file Implements _form_builder_webform_form_builder_preview_alter_component().
_form_builder_webform_form_builder_preview_alter_hidden Implements _form_builder_webform_form_builder_preview_alter_component().
_form_builder_webform_form_builder_preview_alter_markup Implements _form_builder_webform_form_builder_preview_alter_component().
_form_builder_webform_form_builder_preview_alter_pagebreak Implements _form_builder_webform_form_builder_preview_alter_component().
_form_builder_webform_form_builder_properties_email Implements _form_builder_webform_form_builder_properties_component().
_form_builder_webform_form_builder_properties_grid Implements _form_builder_webform_form_builder_properties_component().
_form_builder_webform_form_builder_properties_number Implements _form_builder_webform_form_builder_properties_component().
_form_builder_webform_form_builder_properties_select Implements _form_builder_webform_form_builder_properties_component().
_form_builder_webform_form_builder_properties_textfield Implements _form_builder_webform_form_builder_properties_component().
_form_builder_webform_form_builder_save_pagebreak Implements _form_builder_webform_form_builder_save_component().
_form_builder_webform_form_builder_types_date Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_email Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_fieldset Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_file Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_grid Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_hidden Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_markup Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_number Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_pagebreak Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_select Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_textarea Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_textfield Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_form_builder_types_time Implements _form_builder_webform_form_builder_types_component().
_form_builder_webform_mapped_form Helper function; Generate a configuration form based on a map.
_form_builder_webform_set_mapped_type Helper function; Alter a form element upon load based on a map.