Merge entities in one timeline (blog posts, for instance)

Hi all!

I'm currently working on extending the functionality of the blog widget, in the following way:

If the widget is in the users profile, then it will show only their own post.
If the widget is in the user's dashboard, then it will show a timeline with the last posts from their friends, and his own posts.

The problem is that I'm able to retrieve the list of posts from the user (is already done in the plugin), and also to retrieve also the friends' blog posts, by using the code available in the friends tab, based on the function elgg_list_entities_from_relationship; but I'm unable to sort that in a timeline.

I've tried to use the "where" parameter of this function, but unfortunately I've ended up with wrong queries, that lasts more than 30 secs to work. I've drilled down to the core, and found out that the function getEntityRelationshipWhereSQL uses AND as a query parameters separator for "where" (in RelationshipsTable.php, line 401), so it seems that I'm not following the correct aproach for getting the timeline working.

Any help/hint is welcomed!

Thanks in advance, and Merry Christmas!

Nalonso

  • I've never tried it but you can:

    $widget = elgg_extract('entity', $vars);
    
    $count = sanitise_int($widget->num_display, false);
    if ($count < 1) {
        $count = 4;
    }
    
    $user = elgg_get_logged_in_user_entity();
    $user_guid = elgg_get_logged_in_user_guid()
        
    $friends = $user->getFriends(['limit' => 0]);
    $friends_guids = array();
    
    foreach ($friends as $friend) {
      $friends_guids[] = $friend->guid;
    }
    
    $options = [
        'type' => 'object',
        'subtype' => 'blog',
        'owner_guids' => [$user_guid, $friends_guids],
        'limit' => $count,
        'full_view' => false,
        'pagination' => false,
    ];
    
    $content = elgg_list_entities($options);
    
    echo $content;
  • Thanks a lot RvR!

    The solution worked almost like you suggested! The only change I have to do was:


    $widget = elgg_extract('entity', $vars);

    $count = sanitise_int($widget->num_display, false);
    if ($count < 1) {
    $count = 4;
    }

    $user = elgg_get_logged_in_user_entity();
    $user_guid = elgg_get_logged_in_user_guid()

    $friends = $user->getFriends(['limit' => 0]);
    $friends_guids = array();

    foreach ($friends as $friend) {
    $friends_guids[] = $friend->guid;
    }

    // Changed, because when we do [$user_guid, $friends_guids] and you have more than one friend
    // the structure was: [int, [int, int]], and should be [int, int, int]

    $friends_guids[] = $user_guid;

    $options = [
    'type' => 'object',
    'subtype' => 'blog',
    'owner_guids' => $friends_guids,
    'limit' => $count,
    'full_view' => false,
    'pagination' => false,
    ];

    $content = elgg_list_entities($options);

    echo $content;

    Just for the record, this changed should be done in /mod/blog/views/default/widgets/blog/content.php. If anyone is interested in the rest of the code, it will be my pleasure to share it!

    Thanks again!!!