Elgg entities are split into four types: ElggSites, ElggUsers, ElggObjects and ElggGroups. These all inherit the ElggEntity class, which provides some basic functionality (like GUID support) that spans all elements in the system.
Each ElggEntity has something called a container_guid. Each user in the system has a container; when you create a blog post in your blog, you're saving an ElggObject of subtype 'blog' to your container. When you upload a file to a group, you're saving an ElggObject of subtype 'file' to the group's container.
ElggUsers and ElggGroups both inherit an interface called Friendable, which established a core set of methods required for social functionality: isFriend() and so on. ElggGroups also emulate a username, which is currently set to group:NNN (where NNN is the group's GUID). This combination of factors means that an ElggGroup can now be the owner of a page, as returned by page_owner().
So whereas the following page returns the files owned by a particular user:
http:/
This one returns files that are part of a particular group:
http:/
This means that most of the work to make plugins work with groups is already done for you. The only difference comes when you're saving or deleting your elements.
The best practice is to have the form take in the container GUID on the URL line, eg:
http:/
This should 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);
The final piece of the puzzle is to add a link to your funcitonality from the group's profile. 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');
Done! With a few small alterations, your plugins can work as group functionality.
Last updated 527 days ago by Dave
