Mercurial > hg > rr-repo
comparison sites/all/modules/views/help/api-forms.html @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
1 Views allows handlers to output form elements, wrapping them automatically in a form, and handling validation / submission. | |
2 The form is multistep by default, allowing other modules to add additional steps, such as confirmation screens. | |
3 | |
4 <h2>Implementation</h2> | |
5 A views handler outputs a special placeholder in render(), while the real form with matching structure gets added in views_form(). | |
6 When the View is being preprocessed for the theme file, all placeholders get replaced with the rendered form elements. | |
7 | |
8 The views handler can also implement views_form_validate() and views_form_submit(). | |
9 <pre> | |
10 function render($values) { | |
11 return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->'; | |
12 } | |
13 | |
14 function form_element_name() { | |
15 // Make sure this value is unique for all the view fields | |
16 return $this->options['id']; | |
17 } | |
18 | |
19 function form_element_row_id($row_id) { | |
20 // You could use values from $this->view->result[$row_id] | |
21 // to provide complex row ids. | |
22 return $row_id; | |
23 } | |
24 | |
25 function views_form(&$form, &$form_state) { | |
26 // The view is empty, abort. | |
27 if (empty($this->view->result)) { | |
28 return; | |
29 } | |
30 | |
31 $field_name = $this->form_element_name(); | |
32 $form[$field_name] = array( | |
33 '#tree' => TRUE, | |
34 ); | |
35 // At this point, the query has already been run, so we can access the results | |
36 foreach ($this->view->result as $row_id => $row) { | |
37 $form_element_row_id = $this->form_element_row_id($row_id); | |
38 $form[$field_name][$form_element_row_id] = array( | |
39 '#type' => 'textfield', | |
40 '#title' => t('Your name'), | |
41 '#default_value' => '', | |
42 ); | |
43 } | |
44 } | |
45 | |
46 // Optional validate function. | |
47 function views_form_validate($form, &$form_state) { | |
48 $field_name = $this->form_element_name(); | |
49 foreach ($form_state['values'][$field_name] as $row_id => $value) { | |
50 if ($value == 'Drupal') { | |
51 form_set_error($field_name . '][' . $row_id, "You can't be named Drupal. That's my name."); | |
52 } | |
53 } | |
54 } | |
55 | |
56 // Optional submit function. | |
57 function views_form_submit($form, &$form_state) { | |
58 // Do something here | |
59 } | |
60 </pre> | |
61 | |
62 The form is multistep by default, with one step: 'views_form_views_form'. | |
63 A "form_example" module could add a confirmation step by setting: | |
64 <pre> | |
65 $form_state['step'] = 'form_example_confirmation'; | |
66 </pre> | |
67 in form_example_views_form_submit(). | |
68 Then, views_form would call form_example_confirmation($form, $form_state, $view, $output) to get that step. | |
69 | |
70 <b>Important:</b> You can fetch the Views object in form_alter and validate / submit hooks from the form state: | |
71 <pre> | |
72 $view = $form_state['build_info']['args'][0]; | |
73 </pre> | |
74 | |
75 <h2>Relevant Views functions</h2> | |
76 <ul> | |
77 <li>template_preprocess_views_view()</li> | |
78 <li>views_form()</li> | |
79 <li>views_form_views_form()</li> | |
80 <li>views_form_views_form_validate()</li> | |
81 <li>views_form_views_form_submit()</li> | |
82 <li>theme_views_form_views_form()</li> | |
83 </ul> | |
84 | |
85 <h2>Hooks</h2> | |
86 <ul> | |
87 <li>hook_views_form_substitutions()</li> | |
88 </ul> |