' . t('About') . ''; $output .= '
' . t('Disable autocomplete for TypedRelation fields') . '
'; return $output; default: } } /** * Implements hook_form_alter(). */ function disable_field_autocomplete_form_node_islandora_object_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { $form['#validate'][] = 'disable_field_autocomplete_node_edit_validate'; } /** * Implements hook_form_alter(). */ function disable_field_autocomplete_form_node_islandora_object_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { $form['#validate'][] = 'disable_field_autocomplete_node_edit_validate'; } /** * Extra Islandora Ojbect node validation required due to lacking autocomplete. * @param $form * @param \Drupal\Core\Form\FormStateInterface $form_state * @return void */ function disable_field_autocomplete_node_edit_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { // TODO: discover fields configured to use the disable_field_autocomplete widget instead of hardcoding. disable_field_autocomplete_validate_fields('field_contributors', $form, $form_state); disable_field_autocomplete_validate_fields('field_host_contributors', $form, $form_state); disable_field_autocomplete_validate_fields('field_series_contributors', $form, $form_state); } /** * Do the validation. * @param $field_id * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state * @return void */ function disable_field_autocomplete_validate_fields($field_id, $form, \Drupal\Core\Form\FormStateInterface $form_state) { $contributor_count = 0; $contributors = &$form_state->getValue($field_id); if(empty($contributors)) { return; } foreach ($contributors as &$contributor) { if (is_array($contributor) && isset($contributor['target_id']) && is_array($contributor['target_id'])) { $entity = ($contributor['target_id']['entity']); $name = $entity->getName(); if (isset($name)) { try { $term_id = disable_field_autocomplete_get_term_id($name); if (isset($term_id) && $term_id > 0) { $contributor['target_id'] = $term_id; } } catch (Exception $e) { $form_state->setError($form[$field_id]['widget'][$contributor_count], t('The entered Term %name is not unique across Vocabularies. You will need to include the term id in brackets. For Example "%name (119)", where 119 is the term id.', array('%name' => $name))); } } } $contributor_count++; } } /** * Checks for existing terms. * Since autocommplete is off we need to check ourselves. * @param $term_name * @return int * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ function disable_field_autocomplete_get_term_id($term_name) { $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); $terms = $storage->loadByProperties([ 'name' => $term_name, 'vid' => ['person', 'corporate_body', 'event', 'family'], ]); if ($terms) { if (count ($terms) > 1) { throw new Exception ('The entered Term is not unique across Vocabularies. You will need to include the term id in brackets. For Example "term value (119)", where 119 is the term id.'); } $term = reset($terms); } if(!isset($term)) { return -1; } $tid = $term->id(); return $tid; }