From 08aa61a2bf2722872f166a69d9e1ce1166a6692e Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 27 Aug 2020 06:19:41 +0200 Subject: [PATCH] Basic bread crumb code to replace obsolete package --- app/Support/Twig/Breadcrumbs.php | 102 +++++++++++++++++++++++++ config/bc.php | 24 ++++++ config/twigbridge.php | 2 + resources/views/v2/accounts/show.twig | 14 ++++ resources/views/v2/layout/default.twig | 3 + 5 files changed, 145 insertions(+) create mode 100644 app/Support/Twig/Breadcrumbs.php create mode 100644 config/bc.php create mode 100644 resources/views/v2/accounts/show.twig diff --git a/app/Support/Twig/Breadcrumbs.php b/app/Support/Twig/Breadcrumbs.php new file mode 100644 index 0000000000..ee1bcf1b1d --- /dev/null +++ b/app/Support/Twig/Breadcrumbs.php @@ -0,0 +1,102 @@ +. + */ + +namespace FireflyIII\Support\Twig; + +use FireflyIII\Exceptions\FireflyException; +use Route; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +/** + * Class Breadcrumbs + */ +class Breadcrumbs extends AbstractExtension +{ + /** + * {@inheritdoc} + */ + public function getFunctions(): array + { + return [ + $this->renderBreadcrumb(), + ]; + } + + /** + * @return TwigFunction + */ + private function renderBreadcrumb(): TwigFunction + { + return new TwigFunction( + 'ff3bc', + static function (array $args): string { + $name = Route::getCurrentRoute()->getName() ?? ''; + + // loop for actual breadcrumb: + $arr = config(sprintf('bc.%s', $name)); + $breadcrumbs = []; + if (null === $arr) { + throw new FireflyException(sprintf('No breadcrumbs for route "%s".', $name)); + } + $hasParent = true; + $loop = 0; + while (true === $hasParent && $loop < 30) { + $breadcrumbs[] = $arr; + if (null === $arr['parent']) { + $hasParent = false; + + } + if (null !== $arr['parent']) { + $arr = config(sprintf('bc.%s', $arr['parent'])); + if (null === $arr) { + throw new FireflyException(sprintf('No (2) breadcrumbs for route "%s".', $name)); + } + } + $loop++; // safety catch + } + // reverse order + $breadcrumbs = array_reverse($breadcrumbs); + + // get HTML + $html = ''; + return $html; + }, + ['is_safe' => ['html']] + ); + } +} \ No newline at end of file diff --git a/config/bc.php b/config/bc.php new file mode 100644 index 0000000000..dd7f217585 --- /dev/null +++ b/config/bc.php @@ -0,0 +1,24 @@ + [ + 'parent' => null, + 'title' => 'breadcrumbs.home', + 'static_route' => 'home', + 'dynamic_route' => null, + ], + 'accounts' => [ + 'index' => [ + 'parent' => 'index', + 'title' => 'breadcrumbs.accounts', + 'static_route' => null, + 'dynamic_route' => 'accounts.index', + ], + 'show' => [ + 'parent' => 'accounts.index', + 'title' => 'breadcrumbs.accounts_show', + 'static_route' => null, + 'dynamic_route' => 'accounts.show', + ], + ], +]; \ No newline at end of file diff --git a/config/twigbridge.php b/config/twigbridge.php index 7ceabb7a57..494c8c8d74 100644 --- a/config/twigbridge.php +++ b/config/twigbridge.php @@ -25,6 +25,7 @@ use TwigBridge\Extension\Laravel\Url; use TwigBridge\Extension\Loader\Facades; use TwigBridge\Extension\Loader\Filters; use TwigBridge\Extension\Loader\Functions; +use FireflyIII\Support\Twig\Breadcrumbs; /** * Configuration options for Twig. @@ -137,6 +138,7 @@ return [ Rule::class, TransactionGroupTwig::class, Translation::class, + Breadcrumbs::class, ], diff --git a/resources/views/v2/accounts/show.twig b/resources/views/v2/accounts/show.twig new file mode 100644 index 0000000000..58fc147884 --- /dev/null +++ b/resources/views/v2/accounts/show.twig @@ -0,0 +1,14 @@ +{% set bcArgs = [[objectType], [1,2,3]] %} + +{% extends "./layout/default" %} +{% block content %} +
+{% endblock %} + +{% block styles %} + +{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/resources/views/v2/layout/default.twig b/resources/views/v2/layout/default.twig index 0d3556cc80..8af427c119 100644 --- a/resources/views/v2/layout/default.twig +++ b/resources/views/v2/layout/default.twig @@ -45,10 +45,13 @@ {{ subTitle }}
+ {{ ff3bc(bcArgs) }} +