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() {
parent::construct();
$this->additional_fields['nid'] = 'nid';
//$this->additional_fields['field_file_download_link'] = 'field_file_download_link';
}
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
}
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));
dpm($nodes);
}
}
?>
Thanks!