Elgg  Version 3.0
widgets.php
Go to the documentation of this file.
1 <?php
24  return _elgg_services()->widgets->getWidgets($owner_guid, $context);
25 }
26 
39  return _elgg_services()->widgets->createWidget($owner_guid, $handler, $context, $access_id);
40 }
41 
54  return _elgg_services()->widgets->canEditLayout($context, $user_guid);
55 }
56 
72 function elgg_register_widget_type($handler, $name = null, $description = null, $context = [], $multiple = false) {
73  if (is_array($handler)) {
75  } else {
76  $definition = \Elgg\WidgetDefinition::factory([
77  'id' => $handler,
78  'name' => $name,
79  'description' => $description,
80  'context' => $context,
81  'multiple' => $multiple,
82  ]);
83  }
84 
85  return _elgg_services()->widgets->registerType($definition);
86 }
87 
97  return _elgg_services()->widgets->unregisterType($handler);
98 }
99 
111  return _elgg_services()->widgets->validateType($handler, $context, $container);
112 }
113 
131  if (is_array($context)) {
132  $params = $context;
133  } else {
134  $params = [
135  'context' => $context,
136  'container' => null,
137  ];
138  }
139  return _elgg_services()->widgets->getTypes($params);
140 }
141 
153  $widget = elgg_extract('entity', $params);
154  if (!($widget instanceof \ElggWidget)) {
155  return;
156  }
157 
158  switch ($widget->handler) {
159  case 'content_stats':
160  return 'admin/statistics';
161  case 'cron_status':
162  return 'admin/cron';
163  case 'new_users':
164  return 'admin/users/newest';
165  case 'online_users':
166  return 'admin/users/online';
167  }
168 }
169 
176 function _elgg_widgets_init() {
177  elgg_register_plugin_hook_handler('entity:url', 'object', '_elgg_widgets_widget_urls');
178 }
179 
206  $default_widgets = elgg_trigger_plugin_hook('get_list', 'default_widgets', null, []);
207 
208  _elgg_config()->default_widget_info = $default_widgets;
209 
210  if (empty($default_widgets)) {
211  return;
212  }
213 
214  elgg_register_menu_item('page', [
215  'name' => 'default_widgets',
216  'text' => elgg_echo('admin:configure_utilities:default_widgets'),
217  'href' => 'admin/configure_utilities/default_widgets',
218  'section' => 'configure',
219  'parent_name' => 'configure_utilities',
220  'context' => 'admin',
221  ]);
222 
223  // override permissions for creating widget on logged out / just created entities
224  elgg_register_plugin_hook_handler('container_permissions_check', 'object', '_elgg_default_widgets_permissions_override');
225 
226  // only register the callback once per event
227  $events = [];
228  foreach ($default_widgets as $info) {
229  if (!is_array($info)) {
230  continue;
231  }
232  $event = elgg_extract('event', $info);
233  $entity_type = elgg_extract('entity_type', $info);
234  if (!$event || !$entity_type) {
235  continue;
236  }
237  if (!isset($events[$event][$entity_type])) {
238  elgg_register_event_handler($event, $entity_type, '_elgg_create_default_widgets');
239  $events[$event][$entity_type] = true;
240  }
241  }
242 }
243 
258  $default_widget_info = _elgg_config()->default_widget_info;
259 
260  if (empty($default_widget_info) || !$entity) {
261  return;
262  }
263 
264  $type = $entity->getType();
265  $subtype = $entity->getSubtype();
266 
267  // event is already guaranteed by the hook registration.
268  // need to check subtype and type.
269  foreach ($default_widget_info as $info) {
270  if (elgg_extract('entity_type', $info) !== $type) {
271  continue;
272  }
273 
274  $entity_subtype = elgg_extract('entity_subtype', $info);
275  if ($entity_subtype !== ELGG_ENTITIES_ANY_VALUE && $entity_subtype !== $subtype) {
276  continue;
277  }
278 
279  // need to be able to access everything
280  $old_ia = elgg_set_ignore_access(true);
281  elgg_push_context('create_default_widgets');
282 
283  // pull in by widget context with widget owners as the site
284  // not using elgg_get_widgets() because it sorts by columns and we don't care right now.
286  'type' => 'object',
287  'subtype' => 'widget',
288  'owner_guid' => elgg_get_site_entity()->guid,
289  'private_setting_name' => 'context',
290  'private_setting_value' => elgg_extract('widget_context', $info),
291  'limit' => 0,
292  'batch' => true,
293  ]);
294  /* @var \ElggWidget[] $widgets */
295 
296  foreach ($widgets as $widget) {
297  // change the container and owner
298  $new_widget = clone $widget;
299  $new_widget->container_guid = $entity->guid;
300  $new_widget->owner_guid = $entity->guid;
301 
302  // pull in settings
303  $settings = $widget->getAllPrivateSettings();
304 
305  foreach ($settings as $name => $value) {
306  $new_widget->$name = $value;
307  }
308 
309  $new_widget->save();
310  }
311 
312  elgg_set_ignore_access($old_ia);
314  }
315 }
316 
328  if ($type == 'object' && $params['subtype'] == 'widget') {
329  return elgg_in_context('create_default_widgets') ? true : null;
330  }
331 
332  return null;
333 }
334 
338 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
339  $events->registerHandler('init', 'system', '_elgg_widgets_init');
340  $events->registerHandler('ready', 'system', '_elgg_default_widgets_init');
341 };
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
_elgg_create_default_widgets($event, $type, $entity)
Creates default widgets.
Definition: widgets.php:257
_elgg_widgets_init()
Function to initialize widgets functionality.
Definition: widgets.php:176
$default_widgets
Definition: add.php:11
$access_id
Definition: access.php:11
$result
Definition: widgets.php:37
Events service.
$subtype
Definition: delete.php:22
elgg_is_widget_type($handler, $context=null,\ElggEntity $container=null)
Has a widget type with the specified handler been registered.
Definition: widgets.php:110
elgg_register_widget_type($handler, $name=null, $description=null, $context=[], $multiple=false)
Register a widget type.
Definition: widgets.php:72
elgg_get_widget_types($context="")
Get the widget types for a context.
Definition: widgets.php:130
$type
Definition: delete.php:21
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$user_guid
Validate a user.
Definition: validate.php:6
Base class for events and hooks.
$entity
Definition: reset.php:8
elgg_get_widgets($owner_guid, $context)
Get widgets for a particular context.
Definition: widgets.php:23
$container
Definition: delete.php:23
static factory(array $options)
Create an WidgetDefinition from an associative array.
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:52
elgg_can_edit_widget_layout($context, $user_guid=0)
Can the user edit the widget layout.
Definition: widgets.php:53
_elgg_widgets_widget_urls($hook, $type, $result, $params)
Returns widget URLS used in widget titles.
Definition: widgets.php:152
_elgg_default_widgets_permissions_override($hook, $type, $return, $params)
Overrides permissions checks when creating widgets for logged out users.
Definition: widgets.php:327
elgg_pop_context()
Removes and returns the top context string from the stack.
Definition: pageowner.php:222
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:238
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
_elgg_default_widgets_init()
Gets a list of events to create default widgets for and register menu items for default widgets with ...
Definition: widgets.php:205
if(!empty($screenshots)) $info
Definition: details.php:59
if(!$owner) if($owner->guid!=$page_owner->guid) $context
Definition: widgets.php:33
const ELGG_ENTITIES_ANY_VALUE
Definition: constants.php:24
elgg_register_menu_item($menu_name, $menu_item)
Register an item for an Elgg menu.
Definition: navigation.php:100
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
if($guid===false) $widget
Definition: add.php:30
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:1131
$widgets
Definition: widgets.php:35
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:449
_elgg_config()
Get the Elgg config service.
$value
Definition: debugging.php:7
elgg_push_context($context)
Push a context onto the top of the stack.
Definition: pageowner.php:212
$owner_guid
Definition: widgets.php:15
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$handler
Definition: add.php:7
if(elgg_view_exists("plugins/{$plugin_id}/settings")) $description
Definition: full.php:179
$settings
Definition: settings.php:3
elgg_unregister_widget_type($handler)
Remove a widget type.
Definition: widgets.php:96
elgg_create_widget($owner_guid, $handler, $context, $access_id=null)
Create a new widget instance.
Definition: widgets.php:38