1

I am using the User Relationships module and I created a table in Views with all of the site users.

There is a field where you can add or remove a friend. But (especially the first time) the users must be able to add multiple friends add once. For example with check boxes and a "send friend request" on the bottom of the page.

I have tried to accomplish that with Views Bulk Operations but there is not a integration with that module. What can I do?

2

Write your own integration!

Really, Views Bulk Operations is quite an easy module to write a plugin for and optionally you can submit it to the User Relationship project to have it maybe embedded in the main project. There is a complete tutorial here, but I will briefly explain each step of the process.

First off, you need to create a custom module. Create a folder with a fitting name for your module, like user_relationships_vbo or something. Add a user_relationships_vbo.info (writing .info files) and a .module file. The module file should contain several specific functions to have it work with VBO.

The action

Implementation of hook_action_info(), this defines the action. On what entities it can act, what the action is called, how it can be configured and if it has an extra VBO configuration option.

The VBO configuration form

This is an optional form that adds settings the the Views VBO config form, allowing you to define additional settings. In your case this is not really important as you probably just want to relate users.

The bulk configuration form

This forms shows after your chose your action in VBO, and can be used to define settings for the current batch. You could for instance use this the choose to have the requester specify the relationship type. Again in the simplest form you would not need this.

The actual action

This is what will eventually relate the users. You can make this as long and as complicated as you like with all kind of notifications, etc. But the basics is to just add the relation.

So the code

Your .module file might look like this:

<?php
// The action definition
function user_relationships_vbo_action_info() {
  return array(
    'user_relationships_vbo_relate_users' => array(
      'type' => 'user',
      'label' => t('Relate users to the current user'),
      'configurable' => FALSE,
      'vbo_configurable' => TRUE,
      'triggers' => array(),
    ),
  );
}

//The per batch form
function user_relationships_vbo_relate_users_form($settings, &$form_state) {
  $form = array();
  $form['type'] = array(
    '#type' => 'select', 
    '#title' => t('Choose your relation type'),
    // This assumes all users have access to all relationships. You might want to add a filter to check access somehow.
    '#options' => user_relationships_types_load(),
    '#required' => TRUE,
    '#default_value' => isset($settings['settings']['type']) ? $settings['settings']['type'] : '',
  );
  return $form;
}

// The form submission
function user_relationships_vbo_relate_users_submit($form, $form_state) {
  $return = array(); 
  $return['type'] = $form_state['values']['type'];
  return $return; //Note, return value here must be an array.
}

// The actual action
function user_relationships_vbo_relate_users(&$target_user, $context) {
  // Get the current user
  global $user;

  // See user_relationships_request_relationship() 
  // http://api.drupalhelp.net/api/user_relationships/user_relationships.module/function/user_relationships_request_relationship/7

  user_relationships_request_relationship($user->uid, $target_user->uid, $context['type'], FALSE)

}
?>

Read the rest of the tutorial, but this should get you started.

  • Thanks for you're reply! I am new to programming but this looks not impossible to me. I am definitely going to try this. I will post my result. – RvO Mar 12 '16 at 12:53
  • If you have only one relation type, you can remove both the form functions and just replace $context['type'] with your relation type id (you can find it in the url when you edit the relation) in user_relationships_request_relationship(). That would make it even less complex. Good luck! – Neograph734 Mar 12 '16 at 13:18

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.