Group

A group in Elgg is an entity that users can join, leave and post content to. In Elgg 0.x, groups were called communities.

A group acts as a container for other entities, and has other methods provided by ElggGroup in order to manage content and membership.

Contents

ElggGroup and default groups

It's important to draw a distinction between the default groups supplied in the Elgg distribution - which have profile pages, forums and draw content to the front - and ElggGroup, the ElggEntity specialization class that provides group-level functionality.

The default group as provided by Elgg is an example. You can certainly use it effectively, but you also have the ability to write very different group functionality using the same underlying methods. Other examples might include an event, a class or a cause. Because ElggGroup can be subtyped like all other ElggEntities, you can have multiple types of group running on the same site.

Writing a group-aware plugin

Plugin owners need not worry too much about writing group-aware functionality, but there are a few key points:

Uploading content

Use can_write_to_container to determine whether or not the current user has the right to upload/post. Be aware that you will then need to pass the container GUID or username to the page responsible for posting and the accompanying value, so that this can then be stored in your form as a hidden input field, for easy passing to your actions. Within a "create" action, you'll need to take in this input field and save it as a property of your new element (defaulting to the current user's container):

$container_guid = get_input('container_guid', $_SESSION['user']->getGUID());
$new_element = new ElggObject;
$new_element->container_guid = $container_guid;

When it comes time to redirect to a page, once your action has finished, you'll need to get the container's username:

$container_entity = get_entity($container_guid);
forward($CONFIG->wwwroot . 'pg/file/' . $container_entity->username);

Usernames and page ownership

Groups have a simulated username of the form group:GUID, which you can get the value of by checking $group->username. If you pass this username to a page on the URL line as part of the username variable (i.e., /yourpage?username=group:nnn), Elgg will automatically register that group as being the owner of the page (unless overridden).

Juggling users and groups

In fact, ElggGroup simulates most of the methods of ElggUser. You can grab the icon, name etc using the same calls, and if you ask for a group's friends, you'll get its members. This has been designed specifically for you to alternate between groups and users in your code easily.

Menu options

The final piece of the puzzle, for default groups, is to add a link to your functionality from the group's profile. Here we'll use the file plugin as an example.

This involves creating a view within your plugin - in this case file/menu - which will extend the group's menu. File/menu consists of a link within paragraph tags that points to the file repository of the page_owner():

<p><a href="<?php echo $vars['url']; ?>pg/file/<?php echo page_owner_entity()->username; ?>"><?php echo elgg_echo("file"); ?></p>

You can then extend the group's menu view with this one, within your plugin's input function (in this case file_init):

extend_view('groups/menu/links', 'file/menu');

Also see

Search docs