Skip to content

Commit

Permalink
move it into a config
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Feb 27, 2025
1 parent fca5fcd commit f682537
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/install/islandora.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions islandora.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion islandora.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion src/Form/IslandoraSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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();

Expand Down
56 changes: 45 additions & 11 deletions src/IslandoraUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -81,6 +83,13 @@ class IslandoraUtils {
*/
protected AccountInterface $currentUser;

/**
* Islandora config settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $islandoraSettings;

/**
* Constructor.
*
Expand All @@ -96,21 +105,25 @@ 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,
EntityFieldManagerInterface $entity_field_manager,
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;
$this->contextManager = $context_manager;
$this->flysystemFactory = $flysystem_factory;
$this->languageManager = $language_manager;
$this->currentUser = $current_user;
$this->islandoraSettings = $config->get(IslandoraSettingsForm::CONFIG_NAME);
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit f682537

Please sign in to comment.