function unique_field_form_alter in Unique field 8.2
Same name and namespace in other branches
- 8 unique_field.module \unique_field_form_alter()
- 5 unique_field.module \unique_field_form_alter()
- 6 unique_field.module \unique_field_form_alter()
- 7 unique_field.module \unique_field_form_alter()
Hook_form_BASE_FORM_ID_alter().
File
- ./
unique_field.module, line 57
Code
function unique_field_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$bundles = entity_get_bundles('node');
$bundle_id = array();
foreach ($bundles as $key => $value) {
$bundle_id['node_' . $key . '_form'] = $key;
$bundle_id['node_' . $key . '_add_form'] = $key;
$bundle_id['node_' . $key . '_edit_form'] = $key;
}
// Call form validate function for nodes.
if (in_array($form_id, array_keys($bundle_id))) {
$form['#validate'][] = 'unique_field_form_validate';
$form['bundle'] = $bundle_id[$form_id];
$form['unique_field_override'] = array(
'#type' => 'hidden',
'#default_value' => '0',
);
}
$taxonomy_vocabularies = entity_get_bundles('taxonomy_term');
$taxonomy_term_form_id = array();
foreach ($taxonomy_vocabularies as $key => $value) {
$taxonomy_term_form_id['taxonomy_term_' . $key . '_form'] = $key;
}
// Call form validate function for taxonomy terms.
if (in_array($form_id, array_keys($taxonomy_term_form_id))) {
$form['#validate'][] = 'unique_field_taxonomy_term_form_validate';
$form['bundle'] = $taxonomy_term_form_id[$form_id];
$form['unique_field_override'] = array(
'#type' => 'hidden',
'#default_value' => '0',
);
}
// Call form validate function for user registration.
if ($form_id == 'user_register_form' || $form_id == 'user_form') {
$form['#validate'][] = 'unique_field_user_form_validate';
$form['bundle'] = 'user';
$form['unique_field_override'] = array(
'#type' => 'hidden',
'#default_value' => '0',
);
}
// Get the current user.
$user = \Drupal::currentUser();
// Load default values.
$config = \Drupal::configFactory()
->get('unique_field.settings');
// Add unique field settings form.
if (($form_id == 'node_type_add_form' || $form_id == 'node_type_edit_form') && isset($form['type']) && $user
->hasPermission('unique_field_perm_admin')) {
// Load fields for content type.
$type = $form_state
->getFormObject()
->getEntity();
$ntype = $type
->id();
$fieldopts = array();
if (\Drupal::moduleHandler()
->moduleExists('language') && !empty($ntype) && \Drupal::languageManager()
->getCurrentLanguage()
->getId()) {
$fieldopts[UNIQUE_FIELD_FIELDS_LANGUAGE] = t('Language');
}
if (!empty($ntype)) {
$entity_type_id = 'node';
$bundle = $ntype;
foreach (\Drupal::entityManager()
->getFieldDefinitions($entity_type_id, $bundle) as $field_name => $field_definition) {
if (!empty($field_definition
->getTargetBundle())) {
$fieldopts[$field_name] = $field_definition
->getLabel() . '(' . $field_name . ')';
}
}
}
foreach ($fieldopts as $key => $value) {
if ($key === "status" || $key === "promote") {
unset($fieldopts[$key]);
}
}
// Buid the unique field form.
$form['unique_field'] = array(
'#title' => t('Unique Field restrictions'),
'#type' => 'details',
'#group' => 'additional_settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 1,
);
$form['unique_field']['unique_field_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose the fields that should be unique'),
'#options' => $fieldopts,
'#default_value' => !empty($ntype) ? $config
->get('unique_field_settings.' . $ntype . '.fields') : 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) ? $config
->get('unique_field_settings.' . $ntype . '.scope') : 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) ? $config
->get('unique_field_settings.' . $ntype . '.comp') : 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.'),
);
// Add validation function in submit handler.
$form['actions']['submit']['#submit'][] = 'unique_field_form_node_type_form_submit';
}
// Unique field for taxonomy terms.
if ($form_id == 'taxonomy_vocabulary_form' && $user
->hasPermission('unique_field_perm_admin')) {
$type = $form_state
->getFormObject()
->getEntity();
$taxonomy_vocabulary_name = $type
->id();
$fieldopts = array();
$fieldopts['name'] = t('Term Name');
$fieldopts['description'] = t('Term Description');
if (!empty($taxonomy_vocabulary_name)) {
$entity_type_id = 'taxonomy_term';
foreach (\Drupal::entityManager()
->getFieldDefinitions($entity_type_id, $taxonomy_vocabulary_name) as $field_name => $field_definition) {
if (!empty($field_definition
->getTargetBundle())) {
$fieldopts[$field_name] = $field_definition
->getLabel() . '(' . $field_name . ')';
}
}
}
// Buid the unique field form.
$form['unique_field_taxonomy'] = array(
'#title' => t('Unique Field restrictions'),
'#type' => 'details',
'#group' => 'additional_settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 1,
);
$form['unique_field_taxonomy']['unique_field_taxonomy_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose the fields that should be unique'),
'#options' => $fieldopts,
'#default_value' => !empty($taxonomy_vocabulary_name) ? $config
->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.fields') : 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 term or update an existing one if it contains values in the designated fields that duplicate others.'),
);
$form['unique_field_taxonomy']['unique_field_taxonomy_scope'] = array(
'#type' => 'radios',
'#title' => t('Choose the scope for the unique values'),
'#options' => array(
UNIQUE_FIELD_SCOPE_VOCABULARY => t('Vocabulary'),
UNIQUE_FIELD_SCOPE_LANGUAGE => t('Language'),
UNIQUE_FIELD_SCOPE_ALL => t('All Vocabularies'),
UNIQUE_FIELD_SCOPE_TERM => t('Single Term only'),
),
'#default_value' => !empty($taxonomy_vocabulary_name) ? $config
->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.scope') : UNIQUE_FIELD_SCOPE_VOCABULARY,
'#description' => t('Choose whether the values in the specified fields must be unique among terms of this Vocabulary, among terms of the same language, among all terms, or only among the fields of the present term.'),
);
$form['unique_field_taxonomy']['unique_field_taxonomy_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($taxonomy_vocabulary_name) ? $config
->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.comp') : 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 term, 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.'),
);
// Add validation function in submit handler.
$form['actions']['submit']['#submit'][] = 'unique_field_form_taxonomy_term_form_submit';
}
// Unique field form for User fields.
if ($form_id == 'user_admin_settings') {
$bundles = entity_get_bundles('user');
$fieldoptions = array();
$config = \Drupal::configFactory()
->get('unique_field.settings');
foreach (\Drupal::entityManager()
->getFieldDefinitions('user', 'user') as $field_name => $field_definition) {
if (!empty($field_definition
->getTargetBundle())) {
$fieldoptions[$field_name] = $field_definition
->getLabel() . '(' . $field_name . ')';
}
}
// Buid the unique field form.
$form['unique_field_user'] = array(
'#title' => t('Unique Field restrictions'),
'#type' => 'details',
'#group' => 'additional_settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 1,
);
$form['unique_field_user']['unique_field_user_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose the fields that should be unique'),
'#options' => $fieldoptions,
'#default_value' => !empty($config
->get('unique_field_user.fields')) ? $config
->get('unique_field_user.fields') : array(),
'#description' => t('After designating that certain fields should be unique, users will not be able to submit the User form to create a new user or update an existing one if it contains values in the designated fields that duplicate others.'),
);
$form['unique_field_user']['unique_field_user_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($config
->get('unique_field_user.comp')) ? $config
->get('unique_field_user.comp') : 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.'),
);
// Add validation function in submit handler.
$form['actions']['submit']['#submit'][] = 'unique_field_form_user_type_form_submit';
}
}