Skip to content
Snippets Groups Projects
Unverified Commit 1f64123b authored by Raf Philtjens's avatar Raf Philtjens
Browse files

Small performance improvement by loading multiple entities at once + provide...

Small performance improvement by loading multiple entities at once + provide limit argument to listDefaultRevisionsForSources() and listDefaultRevisionsForTargets()
parent 5aec0a08
No related branches found
No related tags found
No related merge requests found
......@@ -94,19 +94,26 @@ final class EntityUsage {
*
* @param \Drupal\Core\Entity\EntityInterface $target_entity
* A target entity.
* @param int $limit
* (optional) Limit number of results returned. Defaults to 0, which
* will return all sources.
*
* @return array
* A nested array with usage data. The first level is keyed by the type of
* the source entities. The value is array contains source entity ids
* that uses reference to the target entity.
*/
public function listDefaultRevisionsForSources(EntityInterface $target_entity) {
public function listDefaultRevisionsForSources(EntityInterface $target_entity, int $limit = 0) {
$references = [];
// Get all sources.
$sources = $this->entityUsage->listSources($target_entity);
$sources = $this->entityUsage->listSources($target_entity, TRUE, $limit);
// Extra filtering for revisionable sources.
foreach ($sources as $source_type => $source_type_entities) {
foreach ($source_type_entities as $source_id => $source_revisions) {
foreach ($sources as $source_type => $source_type_entity_usage) {
$source_type_entities = $this
->entityTypeManager
->getStorage($source_type)
->loadMultiple(array_keys($source_type_entity_usage));
foreach ($source_type_entity_usage as $source_id => $source_revisions) {
if (isset($references[$source_type][$source_id])) {
// Relation between target and default source revision was confirmed
// earlier and it is attached in response, no need to check again.
......@@ -114,7 +121,7 @@ final class EntityUsage {
}
// Load source entity default revision.
$source = $this->entityTypeManager->getStorage($source_type)->load($source_id);
$source = $source_type_entities[$source_id] ?? NULL;
// If for some reason this record is broken, just skip it.
if (empty($source)) {
......@@ -165,13 +172,16 @@ final class EntityUsage {
*
* @param \Drupal\Core\Entity\EntityInterface $source_entity
* The source entity to check for references.
* @param int $limit
* (optional) Limit number of results returned. Defaults to 0, which
* will return all sources.
*
* @return array
* A nested array with usage data. The first level is keyed by the type of
* the target entities. The value is array contains target entity ids
* that are referenced by source entity.
*/
public function listDefaultRevisionsForTargets(EntityInterface $source_entity) {
public function listDefaultRevisionsForTargets(EntityInterface $source_entity, int $limit = 0) {
$references = [];
// If entity supports revisions, get targets for current revision.
if ($source_entity->getEntityType()->isRevisionable()) {
......@@ -193,8 +203,11 @@ final class EntityUsage {
$query
->orderBy('target_id', 'DESC');
$result = $query->execute();
if ($limit > 0) {
$query->range(0, $limit);
}
$result = $query->execute();
foreach ($result as $usage) {
$target_id_value = !empty($usage->target_id) ? $usage->target_id : (string) $usage->target_id_string;
$references[$usage->target_type][$target_id_value] = $target_id_value;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment