Project

General

Profile

Paste
Download (1.9 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / search / tests / search_embedded_form.module @ c7768a53

1
<?php
2

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

    
12
/**
13
  * Implements hook_menu().
14
  */
15
function search_embedded_form_menu() {
16
  $items['search_embedded_form'] = array(
17
    'title' => 'Search_Embed_Form',
18
    'page callback' => 'drupal_get_form',
19
    'page arguments' => array('search_embedded_form_form'),
20
    'access arguments' => array('search content'),
21
    'type' => MENU_CALLBACK,
22
  );
23

    
24
  return $items;
25
}
26

    
27
/**
28
 * Builds a form for embedding in search results for testing.
29
 *
30
 * @see search_embedded_form_form_submit().
31
 */
32
function search_embedded_form_form($form, &$form_state) {
33
  $count = variable_get('search_embedded_form_submitted', 0);
34

    
35
  $form['name'] = array(
36
    '#type' => 'textfield',
37
    '#title' => t('Your name'),
38
    '#maxlength' => 255,
39
    '#default_value' => '',
40
    '#required' => TRUE,
41
    '#description' => t('Times form has been submitted: %count', array('%count' => $count)),
42
  );
43

    
44
  $form['actions'] = array('#type' => 'actions');
45
  $form['actions']['submit'] = array(
46
    '#type' => 'submit',
47
    '#value' => t('Send away'),
48
  );
49

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

    
52
  return $form;
53
}
54

    
55
/**
56
 * Submit handler for search_embedded_form_form().
57
 */
58
function search_embedded_form_form_submit($form, &$form_state) {
59
  $count = variable_get('search_embedded_form_submitted', 0) + 1;
60
  variable_set('search_embedded_form_submitted', $count);
61
  drupal_set_message(t('Test form was submitted'));
62
}
63

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