Browse Source

added form validation function

main
Paul Pound 2 months ago
parent
commit
1f6db82f12
  1. 74
      disable_field_autocomplete.module
  2. 4
      src/Plugin/Field/FieldWidget/DisableAutocompleteWidget.php

74
disable_field_autocomplete.module

@ -6,19 +6,87 @@
*/ */
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\taxonomy\Entity\Term;
/** /**
* Implements hook_help(). * 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) { switch ($route_name) {
// Main module help for the facet_field_formatter module.
case 'help.page.facet_field_formatter': case 'help.page.facet_field_formatter':
$output = ''; $output = '';
$output .= '<h3>' . t('About') . '</h3>'; $output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Format string and text fields as doi links') . '</p>'; $output .= '<p>' . t('Disable autocomplete for TypedRelation fields') . '</p>';
return $output; return $output;
default: 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;
}

4
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\controlled_access_terms\Plugin\Field\FieldWidget\TypedRelationWidget;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\taxonomy\Entity\Term;
/** /**
* Plugin implementation of the 'chosen_select' widget. * 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) { public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$widget = parent::formElement($items, $delta, $element, $form, $form_state); $widget = parent::formElement($items, $delta, $element, $form, $form_state);
// disable autocomplete
$widget['target_id']['#selection_settings']['target_bundles'] = []; $widget['target_id']['#selection_settings']['target_bundles'] = [];
return $widget; return $widget;
} }
} }

Loading…
Cancel
Save