You are here

function unique_field_node_settings_form in Unique field 7

Same name and namespace in other branches
  1. 8 unique_field.module \unique_field_node_settings_form()
  2. 5 unique_field.module \unique_field_node_settings_form()
  3. 6 unique_field.module \unique_field_node_settings_form()

Add the unique field settings form to content type forms (node_type_form).

1 call to unique_field_node_settings_form()
unique_field_form_alter in ./unique_field.module
Implements hook_form_alter().

File

./unique_field.module, line 315
Provides content validation requirement that a node's author, language, taxonomy terms, or other fields are unique.

Code

function unique_field_node_settings_form(&$form) {

  // load fields for content type
  $ntype = $form['#node_type']->type;
  $nodetype = !empty($ntype) ? node_type_get_type($ntype) : FALSE;
  $fieldopts = array();
  $fieldopts[UNIQUE_FIELD_FIELDS_TITLE] = $nodetype && !empty($nodetype->title_label) ? check_plain($nodetype->title_label) . ' (' . t('title') . ')' : t('Title');
  $fieldopts[UNIQUE_FIELD_FIELDS_AUTHOR] = t('Author');
  if (module_exists('locale') && !empty($ntype) && variable_get('language_content_type_' . $ntype, 0)) {
    $fieldopts[UNIQUE_FIELD_FIELDS_LANGUAGE] = t('Language');
  }
  if (!empty($ntype)) {
    $fields = field_info_instances('node', $ntype);
    foreach ($fields as $fieldname => $info) {
      $fieldopts[$fieldname] = $info['label'] . ' (' . $fieldname . ')';
    }
  }

  // build the form
  $form['unique_field'] = array(
    '#type' => 'fieldset',
    '#title' => t('Unique Field restrictions'),
    '#weight' => 1,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
  );
  $form['unique_field']['unique_field_fields'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Choose the fields that should be unique'),
    '#options' => $fieldopts,
    '#default_value' => !empty($ntype) ? variable_get('unique_field_fields_' . $ntype, array()) : array(),
    '#description' => t('After designating that certain fields should be unique, users will not be able to submit the content form to create a new node or update an existing one if it contains values in the designated fields that duplicate others.'),
  );
  $form['unique_field']['unique_field_scope'] = array(
    '#type' => 'radios',
    '#title' => t('Choose the scope for the unique values'),
    '#options' => array(
      UNIQUE_FIELD_SCOPE_TYPE => t('Content type'),
      UNIQUE_FIELD_SCOPE_LANGUAGE => t('Language'),
      UNIQUE_FIELD_SCOPE_ALL => t('All nodes'),
      UNIQUE_FIELD_SCOPE_NODE => t('Single node only'),
    ),
    '#default_value' => !empty($ntype) ? variable_get('unique_field_scope_' . $ntype, UNIQUE_FIELD_SCOPE_TYPE) : UNIQUE_FIELD_SCOPE_TYPE,
    '#description' => t('Choose whether the values in the specified fields must be unique among nodes of this content type, among nodes of the same language, among all nodes, or only among the fields of the present node.'),
  );
  $form['unique_field']['unique_field_comp'] = array(
    '#type' => 'radios',
    '#title' => t('Choose whether values must be unique individually or in combination'),
    '#options' => array(
      UNIQUE_FIELD_COMP_EACH => t('Each of the specified fields must have a unique value'),
      UNIQUE_FIELD_COMP_ALL => t('The combination of values from the specified fields must be unique'),
    ),
    '#default_value' => !empty($ntype) ? variable_get('unique_field_comp_' . $ntype, UNIQUE_FIELD_COMP_EACH) : UNIQUE_FIELD_COMP_EACH,
    '#description' => t('For example, if you have fields for the parts of a street address (street number and name, city, and zip code) on a node, and want to allow only one node per complete address, but not only one node per city or per zip code, then you would want to choose that the fields must be unique in combination.'),
  );
  $form['unique_field']['unique_field_show_matches'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Check this box to show which nodes match when duplicate values are found'),
    '#options' => array(
      UNIQUE_FIELD_SHOW_MATCHES => t('Enabled'),
    ),
    '#default_value' => !empty($ntype) ? variable_get('unique_field_show_matches_' . $ntype, array()) : array(),
  );

  // add validation function
  $form['#validate'][] = 'unique_field_node_settings_form_validate';
}