I have build a form using the Form API that adds a checkbox to every row in my custom view (the view page can be found here: http://goo.gl/Pg6Ja7). I want this form to store the values even after I change the exposed filter of the view or change the view page.

I was thinking to create a new submit button that will store the values on the current page, but I don't know how to do that, I googled the whole day. Or maybe some JavaScript ?

I added the View form with a handler, here is the simple code I wrote for it:

   * @file
   * A Views' field handler for adding a checkbox.
class files_download_links_mail_field_handler_checkbox  extends views_handler_field {
  function construct() {
    $this->additional_fields['nid'] = 'nid';
    //$this->additional_fields['field_file_download_link'] = 'field_file_download_link';


  function query() {

  function render($values) {
    // Render a Views form item placeholder.
    // This causes Views to wrap the View in a form.
    // Render a Views form item placeholder.
    return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';

  function views_form(&$form, &$form_state) {
    // Create a container for our replacements
    $form[$this->options['id']] = array(
      '#type' => 'container',
      '#tree' => TRUE,
    // Iterate over the result and add our new field to the form.
    foreach($this->view->result as $row_index => $row) {
      // Add a checkbox field to the form.
      $form[$this->options['id']][$row_index] = array(
        '#type' => 'checkbox',

   * Form submit method.
  function views_form_submit($form, &$form_state) {
    // Determine which nodes were selected.
    $selected_items = array();
    // Iterate over the view result.
    foreach($this->view->result as $row_index => $row) {
      if ($form_state['values'][$this->options['id']][$row_index] == 1) {
        $selected_items[$row->{$this->aliases['nid']}] = $form_state['values'][$this->options['id']][$row_index];
    // Grab the nodes
    $nodes = node_load_multiple(array_keys($selected_items));



This seems like the perfect scenario for a custom module in my opinion. I know it's possible to get this working based on some light research I did (specifically this web page).

Your solution is using a custom module anyway, best to create the form and hooks and sql in the custom module and remove your dependency on views.

Create your hook_menu:

function MODULE_menu()
    $items['PATH'] = array(
        'title'            => t('TITLE'),
        'description'      => t('DESCRIPTION'),
        'page callback'    => 'drupal_get_form',
        'page arguments'   => array('MODULE_form_function'),
        'access arguments' => array('access content'),
        'type'             => MENU_NORMAL_ITEM,

    return $items;

Create your form function (using the Form API and Database Abstraction Layer):

function MODULE_form_function($form, &$form_state)
    // handle creation of your form, including sql to replace views.

    return $form;

Create your validate and submit handlers:

function MODULE_form_function_validate(&$form_state)
    // handle the validation of your form

function MODULE_form_function_submit(&$form_state)
    // handle the submission of your form

This is a much simpler and proven way to accomplish what you are trying to do.

| improve this answer | |
  • Hi @Scott, I'm not trying to create a simple form, even if I wanted, your answer doesn't cover at all what I asked, it just provides me with info on how to build a simple form, I know that. I want to use views in order to sort, filter and choose different layouts for it and the values selected not to be wiped when the page is changed or the filter value is changed. – Canutza Nov 13 '13 at 7:46
  • You can do all of that with a custom module. I didn't want to write it for you, but gave you the basics to get started. – Scott Joudry Nov 13 '13 at 12:10
  • @Scoott, I have a custom module build, the code, but just for the view field handler is above. My question is how do I store the values of my custom form written in my custom module if I change the page of the view or change the filter. Please read the question. Thanks for your replies. – Canutza Nov 13 '13 at 15:09
  • add it to the session using $_SESSION would be the easiest way – Scott Joudry Nov 13 '13 at 15:10
  • good idea, but I know that Drupal changes the session for anonymous users every time you refresh the page. I have to explore this, but good starting point, thanks! – Canutza Nov 13 '13 at 15:11

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.