| 1 |
*Widget API* |
|---|
| 2 |
|
|---|
| 3 |
The Elgg widget module is designed to have a very simple API with as much as |
|---|
| 4 |
possible handled by the widget module itself. |
|---|
| 5 |
|
|---|
| 6 |
If you want to create a plugin that supplies widgets, then your module needs |
|---|
| 7 |
to provide two functions to generate HTML that displays and edits your |
|---|
| 8 |
widgets: |
|---|
| 9 |
|
|---|
| 10 |
module_widget_display($widget) |
|---|
| 11 |
|
|---|
| 12 |
which displays the widget pointed to by $widget |
|---|
| 13 |
|
|---|
| 14 |
and |
|---|
| 15 |
|
|---|
| 16 |
module_widget_edit($widget) |
|---|
| 17 |
|
|---|
| 18 |
which generates an edit form for the widget pointed to by $widget. |
|---|
| 19 |
|
|---|
| 20 |
In both cases, "module" should be replaced by the name of your module. |
|---|
| 21 |
|
|---|
| 22 |
Your edit form should not be a complete form - it can include some |
|---|
| 23 |
introductory text and the fields you want to edit (with the names |
|---|
| 24 |
widget_data[field_name], where "field_name" is the name of the field you want |
|---|
| 25 |
to edit). It should not include a "form" tag or submit button - these will be |
|---|
| 26 |
added automatically. |
|---|
| 27 |
|
|---|
| 28 |
You can see examples of display and edit functions in the widget module itself |
|---|
| 29 |
in mod/widget/lib.php. They, of course, are called widget_widget_display and |
|---|
| 30 |
widget_widget_edit. |
|---|
| 31 |
|
|---|
| 32 |
When you are creating the HTML to display and edit your widgets, you can use |
|---|
| 33 |
the widget_get_data($field_name,$widget->ident) function to get any data |
|---|
| 34 |
associated with your widget. |
|---|
| 35 |
|
|---|
| 36 |
If you want to register your widgets publicly and allow users to add them to |
|---|
| 37 |
their dashboards, then you can add a widget declaration to your module_init |
|---|
| 38 |
function (replace "module" in module_init by the name of your module). |
|---|
| 39 |
|
|---|
| 40 |
Here is the example from the widget module itself, which registers a simple |
|---|
| 41 |
text widget. |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
$CFG->widgets->list[] = array( |
|---|
| 45 |
'name' => __gettext("Text box"), |
|---|
| 46 |
'description' => __gettext("Displays the text of your choice."), |
|---|
| 47 |
'type' => "text", |
|---|
| 48 |
'module' => "widget" |
|---|
| 49 |
); |
|---|
| 50 |
|
|---|
| 51 |
You can register multiple widget types for the same module and can access this |
|---|
| 52 |
information through the $widget->widget_type field when displaying or editing |
|---|
| 53 |
your widget. |
|---|
| 54 |
|
|---|
| 55 |
If all you want to do is register widgets with the Elgg dashboard, then this |
|---|
| 56 |
is probably all the API that you need to know. |
|---|
| 57 |
|
|---|
| 58 |
*Dashboard-like Pages* |
|---|
| 59 |
|
|---|
| 60 |
The widget system has more functionality that supports adding dashboard-like |
|---|
| 61 |
pages to your own module. |
|---|
| 62 |
|
|---|
| 63 |
If you want to display widgets on your own module page, then you need to |
|---|
| 64 |
provide another function: |
|---|
| 65 |
|
|---|
| 66 |
module_widget_display_url() |
|---|
| 67 |
|
|---|
| 68 |
(where "module" is the name of your module), which returns the URL of the |
|---|
| 69 |
page in your module responsible for displaying widgets. |
|---|
| 70 |
|
|---|
| 71 |
Eg. $CFG->wwwroot.'mod/module/view_widgets.php' |
|---|
| 72 |
|
|---|
| 73 |
You can use: |
|---|
| 74 |
|
|---|
| 75 |
widget_create($module,$location,$location_id,$type,$owner,$access,$display_order) |
|---|
| 76 |
|
|---|
| 77 |
to create your widget, where |
|---|
| 78 |
|
|---|
| 79 |
- $module is the name of your module |
|---|
| 80 |
- $location is an arbitrary string to describe the widget location |
|---|
| 81 |
- $location_id is an arbitrary number to describe the widget location |
|---|
| 82 |
(you can use location, location_id, or both, depending upon your |
|---|
| 83 |
application) |
|---|
| 84 |
- $type - the type of widget that you are creating |
|---|
| 85 |
|
|---|
| 86 |
The remaining 3 parameters are optional: |
|---|
| 87 |
|
|---|
| 88 |
- $owner is the user_id of the owner of the widget - if you do not provide |
|---|
| 89 |
this or set it to 0, then the widget will be owned by the current logged in |
|---|
| 90 |
user |
|---|
| 91 |
|
|---|
| 92 |
- $access - defaults to 'PUBLIC' |
|---|
| 93 |
|
|---|
| 94 |
- $display order - defaults to first if not provided or set to 0. You |
|---|
| 95 |
can set this to a large number (eg. 10000) if you want this to be the |
|---|
| 96 |
last widget displayed. The widgets are reshuffled as soon as they are |
|---|
| 97 |
inserted, so you can safely use 0 or 10000 each time you are creating a |
|---|
| 98 |
widget and it will always go first or at the end respectively. |
|---|
| 99 |
|
|---|
| 100 |
widget_create returns $widget->ident (the widget id). |
|---|
| 101 |
|
|---|
| 102 |
Note: the Elgg dashboard uses location = "dashboard" and location_id = 0. |
|---|
| 103 |
So creating a widget with these parameters is another way to place a |
|---|
| 104 |
widget on the owner's dashboard. Currently the dashboard system does |
|---|
| 105 |
not use dashboards with location_id set to something other than 0, but |
|---|
| 106 |
reserves the right to do so in future. So if you want to create your |
|---|
| 107 |
own dashboard-like pages, use a location other than "dashboard". |
|---|
| 108 |
|
|---|
| 109 |
To add or change widget data fields, you can use: |
|---|
| 110 |
|
|---|
| 111 |
widget_set_data($field_name, $widget->ident, $field_value) |
|---|
| 112 |
|
|---|
| 113 |
If the field_name record does not exist, it will be created when you set its |
|---|
| 114 |
value. |
|---|
| 115 |
|
|---|
| 116 |
To display widgets, you can use: |
|---|
| 117 |
|
|---|
| 118 |
widget_page_display($owner,$location,$location_id,$count,$collapsed) |
|---|
| 119 |
|
|---|
| 120 |
All the parameters are optional. widget_page_display() with no parameters |
|---|
| 121 |
returns all of the widgets of the currently logged-in user. |
|---|
| 122 |
|
|---|
| 123 |
If you set $owner to zero, the currently-logged-in user will be used as well. |
|---|
| 124 |
|
|---|
| 125 |
$location and $location_id can be used to display widgets associated with |
|---|
| 126 |
particular locations. |
|---|
| 127 |
|
|---|
| 128 |
$count is used to determine the number of widgets that should be displayed per |
|---|
| 129 |
page. Set this to 0 if you want all the widgets to be displayed on the same page. |
|---|
| 130 |
|
|---|
| 131 |
$collapsed determines if you want the widgets to be displayed in collapsed |
|---|
| 132 |
view (one line per widget). Set this to 1 if you want a collapsed view. |
|---|
| 133 |
|
|---|
| 134 |
If you want to display collapsed widgets, your module needs to supply another |
|---|
| 135 |
function, |
|---|
| 136 |
|
|---|
| 137 |
module_widget_display_collapsed($widget) |
|---|
| 138 |
|
|---|
| 139 |
which displays the collapsed widget pointed to by $widget |
|---|
| 140 |
|
|---|
| 141 |
(where "module" should be replaced by the name of your module). |
|---|
| 142 |
|
|---|
| 143 |
Keep in mind that various links to edit, delete and move your widget will |
|---|
| 144 |
appear on the same line, so your actual collapsed display area needs to be |
|---|
| 145 |
restricted to about 350 pixels or less. |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|