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, 2013 at 16:56

1 Answer 1


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'] = 
      && ($account->field_optout[LANGUAGE_NONE][0]['value'] == 1))
      ? 0 : 1;

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:

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.


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'] = 
      && ($profile->field_optout[LANGUAGE_NONE][0]['value'] == 1))
      ? 0 : 1;

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, 2013 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, 2013 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, 2013 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, 2013 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, 2013 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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