Make "User" and dateformatting work

This commit is contained in:
Bob den Otter
2019-01-11 12:18:17 +01:00
parent da9c656d61
commit 3bb40891df
4 changed files with 52 additions and 8 deletions
@@ -4,8 +4,8 @@
<span class="edit-link"><a href="{{ record.editlink() }}"><strong>{{ __('general.phrase.edit') }}</strong></a></span> -
{% endif %}
{{ __('general.phrase.written-by-on', {
'%name%': record.user.displayname|default(__('Unknown')),
'%date%': record.datepublish|localedatetime("%A %B %e, %Y")
'%name%': record.author.displayname|default(__('Unknown')),
'%date%': record.publishedAt|localedatetime("%A %B %e, %Y")
}) }}
</p>
+8
View File
@@ -247,6 +247,14 @@ class Content implements ObjectManagerAware
return $this->author;
}
/**
* Backward-compatible alias for `getAuthor`
*/
public function geUser(): User
{
return $this->author;
}
public function setAuthor(?User $author): void
{
$this->author = $author;
+1 -1
View File
@@ -72,7 +72,7 @@ trait ContentMagicTraits
public function magicLink()
{
return $this->urlGenerator->generate('record', [
'slug' => $this->getSlug(),
'slug' => $this->getSlug() ?: $this->getId(),
'contenttypeslug' => $this->getDefinition()->get('singular_slug'),
]);
}
+41 -5
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Bolt\Twig;
use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
@@ -19,10 +20,12 @@ class AppExtension extends AbstractExtension
*/
public function getFilters(): array
{
$safe = ['is_safe' => ['html']];
return [
new TwigFilter('order', [$this, 'dummy']),
new TwigFilter('unique', [$this, 'unique']),
new TwigFilter('localedatetime', [$this, 'dummy']),
new TwigFilter('localedatetime', [$this, 'localedatetime'], $safe),
new TwigFilter('showimage', [$this, 'dummy']),
new TwigFilter('ucwords', [$this, 'ucwords']),
];
@@ -33,11 +36,13 @@ class AppExtension extends AbstractExtension
*/
public function getFunctions(): array
{
$safe = ['is_safe' => ['html']];
return [
new TwigFunction('image', [$this, 'dummy'], ['is_safe' => ['html']]),
new TwigFunction('thumbnail', [$this, 'dummy'], ['is_safe' => ['html']]),
new TwigFunction('widgets', [$this, 'dummy'], ['is_safe' => ['html']]),
new TwigFunction('popup', [$this, 'dummy'], ['is_safe' => ['html']]),
new TwigFunction('image', [$this, 'dummy'], $safe),
new TwigFunction('thumbnail', [$this, 'dummy'], $safe),
new TwigFunction('widgets', [$this, 'dummy'], $safe),
new TwigFunction('popup', [$this, 'dummy'], $safe),
];
}
@@ -59,4 +64,35 @@ class AppExtension extends AbstractExtension
{
return array_unique($array);
}
public function localedatetime($dateTime, $format = '%B %e, %Y %H:%M', $locale = 0)
{
if (!$dateTime instanceof \DateTime) {
$dateTime = new \DateTime($dateTime);
}
// Check for Windows to find and replace the %e modifier correctly
// @see: http://php.net/strftime
$os = strtoupper(substr(PHP_OS, 0, 3));
$format = $os !== 'WIN' ? $format : preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
// According to http://php.net/manual/en/function.setlocale.php manual
// if the second parameter is "0", the locale setting is not affected,
// only the current setting is returned.
$result = setlocale(LC_ALL, $locale);
if ($result === false) {
// This shouldn't occur, but.. Dude!
// You ain't even got locale or English on your platform??
// Various things we could do. We could fail miserably, but a more
// graceful approach is to use the datetime to display a default
// format
// $this->systemLogger->error('No valid locale detected. Fallback on DateTime active.', ['event' => 'system']);
return $dateTime->format('Y-m-d H:i:s');
}
$timestamp = $dateTime->getTimestamp();
return strftime($format, $timestamp);
}
}