Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '@cache.data']
islandora.entity_mapper:
class: Islandora\EntityMapper\EntityMapper
islandora.stomp.auth_header_listener:
Expand Down
44 changes: 39 additions & 5 deletions src/IslandoraUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Drupal\islandora;

use Drupal\context\ContextManager;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
Expand Down Expand Up @@ -81,6 +84,13 @@ class IslandoraUtils {
*/
protected AccountInterface $currentUser;

/**
* Cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;

/**
* Constructor.
*
Expand All @@ -96,21 +106,25 @@ class IslandoraUtils {
* Language manager.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Cache\CacheBackendInterface
* Cache backend.
*/
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,
CacheBackendInterface $cache,
) {
$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->cache = $cache;
}

/**
Expand Down Expand Up @@ -250,6 +264,19 @@ function ($field) {
* Calling getStorage() throws if the storage handler couldn't be loaded.
*/
public function getTermForUri($uri) {

$cid_parts = [
'islandora',
'term-for-uri',
'user-' . $this->currentUser->id(),
'uri-' . Html::getClass($uri),
];
$cid = implode(':', $cid_parts);

if ($cache = $this->cache->get($cid)) {
return $cache->data;
}

// Get authority link fields to search.
$field_map = $this->entityFieldManager->getFieldMap();
$fields = [];
Expand All @@ -273,12 +300,19 @@ public function getTermForUri($uri) {
->condition($orGroup)
->execute();

if (empty($results)) {
return NULL;
$term = NULL;
if (!empty($results)) {
$term = $this->entityTypeManager->getStorage('taxonomy_term')
->load(reset($results));
$this->cache->set(
$cid,
$term,
CacheBackendInterface::CACHE_PERMANENT,
Cache::mergeTags(array_merge($term->getCacheTags(), ['user:' . $this->currentUser->id()]))
);
}

return $this->entityTypeManager->getStorage('taxonomy_term')
->load(reset($results));
return $term;
}

/**
Expand Down