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 
76  if ($item->getSelected()) {
77  $has_selected = true;
78  }
79  }
80 
81  if (!$has_selected) {
82  // no selected item found, try to match on context
84 
85  $is_selected = function (\ElggMenuItem $item) use ($site_url) {
86  if (elgg_strpos($item->getHref(), $site_url) !== 0) {
87  // not an url on the site
88  return false;
89  }
90 
91  if ($item->getName() == elgg_get_context()) {
92  // menu name matches current context
93  return true;
94  }
95 
96  return false;
97  };
98 
99  foreach ($registered as $item) {
100  if ($is_selected($item)) {
101  $item->setSelected(true);
102  break;
103  }
104  }
105  }
106 
107  usort($registered, [\ElggMenuBuilder::class, 'compareByPriority']);
108 
109  $max_display_items = $event->getParam('max_display_items', 5);
110 
111  $num_menu_items = count($registered);
112 
113  $more = [];
114  if ($max_display_items && $num_menu_items > ($max_display_items + 1)) {
115  $more = array_splice($registered, $max_display_items);
116  }
117 
118  if (!empty($more)) {
119  $dropdown = \ElggMenuItem::factory([
120  'name' => 'more',
121  'text' => elgg_echo('more'),
122  'href' => false,
123  'priority' => 999,
124  ]);
125 
126  foreach ($more as $item) {
127  $item->setParentName('more');
128  }
129 
130  $dropdown->setChildren($more);
131 
132  $registered[] = $dropdown;
133  }
134 
135  $menu->getSection('default')->fill($registered);
136 
137  return $menu;
138  }
139 }
if(!$items) $item
Definition: delete.php:13
$custom_menu_items
Definition: save.php:19
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
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:71
$num_menu_items
Definition: save.php:18
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 "/".
$menu
Form body for setting up site menu.
Definition: save.php:7
$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