diff --git a/src/Twig/RelatedExtension.php b/src/Twig/RelatedExtension.php index b0a98b934..d2bffb3d0 100644 --- a/src/Twig/RelatedExtension.php +++ b/src/Twig/RelatedExtension.php @@ -29,12 +29,16 @@ class RelatedExtension extends AbstractExtension /** @var ContentHelper */ private $contentHelper; - public function __construct(RelationRepository $relationRepository, Config $config, Query $query, ContentHelper $contentHelper) + /** @var Notifications */ + private $notifications; + + public function __construct(RelationRepository $relationRepository, Config $config, Query $query, ContentHelper $contentHelper, Notifications $notifications) { $this->relationRepository = $relationRepository; $this->config = $config; $this->query = $query; $this->contentHelper = $contentHelper; + $this->notifications = $notifications; } /** @@ -70,6 +74,10 @@ public function getFunctions(): array */ public function getRelatedContentByType(Content $content, bool $bidirectional = true, ?int $limit = null, bool $publishedOnly = true): array { + if (! $this->checkforContent($content, 'related_by_type')) { + return []; + } + $relations = $this->relationRepository->findRelations($content, null, $bidirectional, $limit, $publishedOnly); return (new Collection($relations)) @@ -89,8 +97,12 @@ public function getRelatedContentByType(Content $content, bool $bidirectional = /** * @return Content[] */ - public function getRelatedContent(Content $content, ?string $name = null, bool $bidirectional = true, ?int $limit = null, bool $publishedOnly = true): array + public function getRelatedContent($content, ?string $name = null, bool $bidirectional = true, ?int $limit = null, bool $publishedOnly = true): array { + if (! $this->checkforContent($content, 'related')) { + return []; + } + $relations = $this->relationRepository->findRelations($content, $name, $bidirectional, $limit, $publishedOnly); return (new Collection($relations)) @@ -101,8 +113,12 @@ public function getRelatedContent(Content $content, ?string $name = null, bool $ ->toArray(); } - public function getFirstRelatedContent(Content $content, ?string $name = null, bool $bidirectional = true, bool $publishedOnly = true): ?Content + public function getFirstRelatedContent($content, ?string $name = null, bool $bidirectional = true, bool $publishedOnly = true): ?Content { + if (! $this->checkforContent($content, 'related_first')) { + return null; + } + $relation = $this->relationRepository->findFirstRelation($content, $name, $bidirectional, $publishedOnly); if ($relation === null) { @@ -164,6 +180,10 @@ public function getRelatedOptions(string $contentTypeSlug, ?string $order = null public function getRelatedValues(Content $source, string $contentType): Collection { + if (! $this->checkforContent($source, 'related_values')) { + return new Collection([]); + } + if ($source->getId() === null) { return new Collection([]); } @@ -179,4 +199,18 @@ public function getRelatedValues(Content $source, string $contentType): Collecti return new Collection($values); } + + private function checkforContent($content, string $keyword): bool + { + if (! $content instanceof Content) { + $this->notifications->warning( + 'Incorrect usage of `' . $keyword . '`-filter or function', + 'The `' . $keyword . '`-filter or function can only be applied to a single Record.' + ); + + return false; + } + + return true; + } } diff --git a/templates/_partials/notification.html.twig b/templates/_partials/notification.html.twig index 00fb21bf6..fba7a5514 100644 --- a/templates/_partials/notification.html.twig +++ b/templates/_partials/notification.html.twig @@ -94,14 +94,16 @@
')|raw }}{%- if frame.type|default() is not empty -%}
+ {% for frame in backtrace|slice(2) %}
+ -
+
{%- if frame.type|default() is not empty -%}
- {{- frame.class|split('\\')|last()|excerpt(24) -}}{{- frame.type -}}
- {%- endif -%}
- {{- frame.function -}}(){% if frame.line|default() %} - line {{ frame.line }}{% endif %}
+ {{- frame.class|split('\\')|last()|excerpt(24) -}}{{- frame.type -}}
+ {%- endif -%}
+ {{- frame.function -}}()
+ {% if prevline|default() %} - line {{ prevline }}{% endif %}
+ {% set prevline = frame.line|default() %}