From 1f6db82f12d34ecb68110a29a9b2663cb181f46b Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Mon, 28 Oct 2024 14:56:35 -0300 Subject: [PATCH] added form validation function --- disable_field_autocomplete.module | 74 ++++++++++++++++++- .../FieldWidget/DisableAutocompleteWidget.php | 4 +- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/disable_field_autocomplete.module b/disable_field_autocomplete.module index 2b94366..2109c48 100644 --- a/disable_field_autocomplete.module +++ b/disable_field_autocomplete.module @@ -6,19 +6,87 @@ */ use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\taxonomy\Entity\Term; /** * Implements hook_help(). */ -function doi_field_formatter_help($route_name, RouteMatchInterface $route_match) { +function disable_field_autocomplete_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { - // Main module help for the facet_field_formatter module. case 'help.page.facet_field_formatter': $output = ''; $output .= '

' . t('About') . '

'; - $output .= '

' . t('Format string and text fields as doi links') . '

'; + $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'; +} + + +/** + * 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){ + $contributors = &$form_state->getValue('field_linked_agent'); + $contributor_count = 0; + 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_linked_agent']['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++; + } + return; +} + +/** + * 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', 'Contributors', 'Host Contributors', 'Series Contributors'], + ]); + + 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; +} diff --git a/src/Plugin/Field/FieldWidget/DisableAutocompleteWidget.php b/src/Plugin/Field/FieldWidget/DisableAutocompleteWidget.php index eb3e29b..1f9697d 100644 --- a/src/Plugin/Field/FieldWidget/DisableAutocompleteWidget.php +++ b/src/Plugin/Field/FieldWidget/DisableAutocompleteWidget.php @@ -5,6 +5,7 @@ namespace Drupal\disable_field_autocomplete\Plugin\Field\FieldWidget; use Drupal\controlled_access_terms\Plugin\Field\FieldWidget\TypedRelationWidget; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\taxonomy\Entity\Term; /** * Plugin implementation of the 'chosen_select' widget. @@ -24,9 +25,8 @@ class DisableAutocompleteWidget extends TypedRelationWidget { */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $widget = parent::formElement($items, $delta, $element, $form, $form_state); + // disable autocomplete $widget['target_id']['#selection_settings']['target_bundles'] = []; - return $widget; } - }