1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui;
6:
7: class Breadcrumb extends Lister
8: {
9: public $ui = 'breadcrumb';
10:
11: public $defaultTemplate = 'breadcrumb.html';
12:
13: /** @var array */
14: public $path = [];
15:
16: /** @var string */
17: public $dividerClass = 'right angle icon';
18:
19: /**
20: * Adds a new link that will appear on the right.
21: *
22: * @param string $section Title of link
23: * @param string|array<0|string, string|int|false> $link Link itself
24: *
25: * @return $this
26: */
27: public function addCrumb($section = null, $link = null)
28: {
29: if (is_array($link)) {
30: $link = $this->url($link);
31: }
32: $this->path[] = ['section' => $section, 'link' => $link, 'divider' => $this->dividerClass];
33:
34: return $this;
35: }
36:
37: /**
38: * Converts the last crumb you added into a title. This may be convenient if you add
39: * crumbs conditionally and the last should remain as a title.
40: *
41: * @return $this
42: */
43: public function popTitle()
44: {
45: $title = array_pop($this->path);
46: $this->set($title['section'] ?? '');
47:
48: return $this;
49: }
50:
51: /**
52: * Adds a new link that will appear on the left.
53: *
54: * @param string $section Title of link
55: * @param string|array $link Link itself
56: *
57: * @return $this
58: */
59: public function addCrumbReverse($section = null, $link = null)
60: {
61: array_unshift($this->path, ['section' => $section, 'link' => $link]);
62:
63: return $this;
64: }
65:
66: #[\Override]
67: protected function renderView(): void
68: {
69: $this->setSource($this->path);
70:
71: parent::renderView();
72: }
73: }
74: