Skip to content

Commit

Permalink
Avoid joining with media tables which are not related to objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Mar 9, 2024
1 parent a302620 commit c1448c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
1 change: 1 addition & 0 deletions islandora_hierarchical_access.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
arguments:
- '@database'
- '@entity_type.manager'
- '@entity_field.manager'
islandora_hierarchical_access.query_tagger:
class: '\Drupal\islandora_hierarchical_access\Access\QueryTagger'
factory: [null, 'create']
Expand Down
40 changes: 15 additions & 25 deletions src/LUTGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,40 @@
namespace Drupal\islandora_hierarchical_access;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\media\MediaTypeInterface;

/**
* Lookup table generator service implementation.
*/
class LUTGenerator implements LUTGeneratorInterface {

/**
* The database service.
*
* @var \Drupal\Core\Database\Connection
*/
protected Connection $database;

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;

/**
* Memoize the list of fields to be considered.
*
* @var string[]|null
* @var string[]
*/
protected ?array $uniqueFileFields = NULL;
protected array $uniqueFileFields;

/**
* Constructor.
*/
public function __construct(
Connection $database,
EntityTypeManagerInterface $entityTypeManager
protected Connection $database,
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityFieldManagerInterface $entityFieldManager,
) {
$this->database = $database;
$this->entityTypeManager = $entityTypeManager;
// No-op, other than stashing services.
}

/**
* {@inheritDoc}
*/
public function regenerate(): void {
$tx = $this->database->startTransaction();
$tx = $this->database->startTransaction('lut_regeneration');
try {
$this->database->truncate(static::TABLE_NAME)->execute();
$this->generate();
Expand Down Expand Up @@ -111,7 +99,7 @@ public function generate(EntityInterface $entity = NULL): void {
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function uniqueFileFields(): array {
if ($this->uniqueFileFields === NULL) {
if (!isset($this->uniqueFileFields)) {
$this->uniqueFileFields = [];
foreach ($this->getFileFields() as $field) {
$name = $field->getName();
Expand All @@ -135,12 +123,14 @@ protected function uniqueFileFields(): array {
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getFileFields() : iterable {
// XXX: Ideally, we could limit the fields returned to those of media types
// which have Islandora's "media of" field; however, unsure how to assert
// the existence of such a field at the moment.
/** @var \Drupal\media\MediaTypeInterface[] $types */
$types = $this->entityTypeManager->getStorage('media_type')->loadMultiple();

foreach ($types as $type) {
$fields = $this->entityFieldManager->getFieldDefinitions('media', $type->id());
if (!isset($fields[IslandoraUtils::MEDIA_OF_FIELD])) {
continue;
}
$field = $type->getSource()->getSourceFieldDefinition($type);
$item_def = $field->getItemDefinition();
if ($item_def->getSetting('handler') == 'default:file') {
Expand Down

0 comments on commit c1448c0

Please sign in to comment.