search_embedded_form.module 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * Test module implementing a form that can be embedded in search results.
  5. *
  6. * Embedded form are important, for example, for ecommerce sites where each
  7. * search result may included an embedded form with buttons like "Add to cart"
  8. * for each individual product (node) listed in the search results.
  9. */
  10. /**
  11. * Implements hook_menu().
  12. */
  13. function search_embedded_form_menu() {
  14. $items['search_embedded_form'] = array(
  15. 'title' => 'Search_Embed_Form',
  16. 'page callback' => 'drupal_get_form',
  17. 'page arguments' => array('search_embedded_form_form'),
  18. 'access arguments' => array('search content'),
  19. 'type' => MENU_CALLBACK,
  20. );
  21. return $items;
  22. }
  23. /**
  24. * Builds a form for embedding in search results for testing.
  25. *
  26. * @see search_embedded_form_form_submit().
  27. */
  28. function search_embedded_form_form($form, &$form_state) {
  29. $count = variable_get('search_embedded_form_submitted', 0);
  30. $form['name'] = array(
  31. '#type' => 'textfield',
  32. '#title' => t('Your name'),
  33. '#maxlength' => 255,
  34. '#default_value' => '',
  35. '#required' => TRUE,
  36. '#description' => t('Times form has been submitted: %count', array('%count' => $count)),
  37. );
  38. $form['actions'] = array('#type' => 'actions');
  39. $form['actions']['submit'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Send away'),
  42. );
  43. $form['#submit'][] = 'search_embedded_form_form_submit';
  44. return $form;
  45. }
  46. /**
  47. * Submit handler for search_embedded_form_form().
  48. */
  49. function search_embedded_form_form_submit($form, &$form_state) {
  50. $count = variable_get('search_embedded_form_submitted', 0) + 1;
  51. variable_set('search_embedded_form_submitted', $count);
  52. drupal_set_message(t('Test form was submitted'));
  53. }
  54. /**
  55. * Adds the test form to search results.
  56. */
  57. function search_embedded_form_preprocess_search_result(&$variables) {
  58. $form = drupal_get_form('search_embedded_form_form');
  59. $variables['snippet'] .= drupal_render($form);
  60. }