From 1572261617ea66f09e2104114cdd328769e80afe Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 23 Jul 2020 22:48:59 +0000 Subject: [PATCH] [TWIG] Add twig function to output the active tag if the current route matches a given one --- src/Twig/Extension.php | 54 +++++++++++++++++++++ src/Twig/Runtime.php | 70 ++++++++++++++++++++++++++++ templates/settings/profile.html.twig | 54 +++++++++++---------- 3 files changed, 154 insertions(+), 24 deletions(-) create mode 100644 src/Twig/Extension.php create mode 100644 src/Twig/Runtime.php diff --git a/src/Twig/Extension.php b/src/Twig/Extension.php new file mode 100644 index 0000000000..6c7758a148 --- /dev/null +++ b/src/Twig/Extension.php @@ -0,0 +1,54 @@ +. +// }}} + +/** + * GNU social Twig extensions + * + * @package GNUsocial + * @category Twig + * + * @author Hugo Sales + * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +namespace App\Twig; + +use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; +use Twig\TwigFunction; + +class Extension extends AbstractExtension +{ + public function getFilters() + { + return [ + // new TwigFilter('foo', [GSRuntime::class, 'foo']), + ]; + } + + public function getFunctions() + { + return [ + /** Twig function to output the 'active' class if the current route matches the given route */ + new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']), + new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']), + ]; + } +} diff --git a/src/Twig/Runtime.php b/src/Twig/Runtime.php new file mode 100644 index 0000000000..48b5c94d6e --- /dev/null +++ b/src/Twig/Runtime.php @@ -0,0 +1,70 @@ +. +// }}} + +/** + * Common utility functions + * + * @package GNUsocial + * @category Util + * + * @author Hugo Sales + * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +namespace App\Twig; + +use Functional as F; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpKernel\KernelEvents; +use Twig\Extension\RuntimeExtensionInterface; + +class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface +{ + private Request $request; + public function __constructor() + { + } + + public function isCurrentRouteActive(string ...$routes): string + { + return $this->isCurrentRoute('active', ...$routes); + } + + public function isCurrentRoute(string $class, string ...$routes): string + { + $current_route = $this->request->attributes->get('_route'); + return F\some($routes, F\equal($current_route)) ? $class : ''; + } + + // ---------------------------------------------------------- + + // Request is not a service, can't find a better way to get it + public function onKernelRequest(RequestEvent $event) + { + $this->request = $event->getRequest(); + } + + public static function getSubscribedEvents() + { + return [KernelEvents::REQUEST => 'onKernelRequest']; + } +} diff --git a/templates/settings/profile.html.twig b/templates/settings/profile.html.twig index 8cf58251d2..5e11b3c176 100644 --- a/templates/settings/profile.html.twig +++ b/templates/settings/profile.html.twig @@ -12,31 +12,37 @@ {% block body %} -
- - +
+ + +
+ {% block form %} +
{{ form(prof) }}