Is it possible to display a Boolean field in a block using views? Using views,


I created a block that displays users that are online.
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.
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, &amp;$form_state) {
  $form['submit'] = array (
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Change my on-line visibility'),
  )
}


Then you need a submit handler for the form:

function modulename_onlinevisibility_form_submit(&amp;$form_state) {
  global $user;
  $account = user_load($user-&gt;uid);
  $account-&gt;field_optout[LANGUAGE_NONE][0]['value'] = 
    (isset($account-&gt;field_optout[LANGUAGE_NONE][0]['value']) 
      &amp;&amp; ($account-&gt;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:

&lt;?php
print render(drupal_get_form('modulename_onlinevisibility_form'));
?&gt;


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


Profile2 profiles are not loaded by user_load.