Skip to content

Commit

Permalink
Allow changing the page title & allow changing the favicon on the log…
Browse files Browse the repository at this point in the history
…in page
  • Loading branch information
jdreesen committed Dec 4, 2023
1 parent d73ad17 commit 6bda676
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ which is set via the `APP_ENV` environment variable.
neusta_pimcore_backend_branding:
environments:
dev:
title: ACME
bezelColor: '#fcc243'
staging:
bezelColor: '#005ea1'
prod:
title:
login: Welcome to ACME!
backend: '{hostname} :: ACME'
favIcon: <url-of-your-fav-icon>
bezelColor: '#00a13a'
signet: # or just: <url-of-your-logo>
Expand Down
5 changes: 5 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
use Neusta\Pimcore\BackendBrandingBundle\Controller\CssController;
use Neusta\Pimcore\BackendBrandingBundle\Controller\JsController;
use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendAssetsListener;
use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendResponseListener;

return static function (ContainerConfigurator $container) {
$container->services()
->set(BackendAssetsListener::class)
->autoconfigure()
->arg('$urlGenerator', service('router'))
->set(BackendResponseListener::class)
->autoconfigure()
->arg('$env', '%kernel.runtime_environment%')
->arg('$config', param('neusta_pimcore_backend_branding.environments'))
->set(CssController::class)
->autoconfigure()
->arg('$env', '%kernel.runtime_environment%')
Expand Down
15 changes: 7 additions & 8 deletions src/Controller/JsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Neusta\Pimcore\BackendBrandingBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -11,24 +12,22 @@
final class JsController
{
/**
* @param array<string, array{favIcon: string}> $config
* @param array<string, array{
* favIcon: string,
* title: array{login: string|null, backend: string|null},
* }> $config
*/
public function __construct(
private readonly string $env,
private readonly array $config,
) {
}

public function __invoke(): Response
public function __invoke(Request $request): Response
{
$config = $this->config[$this->env];
$js = [];

if ($favIcon = $this->config[$this->env]['favIcon'] ?? null) {
$js[] = <<<JS
document.querySelector("link[rel*='icon']").href = '{$favIcon}';
JS;
}

return new Response(implode("\n", $js), Response::HTTP_OK, ['Content-type' => 'text/javascript']);
}
}
10 changes: 10 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public function getConfigTreeBuilder(): TreeBuilder
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->arrayNode('title')
->beforeNormalization()
->ifString()
->then(fn (string $v) => ['login' => $v, 'backend' => $v])
->end()
->children()
->scalarNode('login')->defaultNull()->end()
->scalarNode('backend')->defaultNull()->end()
->end()
->end()
->scalarNode('favIcon')->end()
->scalarNode('bezelColor')->end()
->append($this->createBackgroundImageNode('signet', '70%', 'center'))
Expand Down
82 changes: 82 additions & 0 deletions src/EventListener/BackendResponseListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\EventListener;

use Pimcore\Tool;
use Pimcore\Tool\Session;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

#[AsEventListener]
final class BackendResponseListener
{
private const INTERCEPTED_ROUTES = [
'pimcore_admin_login',
'pimcore_admin_login_fallback',
'pimcore_admin_index',
];

/**
* @param array<string, array{
* favIcon: string,
* title: array{login: string|null, backend: string|null},
* }> $config
*/
public function __construct(
private readonly string $env,
private readonly array $config,
) {
}

public function __invoke(ResponseEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}

$request = $event->getRequest();

if (!\in_array($request->attributes->get('_route'), self::INTERCEPTED_ROUTES, true)) {
return;
}

$response = $event->getResponse();

if (!$content = $response->getContent()) {
return;
}

if ($titleConfig = $this->config[$this->env]['title'] ?? null) {
$loggedIn = null !== Session::getSessionBag($request->getSession(), 'pimcore_admin')?->get('user');
$title = $loggedIn ? $titleConfig['backend'] : $titleConfig['login'];

if ($title) {
$title = strtr($title, ['{hostname}' => htmlentities((string) Tool::getHostname(), \ENT_QUOTES, 'UTF-8')]);
$replaced = preg_replace(
'#<title>(.+)</title>#',
"<title>{$title}</title>",
$content,
);

if (null !== $replaced) {
$content = $replaced;
}
}
}

if ($favIcon = $this->config[$this->env]['favIcon'] ?? null) {
$replaced = preg_replace(
'#<link rel="icon"[^>]+>#',
sprintf('<link rel="shortcut icon" href="%s">', $favIcon),
$content,
);

if (null !== $replaced) {
$content = $replaced;
}
}

$response->setContent($content);
}
}

0 comments on commit 6bda676

Please sign in to comment.