api-forms.html 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. <h2>Implementation</h2>
  4. A views handler outputs a special placeholder in render(), while the real form with matching structure gets added in views_form().
  5. When the View is being preprocessed for the theme file, all placeholders get replaced with the rendered form elements.
  6. The views handler can also implement views_form_validate() and views_form_submit().
  7. <pre>
  8. function render($values) {
  9. return '&lt;!--form-item-' . $this-&gt;options['id'] . '--' . $this-&gt;view-&gt;row_index . '--&gt;';
  10. }
  11. function form_element_name() {
  12. // Make sure this value is unique for all the view fields
  13. return $this-&gt;options['id'];
  14. }
  15. function form_element_row_id($row_id) {
  16. // You could use values from $this-&gt;view-&gt;result[$row_id]
  17. // to provide complex row ids.
  18. return $row_id;
  19. }
  20. function views_form(&$form, &$form_state) {
  21. // The view is empty, abort.
  22. if (empty($this-&gt;view-&gt;result)) {
  23. return;
  24. }
  25. $field_name = $this-&gt;form_element_name();
  26. $form[$field_name] = array(
  27. '#tree' => TRUE,
  28. );
  29. // At this point, the query has already been run, so we can access the results
  30. foreach ($this-&gt;view-&gt;result as $row_id => $row) {
  31. $form_element_row_id = $this-&gt;form_element_row_id($row_id);
  32. $form[$field_name][$form_element_row_id] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Your name'),
  35. '#default_value' => '',
  36. );
  37. }
  38. }
  39. // Optional validate function.
  40. function views_form_validate($form, &$form_state) {
  41. $field_name = $this->form_element_name();
  42. foreach ($form_state['values'][$field_name] as $row_id => $value) {
  43. if ($value == 'Drupal') {
  44. form_set_error($field_name . '][' . $row_id, "You can't be named Drupal. That's my name.");
  45. }
  46. }
  47. }
  48. // Optional submit function.
  49. function views_form_submit($form, &$form_state) {
  50. // Do something here
  51. }
  52. </pre>
  53. The form is multistep by default, with one step: 'views_form_views_form'.
  54. A "form_example" module could add a confirmation step by setting:
  55. <pre>
  56. $form_state['step'] = 'form_example_confirmation';
  57. </pre>
  58. in form_example_views_form_submit().
  59. Then, views_form would call form_example_confirmation($form, $form_state, $view, $output) to get that step.
  60. <b>Important:</b> You can fetch the Views object in form_alter and validate / submit hooks from the form state:
  61. <pre>
  62. $view = $form_state['build_info']['args'][0];
  63. </pre>
  64. <h2>Relevant Views functions</h2>
  65. <ul>
  66. <li>template_preprocess_views_view()</li>
  67. <li>views_form()</li>
  68. <li>views_form_views_form()</li>
  69. <li>views_form_views_form_validate()</li>
  70. <li>views_form_views_form_submit()</li>
  71. <li>theme_views_form_views_form()</li>
  72. </ul>
  73. <h2>Hooks</h2>
  74. <ul>
  75. <li>hook_views_form_substitutions()</li>
  76. </ul>