You are here

function field_validation_ui_manage_rule in Field Validation 7.2

Callback function to add or edit a validation rule.

2 string references to 'field_validation_ui_manage_rule'
field_validation_ui_callback_dispatch in ./field_validation_ui.admin.inc
Menu callback function using to dispatch.
field_validation_ui_menu in ./field_validation_ui.module
Implements hook_menu().

File

./field_validation_ui.admin.inc, line 66
Manages validation rules administration UI.

Code

function field_validation_ui_manage_rule($form, $form_state, $instance, $action = 'add', $validator = 'regex', $ruleid = NULL) {

  // $form = array();
  // $rule_validator = field_validation_get_validator_info($validator);
  $rule_validator = array();
  if ($action == 'overwrite') {
    $rule = field_validation_ui_rule_load($ruleid);
  }
  elseif ($action == 'edit') {
    $rule = field_validation_ui_get_rule($ruleid);
  }
  else {
    $rule = new stdClass();
  }
  $form_state['item'] = $rule;
  $form['validator'] = array(
    '#type' => 'hidden',
    '#value' => $validator,
  );
  $form['action'] = array(
    '#type' => 'hidden',
    '#value' => $action,
  );
  if ($action == 'edit' && $rule) {
    $form['ruleid'] = array(
      '#type' => 'hidden',
      '#value' => $rule->ruleid,
    );
    $form['field_name'] = array(
      '#type' => 'hidden',
      '#value' => $rule->field_name,
    );
    $form['entity_type'] = array(
      '#type' => 'hidden',
      '#value' => $rule->entity_type,
    );
    $form['bundle'] = array(
      '#type' => 'hidden',
      '#value' => $rule->bundle,
    );
  }
  else {
    $form['field_name'] = array(
      '#type' => 'hidden',
      '#value' => $instance['field_name'],
    );
    $form['entity_type'] = array(
      '#type' => 'hidden',
      '#value' => $instance['entity_type'],
    );
    $form['bundle'] = array(
      '#type' => 'hidden',
      '#value' => $instance['bundle'],
    );
  }
  $form['rulename'] = array(
    '#type' => 'textfield',
    '#title' => t('Rule name'),
    '#default_value' => isset($rule->rulename) ? $rule->rulename : '',
    '#required' => TRUE,
    '#size' => 60,
    '#maxlength' => 255,
    '#weight' => 1,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => isset($rule->name) ? $rule->name : '',
    '#machine_name' => array(
      'exists' => 'field_validation_ui_rule_load',
      'source' => array(
        'rulename',
      ),
    ),
    '#weight' => 2,
    '#maxlength' => 32,
  );
  $field = field_info_field($instance['field_name']);
  $col_options = array(
    '' => t('Choose a column'),
  );
  $columns = !empty($field['columns']) ? $field['columns'] : array();
  foreach ($columns as $key => $column) {
    $col_options[$key] = $key;
  }

  // Support free tagging.
  if ($field['type'] == 'taxonomy_term_reference') {
    $col_options['name'] = 'name';
  }
  $form['col'] = array(
    '#type' => 'select',
    '#options' => $col_options,
    '#title' => t('Column'),
    '#description' => t('A column defined in the hook_field_schema() of this field.'),
    '#default_value' => isset($rule->col) ? $rule->col : '',
    '#required' => TRUE,
    '#weight' => 3,
  );
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#weight' => 4,
  );
  ctools_include('plugins');
  $plugin = ctools_get_plugins('field_validation', 'validator', $validator);
  $class = ctools_plugin_get_class($plugin, 'handler');
  $entity_type = $instance['entity_type'];
  $validator_class = new $class($entity_type);
  $validator_class
    ->settings_form($form, $form_state);
  $form['error_message'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom error message'),
    '#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
    '#required' => TRUE,
    '#size' => 60,
    '#maxlength' => 255,
    '#default_value' => isset($rule->error_message) ? $rule->error_message : '',
    '#weight' => 5,
  );
  $output = '<p>' . t('The following tokens are available for error message.' . '</p>');
  $token_help = $validator_class
    ->token_help();
  if (!empty($token_help)) {
    $items = array();
    foreach ($token_help as $key => $value) {
      $items[] = $key . ' == ' . $value;
    }
    $output .= theme('item_list', array(
      'items' => $items,
    ));
  }
  $form['token_help'] = array(
    '#type' => 'fieldset',
    '#title' => t('Replacement patterns'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#value' => $output,
    '#id' => 'error-message-token-help',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#weight' => 6,
  );
  if (module_exists("token")) {
    $mappings = token_get_entity_mapping('entity');

    // Print debug($mappings);
    $token_type = isset($mappings[$instance['entity_type']]) ? $mappings[$instance['entity_type']] : "";
    $form['token_help']['tokens'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        $token_type,
      ),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => isset($rule->ruleid) ? t('Save rule') : t('Add rule'),
    '#weight' => 25,
  );
  if ($action == 'overwrite') {
    $form['submit']['#value'] = t('Overwrite rule');
  }
  return $form;
}