Comments only for friends

This is the code for the comments box that some plugins use:

 

if (isset($vars['entity']) && isloggedin()) {

$form_body = "<div class=\"contentWrapper\"><p class='longtext_editarea'><label>".elgg_echo("generic_comments:text")."<br />" . elgg_view('input/longtext',array('internalname' => 'generic_comment')) . "</label></p>";

$form_body .= "<p>" . elgg_view('input/hidden', array('internalname' => 'entity_guid', 'value' => $vars['entity']->getGUID()));

$form_body .= elgg_view('input/submit', array('value' => elgg_echo("post"))) . "</p></div>";

 

echo elgg_view('input/form', array('body' => $form_body, 'action' => "{$vars['url']}action/comments/add"));

 

}

 

 

Now I want to make that comment box only available for friends. I know I need to add a variable to this code:

if (isset($vars['entity']) && isloggedin())

(add an && isaFriend) but I don't know how to do it or if there's a variable that has the relationship type that I can use to make this work.

 

Any help will be appreciatted

 

  • Ok, So I found the isFriend() function, but how do I call it from root/views/default/comments/form/edit.php

  • You need the user object of the person you're checking, and isFriend() will tell you if they're a friend of the currently logged in user.

    For example, code to see if you are a friend would be something like this:

    $user = get_user_by_username("rjcalifornia");

    if($user->isFriend()){

              echo "we are friends";

    }

    else{

              echo "we are not friends";

    }

     

    Note that the isFriend() method only checks for a friend of the currently logged in user.  If you want to check for other users there are other methods on that same reference page.

     

    So for your exmple I'm assuming you mean that you want the form to be accessible to the friends of the content owner.  Something like this should work:

    $owner = get_user($vars['entity']->owner_guid);

    if(isset($vars['entity']) && isloggedin() && $owner->isFriendsWith(get_loggedin_userid())){

    // form code

    }

  • Thanks Matt Beckett! It works as expected! =)

     

    It should be part of user's privacy control ;)

  • One las thing, sorry hehe

    In order to show the comments box to the owner too should I add

    || $owner or && owner

    ???