Skip to content
Merged
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
40 changes: 37 additions & 3 deletions src/Twig/RelatedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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) {
Expand Down Expand Up @@ -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([]);
}
Expand All @@ -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;
}
}
16 changes: 9 additions & 7 deletions templates/_partials/notification.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@
<h1> {{ subject|markdown|striptags('<em><i><code>')|raw }}</h1>
{{ body|markdown|raw }}
<ul>
{% for frame in backtrace|slice(2) %}
<li>
<code>{%- if frame.type|default() is not empty -%}
{% for frame in backtrace|slice(2) %}
<li>
<code>{%- if frame.type|default() is not empty -%}
<abbr title="{{ frame.class }}">
{{- frame.class|split('\\')|last()|excerpt(24) -}}</abbr>{{- frame.type -}}
{%- endif -%}
<strong>{{- frame.function -}}()</strong></code>{% if frame.line|default() %} - line {{ frame.line }}{% endif %}
{{- frame.class|split('\\')|last()|excerpt(24) -}}</abbr>{{- frame.type -}}
{%- endif -%}
<strong>{{- frame.function -}}()</strong>
</code>{% if prevline|default() %} - line {{ prevline }}{% endif %}
{% set prevline = frame.line|default() %}
</li>
{% endfor %}
{% endfor %}
</ul>
</div>