Elgg  Version master
Site.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menus;
4 
7 
14 class Site {
15 
23  public static function registerAdminConfiguredItems(\Elgg\Event $event) {
24  $custom_menu_items = elgg_get_config('site_custom_menu_items');
25  if (empty($custom_menu_items)) {
26  return;
27  }
28 
29  /* @var $return MenuItems */
30  $return = $event->getValue();
31 
32  // add custom menu items
33  $n = 1;
34  foreach ($custom_menu_items as $title => $url) {
35  $return[] = \ElggMenuItem::factory([
36  'name' => "custom{$n}",
37  'text' => $title,
38  'href' => $url,
39  ]);
40  $n++;
41  }
42 
43  return $return;
44  }
45 
53  public static function reorderItems(\Elgg\Event $event) {
54  /* @var $menu PreparedMenu */
55  $menu = $event->getValue();
56 
57  $featured_menu_names = array_values((array) elgg_get_config('site_featured_menu_names'));
58 
59  $registered = $menu->getItems('default');
60  if (empty($registered)) {
61  return;
62  }
63 
64  $has_selected = false;
65  $priority = 500;
66 
67  foreach ($registered as $item) {
68  if (in_array($item->getName(), $featured_menu_names)) {
69  $featured_index = array_search($item->getName(), $featured_menu_names);
70  $item->setPriority($featured_index);
71  } else {
72  $item->setPriority($priority);
73  $priority++;
74  }
75  if ($item->getSelected()) {
76  $has_selected = true;
77  }
78  }
79 
80  if (!$has_selected) {
81  // no selected item found, try to match on context
83 
84  $is_selected = function (\ElggMenuItem $item) use ($site_url) {
85  if (elgg_strpos($item->getHref(), $site_url) !== 0) {
86  // not an url on the site
87  return false;
88  }
89 
90  if ($item->getName() == elgg_get_context()) {
91  // menu name matches current context
92  return true;
93  }
94 
95  return false;
96  };
97 
98  foreach ($registered as $item) {
99  if ($is_selected($item)) {
100  $item->setSelected(true);
101  break;
102  }
103  }
104  }
105 
106  usort($registered, [\ElggMenuBuilder::class, 'compareByPriority']);
107 
108  $max_display_items = $event->getParam('max_display_items', 5);
109 
110  $num_menu_items = count($registered);
111 
112  $more = [];
113  if ($max_display_items && $num_menu_items > ($max_display_items + 1)) {
114  $more = array_splice($registered, $max_display_items);
115  }
116 
117  if (!empty($more)) {
118  $dropdown = \ElggMenuItem::factory([
119  'name' => 'more',
120  'text' => elgg_echo('more'),
121  'href' => false,
122  'priority' => 999,
123  ]);
124 
125  foreach ($more as &$item) {
126  $item->setParentName('more');
127  }
128 
129  $dropdown->setChildren($more);
130 
131  $registered[] = $dropdown;
132  }
133 
134  $menu->getSection('default')->fill($registered);
135 
136  return $menu;
137  }
138 }
if(!$items) $item
Definition: delete.php:13
$custom_menu_items
Definition: save.php:18
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
$title
Definition: generic.php:50
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$menu
Admin sidebar – just outputs the page menus.
Definition: sidebar.php:6
static factory(array $options)
Create an ElggMenuItem from an associative array.
elgg_get_context()
Get the current context.
Definition: context.php:41
Elgg Menu Item.
$featured_menu_names
Definition: save.php:15
static reorderItems(\Elgg\Event $event)
Reorder the site menu based on custom order and move excess to dropdown.
Definition: Site.php:53
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:69
$num_menu_items
Definition: save.php:17
static registerAdminConfiguredItems(\Elgg\Event $event)
Registers custom menu items.
Definition: Site.php:23
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
$site_url
Definition: upgrade.php:3
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
$priority
Models an event passed to event handlers.
Definition: Event.php:11
Register menu items to the site menu.
Definition: Site.php:14