You are here

function _fc_form_field_ui_field_edit_form_alter in Field Complete 7

Implements hook_field_widget_settings_form().

This function adds the checkbox to every field to determine whether it should be used in the completeness calculation. It is set differently for every field instance.

1 call to _fc_form_field_ui_field_edit_form_alter()
fc_form_field_ui_field_edit_form_alter in ./fc.module
Implements hook_field_widget_settings_form().

File

./fc.form.inc, line 14
Field Complete - Provides field-based completeness for any entity - admin.

Code

function _fc_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  $settings = $form['#instance']['settings'];
  if (!empty($form['instance']['required'])) {
    $form['instance']['required'] += array(
      '#states' => array(
        // Hide the "required" field when the 'Field Complete' checkbox is enabled.
        'invisible' => array(
          ':input[name="instance[settings][fc][fc_include]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  $form['instance']['settings']['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.'),
  );
  $form['instance']['settings']['fc']['fc_include'] = array(
    '#type' => 'checkbox',
    '#title' => t('Include in field completeness'),
    '#description' => t('If checked, this field instance will be included in the completeness calculation for its parent entity.'),
    '#default_value' => !empty($settings['fc']['fc_include']),
    '#states' => array(
      // Only show this field when the 'fc_include' checkbox is enabled.
      'invisible' => array(
        ':input[name="instance[required]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['instance']['settings']['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 this field when the 'fc_include' checkbox is enabled.
      'visible' => array(
        ':input[name="instance[required]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Choose the right plugin for the field type.
  $plugin = fc_get_plugin($form['#field']['type']);
  if (!empty($plugin['zero can be empty'])) {
    $form['instance']['settings']['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']['fc_check_zero']),
      '#states' => array(
        // Only show this field when the 'fc_include' checkbox is enabled.
        'visible' => array(
          ':input[name="instance[settings][fc][fc_include]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  if (!empty($plugin['can be empty'])) {
    $form['instance']['settings']['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']['fc_allow_empty']),
      '#states' => array(
        // Only show this field when the 'fc_include' checkbox is enabled.
        'visible' => array(
          ':input[name="instance[settings][fc][fc_include]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  else {
    $form['instance']['settings']['fc']['fc_allow_empty'] = array(
      '#type' => 'value',
      '#value' => FALSE,
    );
  }

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