Skip to content

Commit

Permalink
Merge pull request #1349 from bolt/Add-length-attribute-for-title
Browse files Browse the repository at this point in the history
Add length attribute for `|title` filter
  • Loading branch information
I-Valchev authored May 4, 2020
2 parents 8671d5b + a807097 commit 3e519d3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Entity/ContentExtrasTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getExtras(): array
$content = $this;

return array_filter([
'title' => $this->contentExtension->getAnyTitle($content),
'title' => $this->contentExtension->getAnyTitle($content, 80),
'image' => $this->contentExtension->getImage($content, true),
'excerpt' => $this->contentExtension->getExcerpt($content),
'link' => $this->contentExtension->getLink($content),
Expand Down
12 changes: 5 additions & 7 deletions src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public function getFunctions(): array
];
}

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

if (! empty($title)) {
return $title;
Expand All @@ -153,7 +153,7 @@ public function getAnyTitle(Content $content): string
$locales = $content->getDefinition()->get('locales');

foreach ($locales as $locale) {
$title = $this->getTitle($content, $locale);
$title = $this->getTitle($content, $locale, $length);

if (! empty($title)) {
return $title;
Expand All @@ -164,7 +164,7 @@ public function getAnyTitle(Content $content): string
return '';
}

public function getTitle(Content $content, string $locale = ''): string
public function getTitle(Content $content, string $locale = '', int $length = 120): string
{
$titleParts = [];

Expand All @@ -184,9 +184,7 @@ public function getTitle(Content $content, string $locale = ''): string
$titleParts[] = $value;
}

$maxLength = 80; // Should we make this configurable, or is that overkill?
return Html::trimText(implode(' ', $titleParts), $maxLength);
return Html::trimText(implode(' ', $titleParts), $length);
}

public function guessTitleFields(Content $content): array
Expand Down

0 comments on commit 3e519d3

Please sign in to comment.