You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.9 KiB
92 lines
2.9 KiB
<?php |
|
|
|
/** |
|
* @file |
|
* Contains disable_field_autocomplete.module. |
|
*/ |
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
use Drupal\taxonomy\Entity\Term; |
|
|
|
/** |
|
* Implements hook_help(). |
|
*/ |
|
function disable_field_autocomplete_help($route_name, RouteMatchInterface $route_match) { |
|
switch ($route_name) { |
|
case 'help.page.facet_field_formatter': |
|
$output = ''; |
|
$output .= '<h3>' . t('About') . '</h3>'; |
|
$output .= '<p>' . t('Disable autocomplete for TypedRelation fields') . '</p>'; |
|
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; |
|
}
|
|
|