Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi all,
proceeding in my development as described here http://drupal.org/node/1818378, I'm trying now to develop the following feature:
If a user already belongs to a group, he cannot join to other groups.
So, to check the first part I can reuse a piece of code from http://drupal.org/node/1818378:
// Check if og_membership table already contains the user id:
$query = db_query('SELECT id FROM {og_membership} WHERE etid = :uid AND entity_type = :entity_type LIMIT 1', array(':uid' => _helper_get_current_user_id(), ':entity_type' => 'user'));
if($query->rowCount() > 0) {
// Yes, the user already belongs to a group, so deny group subscription:
***WHAT TO DO NOW?***
}
The problem is that I don't know where to put the code above (some OG hook ? a system hook? where?).
The final result should be an "access denied" when the user clicks on "Request membership" to join to a group.
Any suggestion?
Please help me!
Thank you very much
Comment | File | Size | Author |
---|---|---|---|
#15 | If a user cannot anymore create groups - node-add page should not be accessible.jpg | 147.62 KB | MXT |
#14 | subscribe.png | 29.01 KB | amitaibu |
#14 | 1818390-og-subscribe-14.patch | 1.62 KB | amitaibu |
#11 | 1818390-og-ui-subscribe-11.patch | 2.13 KB | amitaibu |
Comments
Comment #1
MXTI don't know: is there a way to hook the function og_ui_subscribe ?
In this way I could add my validation... and write a feedback message to the user similar to "You are already subscribed to a group, so you actually can't join this group, sorry"...
Comment #2
MXTDo you think I have to use hook_og_membership_presave ? Please tell me if it's the right way!
I wrote the following code:
The code above seems to work, but please, tell me if this is a good stuff!
Comment #3
thetech249 CreditAttribution: thetech249 commentedDid you get this to work? If so how? I am interested in doing this also.
Comment #4
tjhart87 CreditAttribution: tjhart87 commentedThe above code worked for me. How would we get the function to only act on specific roles though? If user has "role" then run the helper function?
Comment #5
amitaibuI don't think any special code is needed. If the og-audience field attached to the user is set to cardinality == 1; then OG won't let that user to subscribe to other groups. Remember that in 2.x you can have multiple OG-audience fields.
Comment #6
tjhart87 CreditAttribution: tjhart87 commentedI'm still running 7.x-1.5 ... should I make the upgrade now? Should I fear it too? haha
Comment #7
MXTThis is not true:
In /admin/config/people/accounts/fields I've tried to set cardinality == 1 to "Group membership " (og_user_group_ref) field with the following results:
Any suggestion?
Thank you very much for any help
Comment #8
amitaibu@MXT, please provide the exact link I should check based on the DB you provided.
Comment #9
MXTFor point 1:
For point 2:
The expected result:
Thank you very much for working on this
Comment #10
amitaibu> For point 2:
Point 2 works as expected. If the user can be subscribed only to 1 group, they are not allowed to create another one, as they will not be able to be subscribed to it.
I'm checking point 1.
Comment #11
amitaibu> Point 1
User should now get "You cannot register to this group, as you have reached your maximum allowed subscriptions." message.
Comment #12
MXTOk:
POINT 1:
I can confirm that the provided patch resolves the issue. Thank you!
POINT 2:
you said:
I completely agree with you, but user TEST2 was no subscribed to any group. So, it should be able to create at least ONE group.
I get a doubt now: how does the subscription mechanism work? I mean: if a user is a manager is ALSO a member so the cardinality is already reached? (1 subscription as manager + 1 subscription as member = 2 subscription?)
POINT 3NEW
I've noticed a minor issue: no "subscribe to this group" link is provided in any group page.
Comment #13
amitaibuI've committed #11
Comment #14
amitaibuPatch to address point 2, thanks for the explanation.
point 3 -- seems to work for me.
Comment #15
MXTGREAT WORK Amitaibu, all works fine now!!! GREAT!!! ;)
I have a suggestion to improve usability (if this doesn't request so much work for you obviously):
If a user has reached his creation group limit, group creation page (node/add) and related creation link in menu shouldn't appear at all (see screenshot)
Thank you!!!
Comment #16
amitaibuCommitted #14
> If a user has reached his creation group limit, group creation page (node/add) and related creation link in menu shouldn't appear at all (see screenshot)
Please open a separate issue for this.
Comment #17
MXTFollow up: http://drupal.org/node/1877812