Test module implementing a form that can be embedded in search results.
Embedded form are important, for example, for e-commerce 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.
File
core/modules/search/tests/search_embedded_form/search_embedded_form.module
View source <?php
* @file
* Test module implementing a form that can be embedded in search results.
*
* Embedded form are important, for example, for e-commerce 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' => 'backdrop_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 = config_get ('search.settings' , 'search_embedded_form_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.settings' );
$submit_count = (int) $config ->get ('search_embedded_form_submitted' );
$config ->set ('search_embedded_form_submitted' , $submit_count + 1 )->save ();
backdrop_set_message (t ('Test form was submitted' ));
}
* Adds the test form to search results.
*/
function search_embedded_form_preprocess_search_results (&$variables ) {
foreach ($variables ['search_results ' ] as $key => $result ) {
$form = backdrop_get_form ('search_embedded_form_form ' );
$snippet = '<p>' . $variables ['search_results ' ][$key ]['snippet' ] . '</p>' ;
$snippet .= backdrop_render ($form );
$variables ['search_results ' ][$key ]['snippet' ] = $snippet ;
}
}
Functions