docs icon indicating copy to clipboard operation
docs copied to clipboard

Document custom login providers

Open bytehead opened this issue 11 months ago • 1 comments

See https://github.com/contao/contao/pull/7817

bytehead avatar Jan 07 '25 09:01 bytehead

Create a backend login listener:

# src/EventListener/Menu/AcmeBackendLoginListener.php
<?php

declare(strict_types=1);

namespace App\EventListener\Menu;

use Contao\CoreBundle\Event\MenuEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

#[AsEventListener]
class AcmeBackendLoginListener
{
    public function __construct(
        private readonly TranslatorInterface $translator,
        private readonly RouterInterface $router,
    ) {
    }

    public function __invoke(MenuEvent $event): void
    {
        $tree = $event->getTree();

        if ('loginMenu' !== $tree->getName()) {
            return;
        }

        $acme = $event->getFactory()
            ->createItem('acme')
            ->setAttribute('class', 'acme')
            ->setLabel('Login with Acme')
            ->setUri('https://login.acme')
            ->setLinkAttribute('class', 'tl_submit has-icon')
            ->setExtra('icon', '/path/to/icon.svg')
            ->setExtra('icon_dark', '/path/to/icon--dark.svg')
            ->setExtra('safe_label', true)
            ->setExtra('translation_domain', false)
        ;

        $tree->addChild($acme);
    }
}

bytehead avatar Jan 07 '25 09:01 bytehead