Skip to content

Commit

Permalink
Content function (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-pilarczyk authored May 10, 2023
1 parent 5c12107 commit abb89a0
Showing 1 changed file with 75 additions and 33 deletions.
108 changes: 75 additions & 33 deletions src/Twig/CmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFunction;

final class CmsExtension extends AbstractExtension implements GlobalsInterface
{
Expand All @@ -21,7 +22,16 @@ public function __construct(

public function getTokenParsers(): array
{
return [new ContentTagTokenParser()];
return [
new ContentTagTokenParser(),
];
}

public function getFunctions(): array
{
return [
new TwigFunction('content', [$this, 'getContent'], ['is_safe' => ['all']]),
];
}

public function getContent(string $name, string $default, array $parameters = []): string
Expand All @@ -42,15 +52,30 @@ public function getContent(string $name, string $default, array $parameters = []
(int)$preview[$name]
);
if (null !== $historicalContent) {
$content = $this->translator->trans($historicalContent->getValue(), $parameters, null, $this->translator->getLocale());
$content = $this->translator->trans(
$historicalContent->getValue(),
$parameters,
null,
$this->translator->getLocale()
);
}
}
}

if (null === $content) {
$content = $this->translator->trans($name, $editMode ? [] : $parameters, null, $this->translator->getLocale());
$content = $this->translator->trans(
$name,
$editMode ? [] : $parameters,
null,
$this->translator->getLocale()
);
if ($content === $name) {
$content = $this->translator->trans($default, $editMode ? [] : $parameters, null, $this->translator->getLocale());
$content = $this->translator->trans(
$default,
$editMode ? [] : $parameters,
null,
$this->translator->getLocale()
);
}
}

Expand Down Expand Up @@ -80,29 +105,46 @@ public function getGlobals(): array
'historyMode' => true,
'appUrl' => $this->generateUrl($ref, $params),
]);
} else if ($this->isArticlePage()) {
$globals = array_merge($globals, [
'articleMode' => true,
'appUrl' => $this->generateUrl($ref, $params) ?? $params['ref'] ?? '/',
]);
} else if ($this->isPreviewPage()) {
unset($params['_preview']);
$globals = array_merge($globals, [
'previewMode' => true,
'appUrl' => $ref,
'saveUrl' => $this->generateUrl('cms_content_rollback'),
'refUrl' => $this->getCmsUrl($this->cms->getRoute(), $params),
'state' => $this->cms->getPreviewParams(),
]);
} else if (null !== $this->cms->getRoute()) {
$globals = array_merge($globals, [
'editMode' => $this->cms->isEditMode(),
'appUrl' => $this->cms->isEditMode() ? $this->getAppUrl($this->cms->getRoute(), $this->cms->getRouteParams()) : null,
'cmsUrl' => $this->getCmsUrl($this->cms->getRoute(), $this->cms->getRouteParams()),
'saveUrl' => $this->generateUrl('cms_content_patch'),
'historyUrl' => $this->getUrlWithRef('cms_content_history', $this->cms->getRoute(), $this->cms->getRouteParams()),
'articlesUrl' => $this->getUrlWithRef('cms_articles', $this->cms->getRoute(), $this->cms->getRouteParams()),
]);
} else {
if ($this->isArticlePage()) {
$globals = array_merge($globals, [
'articleMode' => true,
'appUrl' => $this->generateUrl($ref, $params) ?? $params['ref'] ?? '/',
]);
} else {
if ($this->isPreviewPage()) {
unset($params['_preview']);
$globals = array_merge($globals, [
'previewMode' => true,
'appUrl' => $ref,
'saveUrl' => $this->generateUrl('cms_content_rollback'),
'refUrl' => $this->getCmsUrl($this->cms->getRoute(), $params),
'state' => $this->cms->getPreviewParams(),
]);
} else {
if (null !== $this->cms->getRoute()) {
$globals = array_merge($globals, [
'editMode' => $this->cms->isEditMode(),
'appUrl' => $this->cms->isEditMode() ? $this->getAppUrl(
$this->cms->getRoute(),
$this->cms->getRouteParams()
) : null,
'cmsUrl' => $this->getCmsUrl($this->cms->getRoute(), $this->cms->getRouteParams()),
'saveUrl' => $this->generateUrl('cms_content_patch'),
'historyUrl' => $this->getUrlWithRef(
'cms_content_history',
$this->cms->getRoute(),
$this->cms->getRouteParams()
),
'articlesUrl' => $this->getUrlWithRef(
'cms_articles',
$this->cms->getRoute(),
$this->cms->getRouteParams()
),
]);
}
}
}
}

return ['cms' => $globals];
Expand All @@ -111,12 +153,12 @@ public function getGlobals(): array
public function getAppUrl(string $route, array $routeParams = []): ?string
{
return $this->generateUrl(
preg_replace('/^_cms_/', '_i18n_', $route),
$routeParams
) ?? $this->generateUrl(
preg_replace('/^_cms_/', '', $route),
$routeParams
);
preg_replace('/^_cms_/', '_i18n_', $route),
$routeParams
) ?? $this->generateUrl(
preg_replace('/^_cms_/', '', $route),
$routeParams
);
}

public function getCmsUrl(string $route, array $routeParams = []): ?string
Expand Down

0 comments on commit abb89a0

Please sign in to comment.