My use case, so the title will make sense:
1) I have a field in a group post that changes according to the context (i.e. I show the members of a group in a selectlist - every group has different members).
2) I use services module to edit existing group posts.
Without the patch, the context of the posts are not set, thus my fields are incorrect. However, since we *edit* the post we have a way to know the context so we set it manually.
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 359291_22_og_set_context_for_post_edit.patch | 12.75 KB | amitaibu |
| #19 | 359291_19_og_set_context_for_post_edit.patch | 11.9 KB | amitaibu |
| #17 | 359291_17_og_set_context_for_post_edit.patch | 15.03 KB | amitaibu |
| #15 | 359291_14_og_set_context_for_post_edit.patch | 14.86 KB | amitaibu |
| #14 | 359291_14_og_set_context_for_post_edit.patch | 0 bytes | amitaibu |
Comments
Comment #1
moshe weitzman commentedThe context we choose when multiple groups are available actually varies based on the groups of $GLOBALS['user']. Perhaps refactor the code that does that and call that code here? Even after that, I'm not 100% I like this patch. og_form_alter() simply expects that some other code has set context and I'm not sure we should change that. I'm worried this will lead to similar 'defensive' code all over the place.
Comment #2
amitaibuwell, this issue encouraged me to dive into the whole OG's context. Here's what the patch does:
1) Use the menu system to find the context.
2) Reorganize code, for example - move away from og_set_theme() the part that gets the $group_node.
I haven't tested the Views AJAX and I have one thing I didn't understand what it's supposed to do - what is this menu path?!:
As for the original patch, I think maybe the way to do it is having a service to og_set_conext() - I'll create a different issue for it.
Comment #3
moshe weitzman commentedThis is a very promising patch. I think you can simplify further by calling menu_get_object() on various paths. I can explain more in IRC if needed.
Comment #4
amitaibuI've started with using menu_get_object() however it served me only on node view/edit (The comment for example doesn't load) so in the end I thought I'd better just use menu_get_item() for consistency.
Comment #5
amitaibuactually, I'll re-roll. menu_get_object() even for the node view/ edit is cleaner.
Comment #6
amitaibuOk, late at night, so I hope there aren't too many errors...
Changes from last patch:
1) Use menu_get_object() for existing nodes.
2) og_form_alter() is trying to be more friendly, which means it checks if the user clicked the 'Preview' button and a group was selected; Or if the user has a single group and audience is required.
3) simpleTest :)
Comment #7
amitaibuSome more changes:
* form_alter() tries to set the context only if no context was already set - like this we don't override other modules set_context().
* I've removed the views_ajax() part. We should find another solution rather than relying on the menu system, since the path is 'views/ajax' and unfortunately menu_get_object() returns nothing. maybe we can use hook_view_pre_render(), but I think this can be done in a following patch.
Comment #8
amitaibuSome more fine tuning on the OG context selection 1 + 4 added:
Comment #9
amitaibuhmm, I found a better way to do this, will re-roll soon.
Comment #10
amitaibuOk, now if user passes ?gids[]=X, system checks user is a member or 'administer organic groups'.
Comment #11
moshe weitzman commentedFor a path like og/manage/%node, you can use menu_get_object('node', 2). Same for comment/reply. I'd like to use as little $item stuff as possible. It looks uglier to my eyes.
I will take a closer look soon. Thanks for the new test.
Comment #12
amitaibuDone.
Comment #13
amitaibuOops, wrong patch :/, sorry.
Comment #14
amitaibuRe-rolled.
Comment #15
amitaibuAnd again.
Comment #16
amitaibuWorking on Mosh'es comments from the IRC, and make sure the test pass.
Comment #17
amitaibuog_set_group_context() now doesn't try to determine itself the group node.
All tests pass.
Comment #18
moshe weitzman commentedPatch review ...
if (is_object($group_node) && og_is_group_type($group_node->type)) {I'd like to commit just "Update Og's context to use the menu system" and then we can talk about other changes. I will pick out these changes and commit.
Comment #19
amitaibuI've kept only the 'menu system' part and addressed your comments.
Comment #20
moshe weitzman commentedThanks ... Why are we deleting the code related to " // Node is in multiple groups. Preference goes to the group we showed on the prior page view (if any),
- // Then to a group the current user is a member of."
This is still valid, no?
Comment #21
moshe weitzman commentedComment #22
amitaibuOne day I will get it done correctly right on the first patch ;)
Comment #23
moshe weitzman commentedI reworked this some more and committed. I moved settng of $_SESSION['og_last'] to end of page and removed user_access('administer nodes') check from the last setting of context in the helper function.
Thanks.
I would love to add tests for the cases when post is in multiple groups, and when the post is or is not in the user's groups (i.e. the various cases in og_determine_context_get_group())