Elgg  Version 6.2
Breadcrumbs.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menus;
4 
6 
12 class Breadcrumbs {
13 
22  public static function cleanupBreadcrumbs(\Elgg\Event $event): void {
23  /* @var $breadcrumbs PreparedMenu */
24  $breadcrumbs = $event->getValue();
25 
26  $items = $breadcrumbs->getItems('default');
27  if (empty($items)) {
28  return;
29  }
30 
31  $last = null;
32  foreach ($items as $crumb) {
33  $last = $crumb;
34  $crumb->setText(elgg_get_excerpt((string) $crumb->getText(), 100));
35  }
36 
37  // remove last crumb if it has no link
38  if (empty($last->getHref())) {
39  elgg_log("Having a breadcrumb at the end of the list without a link makes no sense. Please update your code for the '{$last->getText()}[{$last->getID()}]' breadcrumb.", \Psr\Log\LogLevel::NOTICE);
40  $breadcrumbs->getSection('default')->remove($last->getID());
41  } elseif (!$last->getChildren() && elgg_http_url_is_identical(elgg_get_current_url(), $last->getHref())) {
42  elgg_log("Having a breadcrumb at the end of the list which links to the current page makes no sense. Please update your code for the '{$last->getText()}[{$last->getID()}]' breadcrumb.", \Psr\Log\LogLevel::NOTICE);
43  $breadcrumbs->getSection('default')->remove($last->getID());
44  }
45  }
46 
54  public static function addHomeItem(\Elgg\Event $event): ?PreparedMenu {
55  /* @var $return PreparedMenu */
56  $return = $event->getValue();
57 
58  /* @var $items \ElggMenuItem[] */
59  $items = $return->getItems('default');
60  if (empty($items)) {
61  return null;
62  }
63 
65  if (elgg_in_context('admin')) {
66  $href = elgg_generate_url('admin');
67  }
68 
69  array_unshift($items, \ElggMenuItem::factory([
70  'name' => 'home',
71  'icon' => 'home',
72  'text' => false,
73  'title' => elgg_get_site_entity()->getDisplayName(),
74  'href' => $href,
75  ]));
76 
77  $return->getSection('default')->fill($items);
78 
79  return $return;
80  }
81 }
$items
Definition: delete.php:8
static factory(array $options)
Create an ElggMenuItem from an associative array.
Models an event passed to event handlers.
Definition: Event.php:11
Represents a menu that has been broken down into sections, with menu hierarchy trees setup.
Prepares breadcrumbs.
Definition: Breadcrumbs.php:12
static addHomeItem(\Elgg\Event $event)
Adds a home item.
Definition: Breadcrumbs.php:54
static cleanupBreadcrumbs(\Elgg\Event $event)
Prepare breadcrumbs before display.
Definition: Breadcrumbs.php:22
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
elgg_in_context(string $context)
Check if this context exists anywhere in the stack.
Definition: context.php:78
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
Definition: item.php:48
elgg_http_url_is_identical(string $url1, string $url2, array $ignore_params=['offset', 'limit'])
Test if two URLs are functionally identical.
Definition: elgglib.php:201
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:101
if($item->getLinkClass()) $href
Definition: submit.php:22
elgg_get_excerpt(string $text, int $num_chars=250)
Returns an excerpt.
Definition: output.php:83
elgg_get_current_url()
Returns the current page's complete URL.
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
$breadcrumbs
Layout breadcrumbs.
Definition: breadcrumbs.php:19