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

Richer content format option #1628

Merged
merged 1 commit into from
Jul 15, 2020
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
6 changes: 3 additions & 3 deletions assets/js/app/editor/Components/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
>
<template v-if="name === 'status'" slot="singleLabel" slot-scope="props">
<span class="status mr-2" :class="`is-${props.option.key}`"></span>
{{ props.option.value }}
{{ props.option.value | raw }}
</template>
<template v-if="name === 'status'" slot="option" slot-scope="props">
<span class="status mr-2" :class="`is-${props.option.key}`"></span>
{{ props.option.value }}
{{ props.option.value | raw }}
</template>
<template v-if="name !== 'status'" slot="tag" slot-scope="props">
<span @drop="drop($event)" @dragover="allowDrop($event)">
Expand All @@ -41,7 +41,7 @@
<div v-if="!taggable" class="multiselect__tag__drag">
<i class="fas fa-arrows-alt"></i>
</div>
<span v-text="props.option.value"></span>
<span v-html="props.option.value"></span>
<i
tabindex="1"
class="multiselect__tag-icon"
Expand Down
6 changes: 3 additions & 3 deletions src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function getTitle(Content $content, string $locale = '', int $length = 12
}

if (ContentHelper::isSuitable($content)) {
$title = ContentHelper::get($content, $content->getDefinition()->get('title_format'), $locale);
$title = $this->contentHelper->get($content, $content->getDefinition()->get('title_format'), $locale);
} else {
$title = ContentHelper::getFieldBasedTitle($content, $locale);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ public function getExcerpt($content, int $length = 280, bool $includeTitle = fal
}

if (ContentHelper::isSuitable($content, 'excerpt_format')) {
$excerpt = ContentHelper::get($content, $content->getDefinition()->get('excerpt_format'));
$excerpt = $this->contentHelper->get($content, $content->getDefinition()->get('excerpt_format'));
} else {
$excerpt = $this->getFieldBasedExcerpt($content, $length, $includeTitle);
}
Expand Down Expand Up @@ -566,7 +566,7 @@ private function selectOptionsContentType(SelectField $field): LaravelCollection
foreach ($records as $record) {
$options[] = [
'key' => $record->getId(),
'value' => ContentHelper::get($record, $format),
'value' => $this->contentHelper->get($record, $format),
];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Twig/RelatedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ class RelatedExtension extends AbstractExtension
/** @var Query */
private $query;

public function __construct(RelationRepository $relationRepository, Config $config, Query $query)
/** @var ContentHelper */
private $contentHelper;

public function __construct(RelationRepository $relationRepository, Config $config, Query $query, ContentHelper $contentHelper)
{
$this->relationRepository = $relationRepository;
$this->config = $config;
$this->query = $query;
$this->contentHelper = $contentHelper;
}

/**
Expand Down Expand Up @@ -151,7 +155,7 @@ public function getRelatedOptions(string $contentTypeSlug, ?string $order = null
foreach ($records as $record) {
$options[] = [
'key' => $record->getId(),
'value' => ContentHelper::get($record, $format),
'value' => $this->contentHelper->get($record, $format),
];
}

Expand Down
34 changes: 32 additions & 2 deletions src/Utils/ContentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bolt\Configuration\Config;
use Bolt\Entity\Content;
use Bolt\Entity\Field\Excerptable;
use Bolt\Twig\LocaleExtension;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

Expand All @@ -22,11 +23,15 @@ class ContentHelper
/** @var Config */
private $config;

public function __construct(Canonical $canonical, RequestStack $requestStack, Config $config)
/** @var LocaleExtension */
private $localeExtension;

public function __construct(Canonical $canonical, RequestStack $requestStack, Config $config, LocaleExtension $localeExtension)
{
$this->canonical = $canonical;
$this->request = $requestStack->getCurrentRequest() ?? Request::createFromGlobals();
$this->config = $config;
$this->localeExtension = $localeExtension;
}

public function setCanonicalPath($record, ?string $locale = null): void
Expand Down Expand Up @@ -110,7 +115,7 @@ public static function isSuitable(Content $record, string $which = 'title_format
return false;
}

public static function get(Content $record, string $format = '', string $locale = ''): string
public function get(Content $record, string $format = '', ?string $locale = null): string
{
if (empty($format)) {
$format = '{title} (№ {id}, {status})';
Expand All @@ -135,6 +140,22 @@ function ($match) use ($record, $locale) {
return $record->getAuthor();
}

if ($match[1] === 'publishedAt') {
return $this->localeExtension->localdate($record->getPublishedAt(), null, $locale);
}

if ($match[1] === 'modifiedAt') {
return $this->localeExtension->localdate($record->getModifiedAt(), null, $locale);
}

if ($match[1] === 'createdAt') {
return $this->localeExtension->localdate($record->getCreatedAt(), null, $locale);
}

if ($match[1] === 'depublishedAt') {
return $this->localeExtension->localdate($record->getDepublishedAt(), null, $locale);
}

if ($record->hasField($match[1])) {
$field = $record->getField($match[1]);

Expand All @@ -145,6 +166,15 @@ function ($match) use ($record, $locale) {
return $field;
}

if (array_key_exists($match[1], $record->getExtras())) {
// If it's the icon, return an html element
if ($match[1] === 'icon') {
return sprintf("<i class='fas %s'></i>", $record->getExtras()[$match[1]]);
}

return $record->getExtras()[$match[1]];
}

return '(unknown)';
},
$format
Expand Down