mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-25 02:10:22 -06:00
Basic bread crumb code to replace obsolete package
This commit is contained in:
parent
a16ac479d5
commit
08aa61a2bf
102
app/Support/Twig/Breadcrumbs.php
Normal file
102
app/Support/Twig/Breadcrumbs.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/*
|
||||
* Breadcrumbs.php
|
||||
* Copyright (c) 2020 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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 = '<ol class="breadcrumb float-sm-right">';
|
||||
foreach ($breadcrumbs as $index => $breadcrumb) {
|
||||
$class = 'breadcrumb-item';
|
||||
if ($index === count($breadcrumbs) - 1) {
|
||||
// active!
|
||||
$class = 'breadcrumb-item active';
|
||||
}
|
||||
$route = '#';
|
||||
if (null !== $breadcrumb['static_route']) {
|
||||
$route = route($breadcrumb['static_route']);
|
||||
}
|
||||
if (null !== $breadcrumb['dynamic_route']) {
|
||||
$route = route($breadcrumb['dynamic_route'], $args[$index - 1] ?? []);
|
||||
}
|
||||
$html .= sprintf('<li class="%1$s"><a href="%2$s" title="%3$s">%3$s</a></li>', $class, $route, trans($breadcrumb['title']));
|
||||
}
|
||||
$html .= '</ol>';
|
||||
return $html;
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
}
|
24
config/bc.php
Normal file
24
config/bc.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'index' => [
|
||||
'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',
|
||||
],
|
||||
],
|
||||
];
|
@ -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,
|
||||
|
||||
],
|
||||
|
||||
|
14
resources/views/v2/accounts/show.twig
Normal file
14
resources/views/v2/accounts/show.twig
Normal file
@ -0,0 +1,14 @@
|
||||
{% set bcArgs = [[objectType], [1,2,3]] %}
|
||||
|
||||
{% extends "./layout/default" %}
|
||||
{% block content %}
|
||||
<div id="accounts"></div>
|
||||
{% endblock %}
|
||||
|
||||
{% block styles %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="v2/js/accounts.js" nonce="{{ JS_NONCE }}"></script>
|
||||
{% endblock %}
|
@ -45,10 +45,13 @@
|
||||
{{ subTitle }}</small></h1>
|
||||
</div><!-- /.col -->
|
||||
<div class="col-sm-6">
|
||||
{{ ff3bc(bcArgs) }}
|
||||
<!--
|
||||
<ol class="breadcrumb float-sm-right">
|
||||
<li class="breadcrumb-item"><a href="#">Home</a></li>
|
||||
<li class="breadcrumb-item active">Bread crumbs v1</li>
|
||||
</ol>
|
||||
-->
|
||||
</div><!-- /.col -->
|
||||
</div><!-- /.row -->
|
||||
</div><!-- /.container-fluid -->
|
||||
|
Loading…
Reference in New Issue
Block a user