Skip to:
Content

BuddyPress.org

Opened 4 years ago

Last modified 3 years ago

#6749 new defect (bug)

bp_get_group_avatar() needs to work outside the loop

Reported by: slaFFik Owned by:
Milestone: Awaiting Contributions Priority: normal
Severity: normal Version:
Component: Groups Keywords:
Cc:

Description

Currently bp_get_group_avatar() function doesn't check $groups_template global var, although using it extensively.
If you try to use this function not in a loop scope - it will generate lots of notices.

It makes it impossible to use outside of the loop more relevant bp_get_group_avatar() comparing to bp_core_fetch_avatar().

Change History (5)

#1 @boonebgorges
4 years ago

  • Keywords 2nd-opinion added
  • Milestone changed from Awaiting Review to Under Consideration

I agree that there should be a way to get a group avatar outside of a group loop.

I'm not 100% sure that bp_get_group_avatar() is the way to do it. Our -template.php functions are pretty inconsistent throughout BuddyPress. In some places, these functions accept an argument so that they can be used outside of the loop, and in other places they do not. Many of the bp-groups-template.php functions, for example, accept a $group object. It's easy for us to add this functionality to bp_get_group_avatar(), but this might also be a good time to take a moment and see whether we'd like to take a step toward greater consistency here. What if, for example, we introduced a function like this:

function bp_get_group_from_param( $group = false ) {
    global $groups_template;

    $group_obj = null;

    if ( $group instanceof BP_Groups_Group ) { // or is_object()
        $group_obj = $group;
    } elseif ( is_numeric( $group ) ) {
        $group_obj = groups_get_group( array( 'group_id' => $group ) );
        // maybe some error checking to make sure the group exists
    } elseif ( isset( $groups_template->group ) ) {
        $group_obj = $groups_template->group;
    }

    return $group_obj;
}

Then, within each template function:

function bp_get_group_avatar( $group = false ) {
    if ( ! $group = bp_get_group_from_param( $group ) ) {
        return '';
    }

    // Now we can assume $group is correct
}

This way, devs have the option of passing a group object, passing a group ID, or passing nothing and having the group determined from the group loop context.

#2 @r-a-y
4 years ago

The bp_get_group_from_param() function would be pretty awesome.

Also, check out a related, crappy patch here - ticket:6387#comment:6 - that only adds groups_get_current_group() as a fallback and doesn't do any group lookups.

#3 @DJPaul
3 years ago

  • Component changed from Tools - Warnings/Notices to Core

#4 @DJPaul
3 years ago

  • Milestone changed from Under Consideration to Awaiting Review

#5 @DJPaul
3 years ago

  • Component changed from Core to Groups
  • Keywords 2nd-opinion removed
  • Milestone changed from Awaiting Review to Future Release
  • Summary changed from bp_get_group_avatar() needs extra error checking to bp_get_group_avatar() needs to work outside the loop
Note: See TracTickets for help on using tickets.