Drupal 6 Form "Radio" #return_value Not Working - drupal-6

I am working with Drupal 6 and creating a module that involves a form submission. I opted to use the #type "radio" as it allows me to dynamically create a table (which is necessary for display purposes).
I am outputting the returned values of each radio I have selected in the image. However, whenever I submit the form, the #default_value is returned rather than the #return_value.
Here is my code as well as a screenshot of the page. Any help is greatly appreciated!
======Image of the Problem Here=======
function peereval_survey(&$formstate, $numStudents)
{
drupal_add_css(drupal_get_path('module', 'peereval') .'/peereval.css');
for ($rowNum = 1; $rowNum <= $numStudents; $rowNum++)
{
for ($colNum = 1; $colNum <= 5; $colNum++)
{
if ($rowNum == 1)
{
$form['v' . $colNum]['u' . $rowNum . ',v' . $colNum] = array
(
'#prefix' => "<tr><td>",
'#suffix' => "</td>",
'#type' => 'radio',
'#title' => t($rowNum . ', ' . $colNum),
'#name' => 'u' . $rowNum,
'#default_value' => 0,
'#return_value' => $colNum,
);
}
else if ($rowNum == 5)
{
$form['v' . $colNum]['u' . $rowNum . ',v' . $colNum] = array
(
'#prefix' => "<td>",
'#suffix' => "</td></tr>",
'#type' => 'radio',
'#title' => t($rowNum . ', ' . $colNum),
'#name' => 'u' . $rowNum,
'#default_value' => 0,
'#return_value' => $colNum,
);
}
else
{
$form['v' . $colNum]['u' . $rowNum . ',v' . $colNum] = array
(
'#prefix' => "<td>",
'#suffix' => "</td>",
'#type' => 'radio',
'#title' => t($rowNum . ', ' . $colNum),
'#name' => 'u' . $rowNum,
'#default_value' => 0,
'#return_value' => $colNum,
);
}
}
}
$form['numStudents'] = array
(
'#type' => 'hidden',
'#value' => $numStudents
);
$form['submit'] = array
(
'#prefix' => '<div id="submit">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
 
function peereval_survey_submit($form, &$form_state)
{
$form_state['redirect'] = 'peereval/1/5/1';
drupal_set_message("hi" . $form_state['values']['u1,v1'] . $form_state['values']['u2,v2'] . $form_state['values']['u3,v3'] . $form_state['values']['u4,v4'] . $form_state['values']['u5,v5']);
return $form;
}

The following variables contained my data:
$form_state['clicked_button']['#post']['u1'],
$form_state['clicked_button']['#post']['u2'],
$form_state['clicked_button']['#post']['u3'],
$form_state['clicked_button']['#post']['u4'],
$form_state['clicked_button']['#post']['u5'].
Big thanks to Adam Balsam for helping me find this!

Related

Yii2 Pagination Issue on Union

I am trying to use Pagination after union of two queries the pagination does not seems to work. However if I try to make one queries without union it works.
The below are the queries.Please help.
//First Query
$first_second = $this->find()->select($strcolumn.', p.featured_name')->from(MYDIRECTORY::tableName().' j')->join('INNER JOIN' ,MYDIRECTORYFEATUREDCLASS::tableName().' p', 'p.featurer_id = j.listdetails_featured_frid')->where(['listdetails_list_frid' => $id['list_id']])->andWhere(['<=', 'listdetails_featured_frid', 2])->andWhere(['listdetails_list_flag' => MYDIRECTORYCLASS::STATUS_ACTIVE])->orderBy(['listdetails_featured_frid'=>SORT_ASC,'listdetails_list_pos'=>new Expression('rand()')]);
//Second Query
$second_list = $this->find()->select($strcolumn.', p.featured_name')->from(MYDIRECTORYCLASS::tableName().' j')->join('INNER JOIN' ,MYDIRECTORYFEATUREDCLASS::tableName().' p', 'p.featurer_id = j.listdetails_featured_frid')->where(['listdetails_list_frid' => $id['list_id']])->andWhere(['>', 'listdetails_featured_frid', 2])->andWhere(['listdetails_list_flag' => MYDIRECTORYCLASS::STATUS_ACTIVE])->orderBy(['listdetails_featured_frid'=>SORT_ASC,'listdetails_list_medname'=>SORT_ASC]);
//Joined Union Query
$joinedquerys=$first_second->union($second_list);
$countQuery = clone $joinedquerys;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => \Yii::$app->params['pagination_limit'],'defaultPageSize' => \Yii::$app->params['pagination_limit'],'forcePageParam' => false,'params' => ['page' => \Yii::$app->request->get('page', 1)] ]);
$resultArray = $joinedquerys->offset($pages->offset)->limit($pages->limit)->asArray()->all();
return $this->render('listing', [
"mylisting" => $resultArray,
"pagination" => $pages
]);
While using the pagination as below , the pagination seems to not work?Any help will be greatly appreciated
echo LinkPager::widget([
'pagination' => $pagination,
'options' => ['class' => 'paginate pag2 clearfix'],
'registerLinkTags' => true,
'prevPageLabel' => \YII::$app->params['linker_page_btn_prev'],
'nextPageLabel' => \YII::$app->params['linker_page_btn_next'],
'maxButtonCount' => \YII::$app->params['linker_page_btn_count'],
'activePageCssClass' => 'current',
'nextPageCssClass' => 'next'
]);
you could use dataProvider
<?php
$joinedquerys=$first_second->union($second_list);
$dataProvider = new SqlDataProvider([
'sql' => $joinedquerys,
]);
?>
I had the same problem, but in addition I had to use the ActiveDataProvider. To achieve this I had to do the following:
$joinedQuery = $first_second->union($second_list);
$dirQuery = MYDIRECTORY::find()->from(['directories' => $joinedQuery]);
$provider = new ActiveDataProvider([
'query' => $dirQuery,
]);
Hope this helps someone :)

Wordpress Pagination with Custom Query

I am attempting to write a plugin that searches an SQL table based on input supplied via a form. My code is...
global $table_name_log;
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 25;
$offset = ( $pagenum - 1 ) * $limit;
$l_task=isset( $_POST['l_task'] ) ? $_POST['l_task'] : "a";
$l_val=$_POST['l_val']; if($l_val=="") { $l_task="a"; }
if($l_task=="d")
{
$criteria="where log.Doc_Number='{$l_val}'";
}
elseif($l_task=="e")
{
$criteria="where log.Employee='{$l_val}'";
}
else
{
$criteria="";
}
$SQLQuery="SELECT em.ID, log.Date, em.Payroll, log.Doc_Number, doc.Doc_Name, doc.Replaced_By, log.Trainer, log.Log, log.Type
FROM {$table_name_log} log
join {$table_name_employee} em on log.Employee=em.ID
join {$table_name_documents} doc on log.Doc_Number=doc.Doc_Number
{$criteria}
order by log.Employee asc
";
//Get total number of results
$results=$wpdb->get_results("{$SQLQuery}",ARRAY_A);
$search_total = $wpdb->num_rows;
//Limit results by page
$results=$wpdb->get_results("{$SQLQuery} LIMIT {$offset}, {$limit}",ARRAY_A);
if(! $results)
{
if($pagenum>1)
{
$pagenum=1;
$results=$wpdb->get_results("{$SQLQuery} LIMIT 0, {$limit}",ARRAY_A);
}
}
I then display the results of the query.
At the bottom of the displayed results I have this code...
$total=$search_total;
$num_of_pages = ceil( $total / $limit );
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'aag' ),
'next_text' => __( '»', 'aag' ),
'total' => $num_of_pages,
'current' => $pagenum
) );
if ( $page_links ) {
echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
The problem is that although the number of pages is calculated correctly and page 1 displays the correct results; when selecting another page it displays the results without the $criteria set by $l_task. How do I edit the code so that the pagination links feed the correct query to use for the additional pages?
Works now by adding this at the top...
if(isset($_GET['l_task'])) { $_POST['l_task']=$_GET['l_task']; }
if(isset($_GET['l_val'])) { $_POST['l_val']=$_GET['l_val']; }
I then added this...
'add_args' => array( 'l_task' => $_POST['l_task'], 'l_val' => $_POST['l_val'] )
to...
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'aag' ),
'next_text' => __( '»', 'aag' ),
'total' => $num_of_pages,
'current' => $pagenum,
'add_args' => array( 'l_task' => $_POST['l_task'], 'l_val' => $_POST['l_val'] )
) );

Extending Drupal 7 search

I want to extend default Drupal 7 node search with one additional field.
I alter search form with the following new field:
function mymodule_form_search_form_alter(&$form, &$form_state, $form_id) {
$form['basic']['site'] = array(
'#type' => 'select',
'#options' => array(
'KEY1' => 'TITLE1',
'KEY2' => 'TITLE2',
'KEY3' => 'TITLE3'
)
);
}
I have a field called field_data_field_site.field_site_value which i need to use as a filter in this search.
I've tried to read about hook_search_* functions but didn't get the idea.
My question is the following. How can I extend search form? Anyone have live examples?
The following is the best way I solve this problem.
First of all I need to alter Drupal's search block and search form with my field and define new submit function.
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_search_block_form_alter(&$form, &$form_state, $form_id) {
$form['#submit'][] = 'search_form_alter_submit';
$form['site'] = array(
'#type' => 'select',
'#options' => _options(),
'#default_value' => (($_GET['site']) ? $_GET['site'] : '')
);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_search_form_alter(&$form, &$form_state, $form_id) {
$form['#submit'][] = 'search_form_alter_submit';
$form['basic']['site'] = array(
'#type' => 'select',
'#options' => _options(),
'#default_value' => (($_GET['site']) ? $_GET['site'] : '')
);
}
function _options() {
return array(
'' => 'Select site',
'site-1' => 'Site 1',
'site-2' => 'Site 2'
);
}
Submit function will forward us to default search/node page but with our query. Page would look like search/node/Our-query-string?site=Our-option-selected.
function search_form_alter_submit($form, &$form_state) {
$path = $form_state['redirect'];
$options = array(
'query' => array(
'site' => $form_state['values']['site']
)
);
drupal_goto($path, $options);
}
Next step is to use hook_search_info (Don't forget to turn it on and set as default on admin/config/search/settings page).
/**
* Implements hook_search_info().
*/
function mymodule_search_info() {
return array(
'title' => 'Content',
'path' => 'node',
'conditions_callback' => '_conditions_callback',
);
}
Conditions callback function defined in hook_search_info. We need to provide additional queries to our search.
function _conditions_callback($keys) {
$conditions = array();
if (!empty($_REQUEST['site'])) {
$conditions['site'] = $_REQUEST['site'];
}
return $conditions;
}
Finally, hook_search_execute will filter our content by our query. I used default code from this hook with modifications I need.
/**
* Implements hook_search_execute().
*/
function mymodule_search_execute($keys = NULL, $conditions = NULL) {
// Build matching conditions
$query = db_select('search_index', 'i', array('target' => 'slave'))
->extend('SearchQuery')
->extend('PagerDefault');
$query->join('node', 'n', 'n.nid = i.sid');
// Here goes my filter where I joined another table and
// filter by required field
$site = (isset($conditions['site'])) ? $conditions['site'] : NULL;
if ($site) {
$query->leftJoin('field_data_field_site', 's', 's.entity_id = i.sid');
$query->condition('s.field_site_value', $site);
}
// End of my filter
$query
->condition('n.status', 1)
->addTag('node_access')
->searchExpression($keys, 'node');
// Insert special keywords.
$query->setOption('type', 'n.type');
$query->setOption('language', 'n.language');
if ($query->setOption('term', 'ti.tid')) {
$query->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
}
// Only continue if the first pass query matches.
if (!$query->executeFirstPass()) {
return array();
}
// Add the ranking expressions.
_node_rankings($query);
// Load results.
$find = $query
->limit(10)
->execute();
$results = array();
foreach ($find as $item) {
// Build the node body.
$node = node_load($item->sid);
node_build_content($node, 'search_result');
$node->body = drupal_render($node->content);
// Fetch comments for snippet.
$node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);
// Fetch terms for snippet.
$node->rendered .= ' ' . module_invoke('taxonomy', 'node_update_index', $node);
$extra = module_invoke_all('node_search_result', $node);
$results[] = array(
'link' => url("node/{$item->sid}", array('absolute' => TRUE)),
'type' => check_plain(node_type_get_name($node)),
'title' => $node->title,
'user' => theme('username', array('account' => $node)),
'date' => $node->changed,
'node' => $node,
'extra' => $extra,
'score' => $item->calculated_score,
'snippet' => search_excerpt($keys, $node->body)
);
}
return $results;
}
I'd be happy if anyone would give me a better answer.

Module development: hook_form(), hook_menu() and how to get $_POST

I want to improve my knowledge in module development (which is far away from basic), so I try to develop a perimeter search module.
What I've achieved for now is a block containing a form:
function perimeter_search_block_view($delta = '') {
// Define an empty array for the block output.
$block = array();
switch($delta) {
case 'perimeter_search_box':
$block['subject'] = t('Perimeter search box');
$block['content'] = drupal_get_form('perimeter_search_form');;
break;
}
return $block;
}
/**
* Implementation of the perimeter search form
* #return array with form data
*/
function perimeter_search_form($form, &$form_state) {
$form = array(
'#action' => 'perimeter-search-results',
'keyword' => array(
'#type' => 'textfield'
),
'location' => array(
'#type' => 'textfield'
),
'perimeter' => array(
'#type' => 'select',
'#title' => t('Perimeter'),
'#options' => array('15 km', '30 km', '60 km', '120 km')
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Start search')
)
);
return $form;
}
I also have a function to output the search results:
/**
* Implementation of hook_menu()
* #return defined menu/page items
*/
function perimeter_search_menu() {
$items = array();
// Search results page
$items['perimeter-search-results'] = array(
'title' => t('Perimeter search results'),
'page callback' => 'perimeter_search_results',
'access arguments' => array('view perimeter search'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Processing job search queries
*/
function perimeter_search_results() {
$page_content = t('Search results');
return $page_content;
}
My (simple?) question is: how to get the post data (keyword, location, perimeter) in my perimeter_search_results() function?
Easy, you have to create the _submit function for your form, here an example:
function perimeter_search_form_submit($form, &$form_state) {
/*
* Your data handling goes here on the $form_state['values']['myfieldname']
* variable.
*/
drupal_set_message(t('Awesome, you managed to fill the form!'));
}
And if you need to validate..
function perimeter_search_form_validate($form, &$form_state) {
if($form_state['values'['myfieldname'] == '') {
form_set_error('', t('Hey, it doesn't work like that!'));
}
}
Just remember that if you add the attribute '#required' => TRUE to a form field, the field will be automatically validated to always require that field, so you don't need to use the validator for that field if you just need that it get compiled.

drupal form api checkboxes

I am using drupal form api and using checkboxes. I am getting problem in default checked values with it. following is the code snippet...
$result = db_query("SELECT nid, filepath FROM {content_type_brand}, {files} WHERE content_type_brand.field_brand_image_fid
= files.fid");
$items = array();
while ($r = db_fetch_array($result)) {
array_push($items, $r);
}
$options = array();
foreach( $items as $i ) {
$imagePath = base_path().$i['filepath'];
$options[$i['nid']] = '<img src="'.$imagePath.'"></img>';
}
$form['favorite_brands'] = array (
'#type' => 'fieldset',
'#title' => t('Favorite Brands'),
//'#weight' => 5,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['favorite_brands']['brands_options']
= array(
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $options_checked,// $options_checked is an array similar to $options but having elements which need to be checked by default...
'#multicolumn' => array('width' => 3)
);
but values are not checked by default... can anyone help what I am missing??
Thanks
Your $options_checked array should not be in the same format as your $options array. Your $options array contains nid => img tag pairs. Your $options_checked array should simply contain the nid values of the options that should be checked by default:
$options_checked = array(8,17);

Resources