1

Using views,

  1. I created a block that displays users that are online.

  2. In order to allow users choose to appear online or offline, I added a Boolean field to the user account, which I subsequently used as a filter for the online users. All this works fine.

Now I need to find a way to have a form - (with the Boolean field as its only field) appear as an 'attachment' to the original block? In this way users can choose to appear offline without having to go to their user account page.

Please, is this possible using views,thanks.

  • Just to be clear, it sounds like you want the field to be in a form so that the currently logged in user can change his preference using the form in the block. If this is correct, please edit your question to indicate it. – Triskelion Jun 4 '13 at 16:56
0

Would a button do?

To do this you have to create a custom module, or add to an existing custom module.

First define your form. All you need is a submit button, and the value can be any label you want on the button.

function modulename_onlinevisibility_form( $form, &$form_state) {
  $form['submit'] = array (
    '#type' => 'submit',
    '#value' => t('Change my on-line visibility'),
  )
}

Then you need a submit handler for the form:

function modulename_onlinevisibility_form_submit(&$form_state) {
  global $user;
  $account = user_load($user->uid);
  $account->field_optout[LANGUAGE_NONE][0]['value'] = 
    (isset($account->field_optout[LANGUAGE_NONE][0]['value']) 
      && ($account->field_optout[LANGUAGE_NONE][0]['value'] == 1))
      ? 0 : 1;
  user_save($account);
}

This will switch the value of the boolean field turning it off if on and visa versa, for the logged in user. (Profile fields may not be included in the $user variable, so a full user object is loaded using user_load.)

Then in your view, add a footer using the Global: PHP type, and add the code:

<?php
print render(drupal_get_form('modulename_onlinevisibility_form'));
?>

This will place a button at the bottom of your view which will toggle the user's on-line visibility.

EDIT.

If you are using the Profile2 module, then the code for the submit handler should be changed to:

function modulename_onlinevisibility_form_submit(&$form_state) {
  global $user;
  $profile_type = 'main'; // substitute the machine name of the profile if you are not using the default
  $profile = profile2_load_by_user( $user, $profile_type);

  $profile->field_optout[LANGUAGE_NONE][0]['value'] = 
    (isset($profile->field_optout[LANGUAGE_NONE][0]['value']) 
      && ($profile->field_optout[LANGUAGE_NONE][0]['value'] == 1))
      ? 0 : 1;
  profile2_save($profile);
}

Profile2 profiles are not loaded by user_load.

  • First of all, thanks! I am not yet proficient at custom module development, but will take time to go through and then implement it. Please will the above work if the Boolean field is created through the profile 2 module rather than in the core user account? – seyi Jun 5 '13 at 6:39
  • Its really not that difficult. Follow the instructions here. Once you have created the mysite module, then add the code for the two functions above in your mysite.module file, and you are done. – Triskelion Jun 5 '13 at 14:32
  • I get this error after i add the php code- Strict warning: Only variables should be passed by reference in eval(). Can you please help out? thanks. – seyi Jun 13 '13 at 20:36
  • The only code using eval() is the print render() statement in the footer of the view. The other code should be placed in a module. What code, and where are you putting it to generate the error? – Triskelion Jun 13 '13 at 21:36
  • This is the code, as above: <?php print render(drupal_get_form('testmodule_form')); ?> (My module name is "testmodule"). The view is a block view. Under the block settings, i added it to the 'FOOTER'. I choose 'Global: Text area' then selected 'PHP code' under text format. Thanks! – seyi Jun 14 '13 at 7:45

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.