From 1a9ecdb86eac3b65f0767ce641e1bdf30433f21d Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Wed, 26 Feb 2025 09:18:18 -0500 Subject: [PATCH] move it into a config --- config/install/islandora.settings.yml | 1 + islandora.post_update.php | 10 +++++ islandora.services.yml | 2 +- src/Form/IslandoraSettingsForm.php | 11 +++++- src/IslandoraUtils.php | 56 +++++++++++++++++++++------ 5 files changed, 67 insertions(+), 13 deletions(-) diff --git a/config/install/islandora.settings.yml b/config/install/islandora.settings.yml index 1497c291e..bc29353a0 100644 --- a/config/install/islandora.settings.yml +++ b/config/install/islandora.settings.yml @@ -2,3 +2,4 @@ broker_url: 'tcp://localhost:61613' jwt_expiry: '+2 hour' delete_media_and_files: TRUE gemini_pseudo_bundles: [] +fast_term_queries: TRUE diff --git a/islandora.post_update.php b/islandora.post_update.php index 0a29e56ed..efb671216 100644 --- a/islandora.post_update.php +++ b/islandora.post_update.php @@ -14,3 +14,13 @@ function islandora_post_update_delete_media_and_files() { $config->set('delete_media_and_files', TRUE); $config->save(TRUE); } + +/** + * Ensure `fast_term_queries` exists. + */ +function islandora_post_update_fast_term_queries() : void { + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('islandora.settings'); + $config->set('fast_term_queries', TRUE); + $config->save(TRUE); +} diff --git a/islandora.services.yml b/islandora.services.yml index 36ef7dac3..dd04dfe58 100644 --- a/islandora.services.yml +++ b/islandora.services.yml @@ -54,7 +54,7 @@ services: arguments: ['@entity_type.manager', '@current_user', '@language_manager', '@file_system', '@islandora.utils'] islandora.utils: class: Drupal\islandora\IslandoraUtils - arguments: ['@entity_type.manager', '@entity_field.manager', '@context.manager', '@flysystem_factory', '@language_manager', '@current_user'] + arguments: ['@entity_type.manager', '@entity_field.manager', '@context.manager', '@flysystem_factory', '@language_manager', '@current_user', '@config.factory'] islandora.entity_mapper: class: Islandora\EntityMapper\EntityMapper islandora.stomp.auth_header_listener: diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php index ad4c83b3e..bc8f05cde 100644 --- a/src/Form/IslandoraSettingsForm.php +++ b/src/Form/IslandoraSettingsForm.php @@ -44,6 +44,7 @@ class IslandoraSettingsForm extends ConfigFormBase { const GEMINI_PSEUDO_FIELD = 'field_gemini_uri'; const NODE_DELETE_MEDIA_AND_FILES = 'delete_media_and_files'; const REDIRECT_AFTER_MEDIA_SAVE = 'redirect_after_media_save'; + const FAST_TERM_QUERIES = 'fast_term_queries'; /** * To list the available bundle types. @@ -79,7 +80,7 @@ class IslandoraSettingsForm extends ConfigFormBase { public function __construct( ConfigFactoryInterface $config_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info, - EntityTypeManagerInterface $entity_type_manager + EntityTypeManagerInterface $entity_type_manager, ) { $this->setConfigFactory($config_factory); $this->entityTypeBundleInfo = $entity_type_bundle_info; @@ -228,6 +229,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => (bool) $config->get(self::REDIRECT_AFTER_MEDIA_SAVE), ]; + $form[self::FAST_TERM_QUERIES] = [ + '#type' => 'checkbox', + '#title' => $this->t('Use multiple queries for term URI lookups.'), + '#description' => $this->t('Using multiple queries to look up taxonomy terms by URI is faster in most setups. You may want to disable this if you have a term access protection module enabled.'), + '#default_value' => (bool) $config->get(self::FAST_TERM_QUERIES), + ]; + $form[self::FEDORA_URL] = [ '#type' => 'textfield', '#title' => $this->t('Fedora URL'), @@ -383,6 +391,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ->set(self::UPLOAD_FORM_ALLOWED_MIMETYPES, $form_state->getValue(self::UPLOAD_FORM_ALLOWED_MIMETYPES)) ->set(self::GEMINI_PSEUDO, $new_pseudo_types) ->set(self::NODE_DELETE_MEDIA_AND_FILES, $form_state->getValue(self::NODE_DELETE_MEDIA_AND_FILES)) + ->set(self::FAST_TERM_QUERIES, $form_state->getValue(self::FAST_TERM_QUERIES)) ->set(self::REDIRECT_AFTER_MEDIA_SAVE, $form_state->getValue(self::REDIRECT_AFTER_MEDIA_SAVE)) ->save(); diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php index d0a101db6..9668b3342 100644 --- a/src/IslandoraUtils.php +++ b/src/IslandoraUtils.php @@ -23,6 +23,8 @@ use Drupal\media\MediaInterface; use Drupal\node\NodeInterface; use Drupal\taxonomy\TermInterface; +use Drupal\islandora\Form\IslandoraSettingsForm; +use Drupal\Core\Config\ConfigFactoryInterface; /** * Utility functions for figuring out when to fire derivative reactions. @@ -81,6 +83,13 @@ class IslandoraUtils { */ protected AccountInterface $currentUser; + /** + * Islandora config settings. + * + * @var \Drupal\Core\Config\ImmutableConfig + */ + protected $islandoraSettings; + /** * Constructor. * @@ -96,6 +105,8 @@ class IslandoraUtils { * Language manager. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config + * Config factory. */ public function __construct( EntityTypeManagerInterface $entity_type_manager, @@ -103,7 +114,8 @@ public function __construct( ContextManager $context_manager, FlysystemFactory $flysystem_factory, LanguageManagerInterface $language_manager, - AccountInterface $current_user + AccountInterface $current_user, + ConfigFactoryInterface $config, ) { $this->entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; @@ -111,6 +123,7 @@ public function __construct( $this->flysystemFactory = $flysystem_factory; $this->languageManager = $language_manager; $this->currentUser = $current_user; + $this->islandoraSettings = $config->get(IslandoraSettingsForm::CONFIG_NAME); } /** @@ -261,19 +274,40 @@ public function getTermForUri($uri) { // Add field_external_uri. $fields[] = self::EXTERNAL_URI_FIELD; - $storage = $this->entityTypeManager->getStorage('taxonomy_term'); - foreach ($fields as $field) { - $query = $storage->getQuery(); - $results = $query - ->accessCheck(TRUE) - ->condition("$field.uri", $uri) - ->execute(); - if (!empty($results)) { - return $storage->load(reset($results)); + if ($this->islandoraSettings->get(IslandoraSettingsForm::FAST_TERM_QUERIES)) { + $storage = $this->entityTypeManager->getStorage('taxonomy_term'); + foreach ($fields as $field) { + $query = $storage->getQuery(); + $results = $query + ->accessCheck(TRUE) + ->condition("$field.uri", $uri) + ->execute(); + if (!empty($results)) { + return $storage->load(reset($results)); + } } + + return NULL; } - return NULL; + $query = $this->entityTypeManager->getStorage('taxonomy_term')->getQuery(); + + $orGroup = $query->orConditionGroup(); + foreach ($fields as $field) { + $orGroup->condition("$field.uri", $uri); + } + + $results = $query + ->accessCheck(TRUE) + ->condition($orGroup) + ->execute(); + + if (empty($results)) { + return NULL; + } + + return $this->entityTypeManager->getStorage('taxonomy_term') + ->load(reset($results)); } /**