2

I have created a custom block (that resides with path visibility of users/*).

I have written some PHP code to allow users to message each other (D7 Privatemsg module):

<?php
// Get user object.
if ($account = menu_get_object('user')) {
  if ($url = privatemsg_get_link(array($account))) {
    return l(t('write a mail to this user'), $url, array(
      'query' => drupal_get_destination(),
    ));
  }
}
?>

In that same block, I would like to add another snippet to show a Flag Friend link. This would give me one block with two links: one for mail, one for adding/removing friendship.

Does anyone know the PHP code for flagging a friend?

1

I think the method below should do the trick. Don't think it is available in the flag_friend module's API.

/**
 * Gets the friend/unfriend link for a given user.
 *
 * Depending on the flagging user's current friend status with the target
 * user, the label of this link will show up as 'Add friend' or 'Remove friend'.
 *
 * @param stdClass $account
 *   Target user object. Defaults to the currenly logged in user.
 *
 * @return mixed
 *   FALSE or 
 *   Formatted flag/unflag link otherwise.
 *
 * @see flag_friend_flag_default_flags().
 * @see flag_create_link().
 */
function flag_friend_get_friend_flag_link($account = NULL) {
  if (!isset($account)) {
    global $user;
    $account = $user;
  }
  return flag_create_link('friend', $account->uid);
}

Do note that the flagging user is assumed by default to be the currently logged in user which fits your use-case anyway.

Access check happens implicitly. So, if the current user does not have access to friend/unfriend a given target user, then this method will return FALSE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.