You are here

function unique_field_node_settings_form in Unique field 6

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. 7 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
Implementation of hook_form_alter().

File

./unique_field.module, line 393
Provides content validation requirement that a node's title, author, language, taxonomy terms, or CCK 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_get_types('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 (module_exists('content') && !empty($ntype)) {
    $ctype = content_types($ntype);
    if (is_array($ctype) && is_array($ctype['fields'])) {
      foreach ($ctype['fields'] as $field => $info) {
        $fieldopts[$field] = $info['widget']['label'] . ' (' . $field . ')';
      }
    }
  }
  if (module_exists('taxonomy') && !empty($ntype)) {
    $vocabs = taxonomy_get_vocabularies($ntype);
    if (is_array($vocabs)) {
      foreach ($vocabs as $vocab) {
        $fieldopts[UNIQUE_FIELD_FIELDS_TAXONOMY . $vocab->vid] = t('Taxonomy terms from @vocab-name', array(
          '@vocab-name' => $vocab->name,
        ));
      }
    }
  }

  // build the form
  $form['unique_field'] = array(
    '#type' => 'fieldset',
    '#title' => t('Unique field settings'),
    '#weight' => 1,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $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';
}