. */ 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']] ); } }