Skip to content

Commit f17e408

Browse files
committed
finished views; class implements Stringable;
1 parent 50efc3d commit f17e408

File tree

6 files changed

+168
-23
lines changed

6 files changed

+168
-23
lines changed

composer.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,9 @@
5656
"laravel": {
5757
"providers": [
5858
"Aelora\\Breadcrumbs\\BreadcrumbsServiceProvider"
59-
],
60-
"aliases": {
61-
"Breadcrumbs": "Aelora\\Breadcrumbs\\Facades\\Breadcrumbs"
62-
}
59+
]
6360
}
6461
},
6562
"minimum-stability": "dev",
6663
"prefer-stable": true
67-
}
64+
}

config/breadcrumbs.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
<?php
2-
// config for Aelora/Breadcrumbs
32
return [
3+
'home' => [
44

5+
// Should the home link be automatically included in the breadcrumbs? You can also manually include
6+
// or exclude the home link when you build the breadcrumb class.
7+
'include' => true,
8+
9+
'text' => 'Home',
10+
11+
'url' => url('/'),
12+
13+
'image' => '',
14+
],
15+
16+
// Which blade template to use. Currently only tailwind is supported.
17+
'theme' => 'tailwind',
18+
19+
// View to use to generate the HTML breadcrumbs. If defined, this will
20+
// override the theme above.
21+
'view' => '',
22+
23+
// Character to use as a separator between crumbs. Use and encoded entity
24+
// if you want something like >
25+
'separator' => '/',
526
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{-- @see https://tailwind-elements.com/docs/standard/navigation/breadcrumbs/ --}}
2+
<nav class="rounded-md w-full">
3+
<ol class="list-none flex">
4+
@foreach ($crumbs as $crumb)
5+
@if (!empty($crumb['url']))
6+
<li>
7+
<a href="{{ $crumb['url'] }}" class="text-blue-600 hover:text-blue-700">{{ $crumb['title'] }}</a>
8+
</li>
9+
@else
10+
<li class="text-gray-500">{{ $crumb['title'] }}</li>
11+
@endif
12+
@if (!$loop->last)
13+
<li><span class="text-gray-500 mx-2">{{ config('breadcrumbs.separator', '/') }}</span></li>
14+
@endif
15+
@endforeach
16+
</ol>
17+
</nav>

resources/views/jsonld.blade.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{-- View partial for the breadcrumb schema --}}
2+
@if (count($crumbs) < 1)
3+
<!-- no breadcrumbs defined -->
4+
@else
5+
<script type="application/ld+json">
6+
@json($jsonld)
7+
</script>
8+
@endif

src/Breadcrumbs.php

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,124 @@
22

33
namespace Aelora\Breadcrumbs;
44

5-
class Breadcrumbs
5+
use Stringable;
6+
7+
class Breadcrumbs implements Stringable
68
{
9+
private array $crumbs = [];
10+
private $home = [];
11+
12+
public function __construct()
13+
{
14+
if (config('breadcrumbs.home.include', true)) {
15+
$this->home = [
16+
'title' => config('breadcrumbs.home.text', ''),
17+
'url' => config('breadcrumbs.home.url', ''),
18+
'image' => config('breadcrumbs.home.image', ''),
19+
];
20+
} else {
21+
$this->home = false;
22+
}
23+
}
24+
25+
public static function create(): self
26+
{
27+
return new self();
28+
}
29+
30+
public function add(string $title, string $url = '', string $image = ''): self
31+
{
32+
$this->crumbs[] = [
33+
'title' => $title,
34+
'url' => $url,
35+
'image' => $image,
36+
];
37+
return $this;
38+
}
39+
40+
public function setHome($title, string $url = '', string $image = ''): self
41+
{
42+
if ($title === false) {
43+
$this->home = false;
44+
} else {
45+
$this->home = [
46+
'title' => $title,
47+
'url' => $url,
48+
'image' => $image,
49+
];
50+
}
51+
return $this;
52+
}
53+
54+
public function reverse(): self
55+
{
56+
$this->crumbs = array_reverse($this->crumbs);
57+
return $this;
58+
}
59+
60+
public function reset(): self
61+
{
62+
$this->crumbs = [];
63+
return $this;
64+
}
65+
66+
public function count(): int
67+
{
68+
return count($this->crumbs);
69+
}
70+
71+
/**
72+
* Returns the generated blade templates for the breadcrumbs, optionally
73+
* echoing it out as well.
74+
*/
75+
public function generate(bool $echo = false): string
76+
{
77+
$out = '';
78+
if (is_array($this->home) && config('breadcrumbs.home.include', true)) {
79+
array_unshift($this->crumbs, $this->home);
80+
}
81+
82+
$view = config('breadcrumbs.view', '');
83+
if (empty($view)) {
84+
$theme = strtolower(config('breadcrumbs.theme', 'tailwind'));
85+
$view = 'breadcrumbs::breadcrumbs-' . $theme;
86+
}
87+
88+
$out .= view($view, ['crumbs' => $this->crumbs])->render();
89+
90+
$itemList = [];
91+
foreach ($this->crumbs as $idx => $crumb) {
92+
$item = [];
93+
if (!empty($crumb['url'])) {
94+
$item['@id'] = $crumb['url'];
95+
}
96+
if (!empty($crumb['title'])) {
97+
$item['name'] = $crumb['title'];
98+
}
99+
if (!empty($crumb['image'])) {
100+
$item['image'] = $crumb['image'];
101+
}
102+
$listItem = [
103+
'@type' => 'ListItem',
104+
'position' => $idx + 1,
105+
'item' => $item,
106+
];
107+
$itemList[] = $listItem;
108+
}
109+
110+
$jsonld = [
111+
'@context' => 'https://schema.org',
112+
'@type' => 'BreadcrumbList',
113+
'itemListElement' => $itemList,
114+
];
115+
116+
$out .= view('breadcrumbs::jsonld', ['jsonld' => $jsonld, 'crumbs' => $this->crumbs])->render();
117+
118+
return $out;
119+
}
120+
121+
public function __toString(): string
122+
{
123+
return $this->generate(false);
124+
}
7125
}

src/Facades/Breadcrumbs.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)