No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

breadcrumbs.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Grav;
  4. class Breadcrumbs
  5. {
  6. /**
  7. * @var array
  8. */
  9. protected $breadcrumbs;
  10. protected $config;
  11. /**
  12. * @param $config
  13. */
  14. public function __construct($config)
  15. {
  16. $this->config = $config;
  17. }
  18. /**
  19. * Get all items in breadcrumbs.
  20. *
  21. * @return array
  22. */
  23. public function get()
  24. {
  25. if (!$this->breadcrumbs) {
  26. $this->build();
  27. }
  28. return $this->breadcrumbs;
  29. }
  30. /**
  31. * Build breadcrumbs.
  32. *
  33. * @internal
  34. */
  35. protected function build()
  36. {
  37. $hierarchy = array();
  38. $grav = Grav::instance();
  39. $current = $grav['page'];
  40. while ($current && !$current->root()) {
  41. $hierarchy[$current->url()] = $current;
  42. $current = $current->parent();
  43. }
  44. // Page cannot be routed.
  45. if (!$current) {
  46. $this->breadcrumbs = array();
  47. return;
  48. }
  49. if ($this->config['include_home']) {
  50. $home = $grav['pages']->dispatch('/');
  51. if ($home && !array_key_exists($home->url(), $hierarchy)) {
  52. $hierarchy[] = $home;
  53. }
  54. }
  55. $this->breadcrumbs = array_reverse($hierarchy);
  56. }
  57. }