Widget Manager: How to create/remove group widgets

Jeroen Dalsem's Widget Manager enables group owners to replace the group tools module block with selected widgets. This shows how to create a "Group Hello" widget placeable on group profile pages.

First, create the widget content view:

<?php // myPlugin/views/default/widgets/myPlugin_group_hello/content.php
$widget = $vars["entity"];
/* @var ElggWidget $widget */

$group = $widget->getOwnerEntity();
/* @var ElggGroup $group */

$count = (int) $widget->display_count;
if (! $count) {
$count = 5;
}

echo str_repeat("Hello!<br />", $count);

...and the edit view:

<?php // myPlugin/views/default/widgets/myPlugin_group_hello/edit.php

$widget = $vars['entity'];
/* @var ElggWidget $widget */

$count = (int) $widget->display_count;
if (! $count) {
$count = 5;
}
echo "<div>";
echo elgg_echo("widget:numbertodisplay");
echo elgg_view("input/dropdown", array("name" => "params[display_count]", "options" => range(1, 10), "value" => $count));
echo "</div>";

Now, we just need to register the widget. Do this at the end of the [init, system] event, and make sure the Widget Manager plugin is active:

<?php // in myPlugin/start.php

elgg_register_event_handler("init", "system", "myPlugin_setup_widgets", 999);

function myPlugin_setup_widgets() {
if (! elgg_is_active_plugin('widget_manager')) {
return;
}
elgg_register_widget_type(
"my_plugin_group_hello",
elgg_echo("myPlugin:widget:myPlugin_group_hello:title"),
elgg_echo("myPlugin:widget:myPlugin_group_hello:description"),
"groups");
}

Now run the Elgg upgrade and you should be able to add the widget to a group. All that's left is adding the appropriate language strings.

Let's say you want to disable some of the included widgets. You can simply use the elgg_unregister_widget_type function in your setup function:

function myPlugin_setup_widgets() {
if (! elgg_is_active_plugin('widget_manager')) {
return;
}
elgg_register_widget_type(
"myPlugin_group_hello",
elgg_echo("myPlugin:widget:myPlugin_group_hello:title"),
elgg_echo("myPlugin:widget:myPlugin_group_hello:description"),
"groups");
// remove some widgets
foreach (array('content_by_tag', 'image_slider') as $id) {
elgg_unregister_widget_type($id);
}
}

Navigation