Test module implementing a form that can be embedded in search results.
A sample use of an embedded form is an e-commerce site where each search result may include an embedded form with buttons like "Add to cart" for each individual product (node) listed in the search results.
<?php
/**
* @file
* Test module implementing a form that can be embedded in search results.
*
* A sample use of an embedded form is an e-commerce site where each search
* result may include 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;
}
/**
* Form constructor for embedding search results for testing.
*
* @see search_embedded_form_form_submit().
*/
function search_embedded_form_form($form, &$form_state) {
$count = config('search_embedded_form.settings')
->get('submitted');
$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) {
$config = config('search_embedded_form.settings');
$submit_count = (int) $config
->get('submitted');
$config
->set('submitted', $submit_count + 1)
->save();
drupal_set_message(t('Test form was submitted'));
}
/**
* Adds the test form to search results.
*/
function search_embedded_form_preprocess_search_result(&$variables) {
$form = drupal_get_form('search_embedded_form_form');
$variables['snippet'] .= drupal_render($form);
}
Name | Description |
---|---|
search_embedded_form_form | Form constructor for embedding search results for testing. |
search_embedded_form_form_submit | Submit handler for search_embedded_form_form(). |
search_embedded_form_menu | Implements hook_menu(). |
search_embedded_form_preprocess_search_result | Adds the test form to search results. |