Widget Manager: How to create/remove group widgets: Revision

Last updated by Steve Clay

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 // my_plugin/views/default/widgets/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 // my_plugin/views/default/widgets/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. To make sure we only register it when the Widget Manager plugin is present, we can hook into a custom event that the plugin triggers:

<?php // in my_plugin/start.php

function my_plugin_init() {
elgg_register_event_handler("widgets_init", "widget_manager", "my_plugin_register_widgets");
}

function my_plugin_register_widgets() {
elgg_register_widget_type("group_hello",
elgg_echo("my_plugin:widget:group_hello:title"),
elgg_echo("my_plugin:widget: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.