Elgg  Version 4.3
navigation.php
Go to the documentation of this file.
1 <?php
89 function elgg_register_menu_item($menu_name, $menu_item) {
90  if (is_array($menu_item)) {
91  $options = $menu_item;
92  $menu_item = ElggMenuItem::factory($options);
93  if (!$menu_item instanceof ElggMenuItem) {
94  $menu_item_name = elgg_extract('name', $options, 'MISSING NAME');
95  elgg_log("Unable to add menu item '{$menu_item_name}' to '$menu_name' menu", 'WARNING');
96  return false;
97  }
98  }
99 
100  if (!$menu_item instanceof ElggMenuItem) {
101  elgg_log('Second argument of elgg_register_menu_item() must be an instance of '
102  . 'ElggMenuItem or an array of menu item factory options', 'ERROR');
103  return false;
104  }
105 
106  $menus = _elgg_services()->config->menus;
107  if (empty($menus)) {
108  $menus = [];
109  }
110 
111  $menus[$menu_name][] = $menu_item;
112  _elgg_services()->config->menus = $menus;
113 
114  return true;
115 }
116 
126 function elgg_unregister_menu_item($menu_name, $item_name) {
127  $menus = _elgg_services()->config->menus;
128  if (empty($menus)) {
129  return null;
130  }
131 
132  if (!isset($menus[$menu_name])) {
133  return null;
134  }
135 
136  foreach ($menus[$menu_name] as $index => $menu_object) {
137  /* @var \ElggMenuItem $menu_object */
138  if ($menu_object instanceof ElggMenuItem && $menu_object->getName() == $item_name) {
139  $item = $menus[$menu_name][$index];
140  unset($menus[$menu_name][$index]);
141  elgg_set_config('menus', $menus);
142  return $item;
143  }
144  }
145 
146  return null;
147 }
148 
165 function elgg_register_title_button($handler = null, $name = 'add', $entity_type = '', $entity_subtype = '') {
166 
168  if (!$owner) {
169  // noone owns the page so this is probably an all site list page
171  }
172 
173  if (($name === 'add') && ($owner instanceof ElggUser)) {
174  // make sure the add link goes to the current logged in user, not the page owner
175  $logged_in_user = elgg_get_logged_in_user_entity();
176  if (!empty($logged_in_user) && ($logged_in_user->guid !== $owner->guid)) {
177  // change the 'owner' for the link to the current logged in user
178  $owner = $logged_in_user;
179  }
180  }
181 
182  // do we have an owner and is the current user allowed to create content here
183  if (!$owner instanceof \ElggEntity || empty($entity_type) || empty($entity_subtype) || !$owner->canWriteToContainer(0, $entity_type, $entity_subtype)) {
184  return;
185  }
186 
187  $href = elgg_generate_url("$name:$entity_type:$entity_subtype", [
188  'guid' => $owner->guid,
189  ]);
190 
191  if (empty($href) && isset($handler)) {
192  elgg_deprecated_notice('Using handler for fallback HREF determination is no longer supported in ' . __METHOD__, '4.0');
193  }
194 
195  if (elgg_language_key_exists("$name:$entity_type:$entity_subtype")) {
196  $text = elgg_echo("$name:$entity_type:$entity_subtype");
197  } else {
198  $text = elgg_echo($name);
199  }
200 
201  // register the title menu item
202  elgg_register_menu_item('title', [
203  'name' => $name,
204  'icon' => $name === 'add' ? 'plus' : '',
205  'href' => $href,
206  'text' => $text,
207  'link_class' => 'elgg-button elgg-button-action',
208  ]);
209 }
elgg_language_key_exists($key, $language= 'en')
Check if a given language key exists.
Definition: languages.php:46
if(!$items) $item
Definition: delete.php:13
elgg_unregister_menu_item($menu_name, $item_name)
Remove an item from a menu.
Definition: navigation.php:126
$owner
Definition: upload.php:7
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: deprecation.php:52
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
$options
Elgg admin footer.
Definition: footer.php:6
static factory($options)
Create an ElggMenuItem from an associative array.
elgg_set_config($name, $value)
Set an Elgg configuration value.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
elgg_generate_url($name, array $parameters=[])
Generate a URL for named route.
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:23
elgg_register_menu_item($menu_name, $menu_item)
Elgg navigation library Functions for managing menus and other navigational elements.
Definition: navigation.php:89
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:24
$index
Definition: gallery.php:40
$handler
Definition: add.php:7
$text
Definition: button.php:32
elgg_register_title_button($handler=null, $name= 'add', $entity_type= '', $entity_subtype= '')
Convenience function for registering a button to the title menu.
Definition: navigation.php:165