From e417b0a84c3e0a2e04839d14010d5cbe0b27b032 Mon Sep 17 00:00:00 2001 From: dannylamb Date: Thu, 2 Jan 2020 19:57:16 +0000 Subject: [PATCH] Filtering out terms that don't have external uris from the autocomplete --- src/Plugin/Condition/NodeHasTerm.php | 21 +-- .../ExternalUriSelection.php | 125 ++++++++++++++++++ 2 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 src/Plugin/EntityReferenceSelection/ExternalUriSelection.php diff --git a/src/Plugin/Condition/NodeHasTerm.php b/src/Plugin/Condition/NodeHasTerm.php index 22f87761e..1e85dea42 100644 --- a/src/Plugin/Condition/NodeHasTerm.php +++ b/src/Plugin/Condition/NodeHasTerm.php @@ -104,10 +104,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta $form['term'] = [ '#type' => 'entity_autocomplete', '#title' => $this->t('Term'), + '#description' => $this->t('Terms need an Authority Link or External Uri to be compatible with this Condition. If the term you are looking for is not appearing in the autocomplete, please ensure it has a value for its Authority Link or External Uri field.'), '#tags' => TRUE, '#default_value' => $default, '#target_type' => 'taxonomy_term', '#required' => TRUE, + '#selection_handler' => 'islandora:external_uri', ]; $form['logic'] = [ @@ -123,25 +125,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta return parent::buildConfigurationForm($form, $form_state); } - /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - parent::validateConfigurationForm($form, $form_state); - $value = $form_state->getValue('term'); - foreach ($value as $target) { - $tid = $target['target_id']; - $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); - $uri = $this->utils->getUriForTerm($term); - if (empty($uri)) { - $form_state->setErrorByName( - 'term', - $this->t('@name does not have an external URI. Give it an Authority Link or the External Uri field.', ['@name' => $term->label()]) - ); - } - } - } - /** * {@inheritdoc} */ diff --git a/src/Plugin/EntityReferenceSelection/ExternalUriSelection.php b/src/Plugin/EntityReferenceSelection/ExternalUriSelection.php new file mode 100644 index 000000000..6715fb23d --- /dev/null +++ b/src/Plugin/EntityReferenceSelection/ExternalUriSelection.php @@ -0,0 +1,125 @@ +utils = $utils; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('module_handler'), + $container->get('current_user'), + $container->get('entity_field.manager'), + $container->get('entity_type.bundle.info'), + $container->get('entity.repository'), + $container->get('islandora.utils') + ); + } + + /** + * {@inheritdoc} + */ + public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { + $options = parent::getReferenceableEntities($match, $match_operator, $limit); + + foreach (array_keys($options) as $vid) { + foreach (array_keys($options[$vid]) as $tid) { + $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); + $uri = $this->utils->getUriForTerm($term); + if (empty($uri)) { + unset($options[$vid][$tid]); + } + } + if (empty($options[$vid])) { + unset($options[$vid]); + } + } + + return $options; + } + +}