summaryrefslogtreecommitdiff
path: root/modules/search/tests/search_embedded_form.module
blob: d80fa170aa97c93d2f4b6d021423c771fe69884d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
// $Id$

/**
 * @file
 * Test module implementing a form that can be embedded in search results.
 *
 * Embedded form are important, for example, for ecommerce sites where each
 * search result may included an embedded form with buttons like "Add to cart"
 * for each individual product (node) listed in the search results.
 */

/**
  * Implements hook_menu().
  */
function search_embedded_form_menu() {
  $items['search_embedded_form'] = array(
    'title' => 'Search_Embed_Form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('search_embedded_form_form'),
    'access arguments' => array('search content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Builds a form for embedding in search results for testing.
 *
 * @see search_embedded_form_form_submit().
 */
function search_embedded_form_form($form, &$form_state) {
  $count = variable_get('search_embedded_form_submitted', 0);

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#maxlength' => 255,
    '#default_value' => '',
    '#required' => TRUE,
    '#description' => t('Times form has been submitted: %count', array('%count' => $count)),
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send away'),
  );

  $form['#submit'][] = 'search_embedded_form_form_submit';

  return $form;
}

/**
 * Submit handler for search_embedded_form_form().
 */
function search_embedded_form_form_submit($form, &$form_state) {
  $count = variable_get('search_embedded_form_submitted', 0) + 1;
  variable_set('search_embedded_form_submitted', $count);
  drupal_set_message(t('Test form was submitted'));
}

/**
 * Adds the test form to search results.
 */
function search_embedded_form_preprocess_search_result(&$variables) {
  $variables['snippet'] .= drupal_render(drupal_get_form('search_embedded_form_form'));
}