You are here

function flexiform_webform_form_flexiform_field_configure_form_alter in Flexiform 7

Implements hook_form_flexiform_field_configure_form_alter().

File

flexiform_webform/flexiform_webform.module, line 357
Module provides simple webform functionality.

Code

function flexiform_webform_form_flexiform_field_configure_form_alter(&$form, &$form_state) {

  // Do nothing if the fc module is not enabled.
  if (!module_exists('fc')) {
    return;
  }

  // Work out whether this form is being used as a completeness handler.
  // We assume that it is possible to use any form for fc if that entity type
  // is using a child of the flexiformFCHandler.
  $flexiform = $form['#flexiform'];
  $base_entity_info = entity_get_info($flexiform->base_entity);
  if (empty($base_entity_info['fc handler class']) || !is_subclass_of($base_entity_info['fc handler class'], 'FlexiformFCHandler') && $base_entity_info['fc handler class'] != 'FlexiformFCHandler') {
    return;
  }

  // Currently only support field api elements.
  $element = $form['#flexiform_element'];
  if (!$element instanceof FlexiformElementField) {
    return;
  }
  $instance = $element
    ->getInstance();
  $field = $element
    ->getField();
  $settings = $instance['settings']['fc'];
  $form['fc'] = array(
    '#type' => 'fieldset',
    '#title' => t('Field Complete'),
    '#description' => t('Select whether this field instance should be included in completeness for its parent entity, plus any additional settings for this field type.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $form['fc']['fc_include'] = array(
    '#type' => 'select',
    '#title' => t('Include in field completeness'),
    '#options' => array(
      'always' => t('Always'),
      'entity_exists' => t('If an Entity Exists'),
      'element_complete' => t('If another element is complete'),
      'never' => t('Never'),
    ),
    '#default_value' => !empty($settings['fc_include']) ? $settings['fc_include'] : 'never',
    '#states' => array(
      // Don't show if required is ticked.
      'invisible' => array(
        ':input[name="instance[required]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['fc']['fc_dummy'] = array(
    '#type' => 'item',
    '#title' => t('Field Complete cannot be selected when this field is set to be "Required".'),
    '#states' => array(
      // Only show if required is ticked.
      'visible' => array(
        ':input[name="instance[required]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Entity Options
  $options = array();
  foreach ($flexiform->entities as $namespace => $entInfo) {
    $options[$namespace] = $entInfo['label'];
  }
  $form['fc']['fc_include__entity_exists'] = array(
    '#type' => 'select',
    '#title' => t('Entity'),
    '#description' => t('The entity that must exist for this element to be included in field complete.'),
    '#options' => $options,
    '#default_value' => !empty($settings['fc_include__entity_exists']) ? $settings['fc_include__entity_exists'] : NULL,
    '#states' => array(
      'visible' => array(
        ':input[name="fc[fc_include]"]' => array(
          'value' => 'entity_exists',
        ),
      ),
    ),
  );

  // Element options.
  $elOptions = array();
  foreach ($flexiform->elements as $namespace => $elInfo) {
    if (empty($elInfo['field_name'])) {
      continue;
    }
    if ($namespace == $element
      ->getElementNamespace()) {
      continue;
    }
    $elOptions[$namespace] = $elInfo['label'];
  }
  $form['fc']['fc_include__element_complete'] = array(
    '#type' => 'select',
    '#title' => t('Element'),
    '#description' => t('The element to check for completeness.'),
    '#options' => $elOptions,
    '#default_value' => !empty($settings['fc_include__element_complete']) ? $settings['fc_include__element_complete'] : NULL,
    '#states' => array(
      'visible' => array(
        ':input[name="fc[fc_include]"]' => array(
          'value' => 'element_complete',
        ),
      ),
    ),
  );
  $form['fc']['fc_include__element_complete_if_ignored'] = array(
    '#type' => 'select',
    '#title' => t('If the required element is not included:'),
    '#description' => t('Sometimes the element that is depended on may be ignored by the completeness checker and so the completeness of that element will not be calculated. In this scenario should this element be included or excluded.'),
    '#options' => array(
      'include' => t('Include'),
      'exclude' => t('Exclude'),
    ),
    '#default_value' => !empty($settings['fc_include__element_complete_if_ignored']) ? $settings['fc_include__element_complete_if_ignored'] : NULL,
    '#states' => array(
      'visible' => array(
        ':input[name="fc[fc_include]"]' => array(
          'value' => 'element_complete',
        ),
      ),
    ),
  );

  // Choose the right plugin for the field type.
  $plugin = fc_get_plugin($field['type']);
  if (!empty($plugin['zero can be empty'])) {
    $form['fc']['fc_check_zero'] = array(
      '#type' => 'checkbox',
      '#title' => t('Treat a string zero as empty'),
      '#description' => t('In some instances you may want a "0" to be the equivalent of empty. This only works for fields which uses the "value" field.'),
      '#default_value' => !empty($settings['fc_check_zero']),
      '#states' => array(
        // Only show this field when the 'fc_include' checkbox is enabled.
        'visible' => array(
          ':input[name="fc[fc_include]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  if (!empty($plugin['can be empty'])) {
    $form['fc']['fc_allow_empty'] = array(
      '#type' => 'checkbox',
      '#title' => t('Mark as complete if empty'),
      '#description' => t('Permit complex fields (like entityreference and field_collection) to be marked as complete even if they are empty. So only check completeness if there is something to check.'),
      '#default_value' => !empty($settings['fc_allow_empty']),
      '#states' => array(
        // Only show this field when the 'fc_include' checkbox is enabled.
        'visible' => array(
          ':input[name="fc[fc_include]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  else {
    $form['fc']['fc_allow_empty'] = array(
      '#type' => 'value',
      '#value' => FALSE,
    );
  }

  // Add any special fields
  foreach ($plugin['field form'] as $setting => $element) {
    $form['fc'][$setting] = $element;
    $form['fc'][$setting] += array(
      '#default_value' => !empty($settings[$setting]) ? $settings[$setting] : NULL,
      '#states' => array(
        // Only show this field when the 'fc_include' checkbox is enabled.
        'visible' => array(
          ':input[name="fc[fc_include]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  $form['#submit'][] = 'flexiform_webform_form_flexiform_field_configure_form_fc_submit';
}