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.

Comments

moshe weitzman’s picture

Status: Needs review » Needs work

The 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.

amitaibu’s picture

Title: Try to set context when editing a group post if no context found. » Update Og's context to use the menu system
Status: Needs work » Needs review
StatusFileSize
new8.18 KB

well, 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?!:

  elseif (arg(0, $path) == 'node' && arg(1, $path) == 'add' && arg(2, $path) == 'book' && arg(3, $path) == 'parent') {
    $group_node = og_set_theme(arg(4, $path));
    $_REQUEST['edit']['og_groups'][] = $group_node->nid; // checks the right box on node form
  }

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.

moshe weitzman’s picture

Status: Needs review » Needs work

This 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.

amitaibu’s picture

I'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.

amitaibu’s picture

actually, I'll re-roll. menu_get_object() even for the node view/ edit is cleaner.

amitaibu’s picture

Status: Needs work » Needs review
StatusFileSize
new14.34 KB

Ok, 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 :)

amitaibu’s picture

StatusFileSize
new14.96 KB

Some 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.

amitaibu’s picture

StatusFileSize
new15.11 KB

Some more fine tuning on the OG context selection 1 + 4 added:

+ // Group post belongs to multiple groups. Selection order:
+ // 1) URL has gids[] and user is a group member.
+ // 2) The group we showed on the prior page view (if any).
+ // 3) The only or one of the group(s) the current user is a member of.
+ // 4) The first group, if the user has 'administer organic groups'

amitaibu’s picture

Status: Needs review » Needs work

hmm, I found a better way to do this, will re-roll soon.

amitaibu’s picture

Status: Needs work » Needs review
StatusFileSize
new15.19 KB

Ok, now if user passes ?gids[]=X, system checks user is a member or 'administer organic groups'.

moshe weitzman’s picture

For 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.

amitaibu’s picture

For a path like og/manage/%node, you can use menu_get_object('node', 2).

Done.

amitaibu’s picture

Oops, wrong patch :/, sorry.

amitaibu’s picture

Re-rolled.

amitaibu’s picture

And again.

amitaibu’s picture

Status: Needs review » Needs work

Working on Mosh'es comments from the IRC, and make sure the test pass.

amitaibu’s picture

Status: Needs work » Needs review
StatusFileSize
new15.03 KB

og_set_group_context() now doesn't try to determine itself the group node.
All tests pass.

moshe weitzman’s picture

Patch review ...

  1. In og_set_context we remove this protection which I think is useful: if (is_object($group_node) && og_is_group_type($group_node->type)) {
  2. remove global $user from determine_context()
  3. "// Group post belongs to multiple groups.". Now we enter thse case even when there is 1 group
  4. The 1,2,3,4 logic for determining context changes in this patch. Thats beyond the scope of "Update Og's context to use the menu system". I have to think about a querystring param 'gids' which works everywhere to set context. I don't think that will play well with Views pages which set context via an Argument.
  5. the form_alter is out of scope too. i'm not too clear about why an edit would have no context. the group had no posts and you are previewing?

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.

amitaibu’s picture

I've kept only the 'menu system' part and addressed your comments.

moshe weitzman’s picture

Thanks ... 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?

moshe weitzman’s picture

Status: Needs review » Needs work
amitaibu’s picture

Status: Needs work » Needs review
StatusFileSize
new12.75 KB

One day I will get it done correctly right on the first patch ;)

moshe weitzman’s picture

Status: Needs review » Fixed

I 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())

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.