Elgg  Version master
widgets.php
Go to the documentation of this file.
1 <?php
12 $num_columns = elgg_extract('num_columns', $vars, 3);
13 $show_add_widgets = elgg_extract('show_add_widgets', $vars, true);
14 $show_access = elgg_extract('show_access', $vars, true);
15 $owner_guid = (int) elgg_extract('owner_guid', $vars);
16 
18 if ($owner_guid) {
20 } else {
22 }
23 
24 if (!$owner instanceof \ElggEntity) {
25  return;
26 }
27 
29 if (elgg_is_empty($context)) {
30  return;
31 }
32 
33 elgg_require_js('elgg/widgets');
34 
35 // Underlying views and functions assume that the page owner is the owner of the widgets
36 if ($owner->guid != $page_owner->guid) {
38 }
39 
41 
42 $result = '';
43 $no_widgets = elgg_extract('no_widgets', $vars);
44 if (empty($widgets) && !empty($no_widgets)) {
45  if ($no_widgets instanceof \Closure) {
46  echo $no_widgets();
47  } else {
49  }
50 }
51 
53  $result .= elgg_view('page/layouts/widgets/add_button', $vars);
54 }
55 
56 // push context after the add_button as add button uses current context
57 elgg_push_context('widgets');
58 
59 if (!empty($widgets)) {
61  'context' => $context,
62  'container' => $owner,
63  ]);
64 }
65 
66 // move hidden columns widgets to last visible column
67 if (!isset($widgets[$num_columns])) {
68  $widgets[$num_columns] = [];
69 }
70 
71 foreach ($widgets as $index => $column_widgets) {
72  if ($index <= $num_columns) {
73  continue;
74  }
75 
76  // append widgets to last column and retain order
77  foreach ($column_widgets as $column_widget) {
78  $widgets[$num_columns][] = $column_widget;
79  }
80 
81  unset($widgets[$index]);
82 }
83 
84 $grid = '';
85 for ($column_index = 1; $column_index <= $num_columns; $column_index++) {
86  $column_widgets = (array) elgg_extract($column_index, $widgets, []);
87 
88  $widgets_content = '';
89  foreach ($column_widgets as $widget) {
90  if (!array_key_exists($widget->handler, $widget_types)) {
91  continue;
92  }
93 
94  $widgets_content .= elgg_view_entity($widget, [
95  'show_access' => $show_access,
96  'register_rss_link' => false,
97  ]);
98  }
99 
100  $grid .= elgg_format_element('div', [
101  'id' => "elgg-widget-col-{$column_index}",
102  'class' => [
103  'elgg-widgets',
104  ],
105 
106  ], $widgets_content);
107 }
108 
109 $result .= elgg_format_element('div', [
110  'class' => 'elgg-widgets-grid',
111 ], $grid);
112 
114 
115 echo elgg_format_element('div', [
116  'class' => [
117  'elgg-layout-widgets',
118  "elgg-layout-widgets-{$context}",
119  ],
120  'data-page-owner-guid' => $owner->guid,
121 ], $result);
122 
123 // Restore original page owner
124 if ($owner->guid != $page_owner->guid) {
126 }
$widget_types
Definition: add_panel.php:33
$owner
Definition: upload.php:7
$no_widgets
Definition: widgets.php:43
if(!empty($widgets)) if(!isset($widgets[$num_columns])) foreach($widgets as $index=> $column_widgets) $grid
Definition: widgets.php:84
$num_columns
Elgg widgets layout.
Definition: widgets.php:12
elgg_can_edit_widget_layout(string $context, int $user_guid=0)
Can the user edit the widget layout.
Definition: widgets.php:50
if($owner->guid!=$page_owner->guid) $widgets
Definition: widgets.php:40
elgg_set_page_owner_guid(int $guid)
Set the guid of the entity that owns this page.
Definition: pageowner.php:34
if(!$owner instanceof\ElggEntity) $context
Definition: widgets.php:28
elgg_get_context()
Get the current context.
Definition: context.php:41
elgg_is_empty($value)
Check if a value isn&#39;t empty, but allow 0 and &#39;0&#39;.
Definition: input.php:176
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
elgg_pop_context()
Removes and returns the top context string from the stack.
Definition: context.php:62
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:177
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:67
elgg_get_widget_types(string|array $context= '')
Get the widget types for a context.
Definition: widgets.php:112
elgg_view_entity(\ElggEntity $entity, array $vars=[])
Returns a string of a rendered entity.
Definition: views.php:514
$page_owner
Definition: widgets.php:17
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:23
$result
Definition: widgets.php:42
if($guid===false) $widget
Definition: add.php:31
elgg_push_context(string $context)
Push a context onto the top of the stack.
Definition: context.php:52
elgg_get_widgets(int $owner_guid, string $context)
Elgg widgets library.
Definition: widgets.php:20
$owner_guid
Definition: widgets.php:15
$vars['head']
Definition: html.php:24
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:130
$show_access
Definition: widgets.php:14
$index
Definition: gallery.php:40
elgg_require_js(string $name)
Request that Elgg load an AMD module onto the page.
$show_add_widgets
Definition: widgets.php:13