Skip to content

Commit

Permalink
Fixed an issue which occurred when an Entry has an `entries.primaryOw…
Browse files Browse the repository at this point in the history
…nerId` which does not exist anymore. #35
  • Loading branch information
internetztube committed Jun 18, 2024
1 parent e90a100 commit bb9ec4e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.0.3 - 2024-06-18
### Fixed
- Fixed an issue which occurred when an Entry has an `entries.primaryOwnerId` which does not exist anymore. [#35](https://github.com/internetztube/craft-element-relations/issues/35)

## 3.0.2 - 2024-06-18
### Fixed
- Move Resaving completely into queue + 2k batching.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "internetztube/craft-element-relations",
"description": "Shows all relations of an element.",
"type": "craft-plugin",
"version": "3.0.2.3",
"version": "3.0.3",
"keywords": [
"craft",
"cms",
Expand Down
21 changes: 21 additions & 0 deletions src/services/RelationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static function getRelations(ElementInterface $element): array
['=', 'elementrelations_cache.targetSiteId', $element->siteId],
])
->collect()
->filter(fn (array $row) => $row['type'])
->groupBy('type')
->map(function (Collection $items, string $elementType) {
$whereStatements = $items->map(fn($value) => [
Expand All @@ -53,6 +54,26 @@ public static function getRelations(ElementInterface $element): array
];
})
->flatten()

// Check if Element has a valid `primaryOwner` or ´owner`
// https://github.com/internetztube/craft-element-relations/issues/35
->filter(function (ElementInterface $element) {
if (method_exists($element, 'getPrimaryOwner')) {
try {
$element->getPrimaryOwner();
} catch (\Exception $e) {
return false;
}
}
if (method_exists($element, 'getOwner')) {
try {
$element->getOwner();
} catch (\Exception $e) {
return false;
}
}
return true;
})
->all();
}
}

0 comments on commit bb9ec4e

Please sign in to comment.