Skip to content

Commit

Permalink
Allow changing the fav icon
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Dec 1, 2023
1 parent 4ef47ac commit d73ad17
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ neusta_pimcore_backend_branding:
staging:
bezelColor: '#005ea1'
prod:
favIcon: <url-of-your-fav-icon>
bezelColor: '#00a13a'
signet: # or just: <url-of-your-logo>
url: <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 @@ -4,6 +4,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Neusta\Pimcore\BackendBrandingBundle\Controller\CssController;
use Neusta\Pimcore\BackendBrandingBundle\Controller\JsController;
use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendAssetsListener;

return static function (ContainerConfigurator $container) {
Expand All @@ -15,5 +16,9 @@
->autoconfigure()
->arg('$env', '%kernel.runtime_environment%')
->arg('$config', param('neusta_pimcore_backend_branding.environments'))
->set(JsController::class)
->autoconfigure()
->arg('$env', '%kernel.runtime_environment%')
->arg('$config', param('neusta_pimcore_backend_branding.environments'))
;
};
34 changes: 34 additions & 0 deletions src/Controller/JsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;

#[AsController, Route('/neusta/backend-branding/js', name: 'neusta_pimcore_backend_branding_js')]
final class JsController
{
/**
* @param array<string, array{favIcon: string}> $config
*/
public function __construct(
private readonly string $env,
private readonly array $config,
) {
}

public function __invoke(): Response
{
$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']);
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->scalarNode('favIcon')->end()
->scalarNode('bezelColor')->end()
->append($this->createBackgroundImageNode('signet', '70%', 'center'))
->append($this->createBackgroundImageNode('tabBarIcon'))
Expand Down
6 changes: 6 additions & 0 deletions src/EventListener/BackendAssetsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public function addCss(PathsEvent $event): void
{
$event->addPaths([$this->urlGenerator->generate('neusta_pimcore_backend_branding_css')]);
}

#[AsEventListener(BundleManagerEvents::JS_PATHS)]
public function addJs(PathsEvent $event): void
{
$event->addPaths([$this->urlGenerator->generate('neusta_pimcore_backend_branding_js')]);
}
}

0 comments on commit d73ad17

Please sign in to comment.