You are here

function conditional_fields_content_admin_field_validate in Conditional Fields 5

Same name and namespace in other branches
  1. 6.2 conditional_fields.module \conditional_fields_content_admin_field_validate()
  2. 6 conditional_fields.module \conditional_fields_content_admin_field_validate()

Check selection of values

File

./conditional_fields.module, line 386

Code

function conditional_fields_content_admin_field_validate($form_id, $form_values, $form, $controlled_fields) {

  // Allowed fields checked here are:
  // - If field is in a group: fields in the same group
  // - If field is not in a group: fields not in a group
  // We check both controlling (available) and controlled fields
  $conditional_fields = array_merge((array) $form_values['conditional_fields'], $controlled_fields);
  foreach ($conditional_fields as $available_field => $trigger_values) {

    // Disallow selecting Not set and values at the same time
    if ($trigger_values['conditional_field_no_value'] && count($trigger_values) > 1) {
      form_error($form['widget']['conditional_fields'][$available_field], t("You cannot select 'Not set' and other values at the same time."));
    }
    else {
      if (module_exists('fieldgroup') && !$trigger_values['conditional_field_no_value']) {
        if ($group = $form_values['group']) {

          // If the field is in a group
          // If the available field IS a group, disallow it
          if (substr($available_field, 0, 6) == 'group_') {
            form_error($form, t("This field is controlling the group %field. You can only control a group from a field not in a group.", array(
              '%field' => $available_field,
            )));
          }
          else {
            if ($group != fieldgroup_get_group($form_values['type_name'], $available_field)) {
              form_error($form['widget']['conditional_fields'][$available_field], t("The field %field is not in the same group that you selected (%group). You can only control a field in a group from another field in the same group.", array(
                '%field' => $available_field,
                '%group' => $group,
              )));
            }
          }
        }
        else {

          // If the field is in not in a group
          // If the available field is in a group, disallow it.
          if ($available_field_group = fieldgroup_get_group($form_values['type_name'], $available_field)) {
            form_error($form['widget']['conditional_fields'][$available_field], t("The field %field is in the group %group. You can only control a field in a group from another field in the same group.", array(
              '%field' => $available_field,
              '%group' => $available_field_group,
            )));
          }
        }
      }
    }
  }

  // Warn user on allowed values change
  $controlling_count = count($controlled_fields, 1);
  if ($controlling_count > 2 && $form_values['allowed_values'] != $form['field']['allowed_values']['#default_value']) {
    drupal_set_message(t('If you removed one or more allowed value from the field, you probably have to edit its controlled Conditional fields and/or fieldgroups settings.'), 'error');
  }
}