Permalink
Browse files

feature(groups): profile buttons can now be filtered with a hook

Due to the limitations of the title menu setup, plugins are unaware of the group
entity that the menu items in the register hook refer to. This allows plugins
to filter profile buttons on per group basis.
  • Loading branch information...
hypeJunction committed Dec 23, 2015
1 parent 548f9e2 commit 52e82943f64a2dd0883866f0e5ec97230990f319
Showing with 73 additions and 38 deletions.
  1. +6 −0 docs/guides/hooks-list.rst
  2. +9 −38 mod/groups/lib/groups.php
  3. +58 −0 mod/groups/start.php
@@ -478,6 +478,12 @@ Embed
**embed_get_upload_sections, all**
Groups
------
**profile_buttons, group**
Filters buttons (``ElggMenuItem`` instances) to be registered in the title menu of the group profile page
HTMLawed
--------
View
@@ -7,48 +7,19 @@
* Registers the buttons for title area of the group profile page
*
* @param ElggGroup $group
* @return void
*/
function groups_register_profile_buttons($group) {
$actions = array();
$params = [
'entity' => $group,
];
// group owners
if ($group->canEdit()) {
// edit and invite
$url = elgg_get_site_url() . "groups/edit/{$group->getGUID()}";
$actions[$url] = 'groups:edit';
$url = elgg_get_site_url() . "groups/invite/{$group->getGUID()}";
$actions[$url] = 'groups:invite';
}
// group members
if ($group->isMember(elgg_get_logged_in_user_entity())) {
if ($group->getOwnerGUID() != elgg_get_logged_in_user_guid()) {
// leave
$url = elgg_get_site_url() . "action/groups/leave?group_guid={$group->getGUID()}";
$url = elgg_add_action_tokens_to_url($url);
$actions[$url] = 'groups:leave';
}
} elseif (elgg_is_logged_in()) {
// join - admins can always join.
$url = elgg_get_site_url() . "action/groups/join?group_guid={$group->getGUID()}";
$url = elgg_add_action_tokens_to_url($url);
if ($group->isPublicMembership() || $group->canEdit()) {
$actions[$url] = 'groups:join';
} else {
// request membership
$actions[$url] = 'groups:joinrequest';
}
}
if ($actions) {
foreach ($actions as $url => $text) {
elgg_register_menu_item('title', array(
'name' => $text,
'href' => $url,
'text' => elgg_echo($text),
'link_class' => 'elgg-button elgg-button-action',
));
$items = elgg_trigger_plugin_hook('profile_buttons', 'group', $params, []);
if (!empty($items)) {
foreach ($items as $item) {
elgg_register_menu_item('title', $item);
}
}
}
View
@@ -109,6 +109,9 @@ function groups_init() {
// allow to be liked
elgg_register_plugin_hook_handler('likes:is_likable', 'group:', 'Elgg\Values::getTrue');
// prepare profile buttons to be registered in the title menu
elgg_register_plugin_hook_handler('profile_buttons', 'group', 'groups_prepare_profile_buttons');
}
/**
@@ -812,3 +815,58 @@ function groups_members_menu_setup($hook, $type, $menu, $params) {
return $menu;
}
/**
* Returns menu items to be registered in the title menu of the group profile
*
* @param string $hook "profile_buttons"
* @param string $type "group"
* @param ElggMenuItem[] $items Buttons
* @param array $params Hook params
* @return ElggMenuItem[]
*/
function groups_prepare_profile_buttons($hook, $type, $items, $params) {
$group = elgg_extract('entity', $params);
if (!$group instanceof ElggGroup) {
return;
}
$actions = [];
if ($group->canEdit()) {
// group owners can edit the group and invite new members
$actions['groups:edit'] = "groups/edit/{$group->guid}";
$actions['groups:invite'] = "groups/invite/{$group->guid}";
}
$user = elgg_get_logged_in_user_entity();
if ($user && $group->isMember($user)) {
if ($group->owner_guid != $user->guid) {
// a member can leave a group if he/she doesn't own it
$actions['groups:leave'] = "action/groups/leave?group_guid={$group->guid}";
}
} else if ($user) {
$url = "action/groups/join?group_guid={$group->guid}";
if ($group->isPublicMembership() || $group->canEdit()) {
// admins can always join
// non-admins can join if membership is public
$actions['groups:join'] = $url;
} else {
// request membership
$actions['groups:joinrequest'] = $url;
}
}
foreach ($actions as $action => $url) {
$items[] = ElggMenuItem::factory(array(
'name' => $action,
'href' => elgg_normalize_url($url),
'text' => elgg_echo($action),
'is_action' => 0 === strpos($url, 'action'),
'link_class' => 'elgg-button elgg-button-action',
));
}
return $items;
}

0 comments on commit 52e8294

Please sign in to comment.