Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Frontend output a bit more robust by catching common pitfalls (non-existing content, etc) #1847

Merged
merged 1 commit into from
Sep 14, 2020
Merged
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
53 changes: 39 additions & 14 deletions src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getFunctions(): array
];
}

public function getAnyTitle(Content $content, int $length = 120): string
public function getAnyTitle(?Content $content, int $length = 120): string
{
$title = $this->getTitle($content, '', $length);

Expand All @@ -187,8 +187,12 @@ public function getAnyTitle(Content $content, int $length = 120): string
return '(untitled)';
}

public function getTitle(Content $content, string $locale = '', int $length = 120): string
public function getTitle(?Content $content, string $locale = '', int $length = 120): string
{
if (! $content instanceof Content) {
return '<mark>No content given</mark>';
}

if (empty($locale)) {
$locale = $this->request->getLocale();
}
Expand Down Expand Up @@ -245,6 +249,10 @@ public function getExcerpt($content, int $length = 280, bool $includeTitle = fal
return Excerpt::getExcerpt((string) $content, $length, $focus);
}

if (! $content instanceof Content) {
return '<mark>No content given</mark>';
}

if (ContentHelper::isSuitable($content, 'excerpt_format')) {
$excerpt = $this->contentHelper->get($content, $content->getDefinition()->get('excerpt_format'));
} else {
Expand Down Expand Up @@ -298,13 +306,21 @@ private function getFieldBasedExcerpt(Content $content, int $length, bool $inclu
return rtrim($excerpt, '. ');
}

public function getPreviousContent(Content $content, string $byColumn = 'id', bool $sameContentType = true): ?Content
public function getPreviousContent(?Content $content, string $byColumn = 'id', bool $sameContentType = true): ?Content
{
if (! $content instanceof Content) {
return null;
}

return $this->getAdjacentContent($content, 'previous', $byColumn, $sameContentType);
}

public function getNextContent(Content $content, string $byColumn = 'id', bool $sameContentType = true): ?Content
public function getNextContent(?Content $content, string $byColumn = 'id', bool $sameContentType = true): ?Content
{
if (! $content instanceof Content) {
return null;
}

return $this->getAdjacentContent($content, 'next', $byColumn, $sameContentType);
}

Expand All @@ -321,8 +337,12 @@ private function getAdjacentContent(Content $content, string $direction, string
return $this->contentRepository->findAdjacentBy($byColumn, $direction, $content->getId(), $contentType);
}

public function isCurrent(Environment $env, Content $content): bool
public function isCurrent(Environment $env, ?Content $content): bool
{
if (! $content instanceof Content) {
return false;
}

// If we have a $record set in the Global Twig env, we can simply
// compare that to what's passed in.
if (array_key_exists('record', $env->getGlobals())) {
Expand Down Expand Up @@ -375,18 +395,18 @@ public function getLink($contentOrTaxonomy, bool $canonical = false, ?string $lo
return null;
}

public function getEditLink(Content $content): ?string
public function getEditLink(?Content $content): ?string
{
if ($content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
if (! $content instanceof Content || $content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
return null;
}

return $this->generateLink('bolt_content_edit', ['id' => $content->getId()]);
}

public function getDeleteLink(Content $content, bool $absolute = false): ?string
public function getDeleteLink(?Content $content, bool $absolute = false): ?string
{
if ($content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
if (! $content instanceof Content || $content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
return null;
}

Expand All @@ -398,18 +418,18 @@ public function getDeleteLink(Content $content, bool $absolute = false): ?string
return $this->generateLink('bolt_content_delete', $params, $absolute);
}

public function getDuplicateLink(Content $content, bool $absolute = false): ?string
public function getDuplicateLink(?Content $content, bool $absolute = false): ?string
{
if ($content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
if (! $content instanceof Content || $content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
return null;
}

return $this->generateLink('bolt_content_duplicate', ['id' => $content->getId()], $absolute);
}

public function getStatusLink(Content $content, bool $absolute = false): ?string
public function getStatusLink(?Content $content, bool $absolute = false): ?string
{
if ($content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
if (! $content instanceof Content || $content->getId() === null || ! $this->security->getUser() || ! $this->security->isGranted('ROLE_ADMIN')) {
return null;
}

Expand All @@ -433,8 +453,13 @@ private function generateLink(string $route, array $params, $canonical = false):
return $link;
}

public function getTaxonomies(Content $content): Collection
public function getTaxonomies(?Content $content): Collection
{
if (! $content instanceof Content) {
$body = sprintf("You have called the <code>|taxonomies</code> filter with a parameter of type '%s', but <code>|taxonomies</code> accepts record (Content).", gettype($content));
$this->notifications->warning('Incorrect use of <code>|taxonomies</code> filter', $body);
}

$taxonomies = [];

$definition = $content->getDefinition();
Expand Down