Drupal 6 : Not populating select list from db_query() - drupal

I have made a custom form in a module where I have used a select list & I am trying to populate that with the name of OG name.
I wrote a function for db_query() & that is giving me exact out put but I am not able to populate that in from select.
Function for db_query():-
function taskform_project_select(){
$options=array();
$project_query = "SELECT node.title FROM {node}, {og} WHERE node.nid = og.nid";
$project_details = db_query($project_query);
while($project_title = db_fetch_object($project_details)){
$options = $project_title->title;
dpm($options);
}
return $options;
}
Code in Form:-
$options = taskform_project_select();
$form['edproject'] = array(
'#type' => 'select',
'#title' => t('Project'),
'#options' => $options,
'#description' => t('Choose a project'),
'#prefix' => '<td>',
'#suffix' => '</td>',
);
Thanks :)

Correct syntax is:
$options[] = $project_title->title;
..with square brackets.
$options[$project_title->title] = $project_title->title;
provides a meaningful key value to the result. You could also retrieve the nid and use that as the key.

Related

Content filter does not search drupal7

I have added a drop down of my courses title at the filter second of the drupal - admin - content area.
http://www.XYZ.com/demo/admin/content
But when I select any of the title and hit Filter nothing gets appear. The data which showed previously it shows again, nothing happens really.
My code for the add filter drop down:
function products_form_node_admin_content_alter(&$form, &$form_state){
$results = db_query("SELECT r.nid, r.title FROM {node} AS n
LEFT JOIN {node_revision} AS r ON r.nid = n.nid
WHERE type = 'product'")->fetchAll();
$optionsF = Array ( '[any]' => 'any' );
foreach($results as $key => $result) {
$options[$result->title] = $result->title;
}
$options = $optionsF + $options;
$course_titles['title'] = Array
(
'#type' => 'select',
'#options' => $options,
'#title' => 'title',
'#default_value' => 'any'
);
$form['filter']['filters']['status']['filters'] = $form['filter']['filters']['status']['filters'] + $course_titles;
$uid_column = array('uniqueid' => array(
'data' => 'UniqueID',
'field' => 'n.nid'
));
$form['admin']['nodes']['#header'] = $form['admin']['nodes']['#header']+$uid_column;
foreach ($form['admin']['nodes']['#options'] as $key => $row) {
$node = node_load(array('nid' => check_plain($key)));
$form['admin']['nodes']['#options'][$key]['uniqueid'] = $node->field_unique_code_course['und'][0]['value'];
}
}
Does any body have any idea what is lacking in my code or method?
Cheers!!!
I would recommend using Admin Views, it's built for this purpose.
https://drupal.org/project/admin_views

Drupal 6: Checkboxes table not rendering properly

I'm working on a Drupal 6 module which I want to generate a table with checkboxes in each row from data I have saved in a database. The table is being generated fine, but the checkboxes are not rendering in the table but are instead having their node id's put below the table. See the screenshot below:
"21" is the node id of "Test Question 01", and "19" is the node id of "Test Question 02".
The code I'm using (yes, it is all in a theme function which isn't ideal. I'm planning on moving stuff around once the checkboxes problem is resolved):
function theme_qt_assignment_questions_table($form) {
// Get the questions from the database
$db_results = db_query('SELECT {qt_questions}.nid, title, lesson, unit FROM node INNER JOIN {qt_questions} on {node}.nid = {qt_questions}.nid WHERE lesson = %d AND unit = %d',
$form['#lesson'], $form['#unit']);
// Define the headers for the table
$headers = array(
theme('table_select_header_cell'),
array('data' => t('Title'), 'field' => 'title'/*, 'sort' => 'asc'*/),
array('data' => t('Lesson'), 'field' => 'lesson'),
array('data' => t('Unit'), 'field' => 'unit'),
);
while($row = db_fetch_object($db_results)) {
$checkboxes[$row->nid] = '';
$form['nid'][$row->nid] = array(
'#value' => $row->nid
);
$form['title'][$row->nid] = array(
'#value' => $row->title
);
$form['lesson'][$row->nid] = array(
'#value' => $row->lesson
);
$form['unit'][$row->nid] = array(
'#value' => $row->unit
);
}
$form['checkboxes'] = array(
'#type' => 'checkboxes',
'#options' => $checkboxes,
);
// Add the questions to the table
if(!empty($form['checkboxes']['#options'])) {
foreach(element_children($form['nid']) as $nid) {
$questions[] = array(
drupal_render($form['checkboxes'][$nid]),
drupal_render($form['title'][$nid]),
drupal_render($form['lesson'][$nid]),
drupal_render($form['unit'][$nid]),
);
}
} else {
// If no query results, show as such in the table
$questions[] = array(array('data' => '<div class="error">No questions available for selected lesson and unit.</div>', 'colspan' => 4));
}
// Render the table and return the result
$output = theme('table', $headers, $questions);
$output .= drupal_render($form);
return $output;
}
Turns out my attempt at simplification of the problem was, in fact, the problem. Namely, doing everything in hook_theme isn't correct. Rather, I defined a function that pulls the info from database and creates the checkboxes array and call it in hook_form as such:
$form['questions_wrapper']['questions'] = _qt_get_questions_table($node->lesson, $node->unit);
At the end of this function (_qt_get_questions_table()), I specify the theme function which put everything into the table as such:
$form['#theme'] = 'qt_assignment_questions_table';
I'm still very new to Drupal so this explanation may not be the best to someone having the same problem, but hopefully it will help.

cannot get checkboxes value using drupal form api

i have form in drupal which uploads images and has got few checkboxes in it.
Here is the form:
$form['checklist_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Check List'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['checklist_fieldset']['heating'] = array(
'#type' => 'checkboxes',
'#title' => t('Heating options'),
'#options' => array(
'0' => t('Yes'),
'1' => t('No')
),
'#description' => t('Heating details.')
);
and here is my submit function where i am processing image upload and grabbing the checkboxes value as well. I am getting the success message and image is getting uploaded but not getting the value of check boxes.
function property_add_view_submit($form,&$form_state){
$validators = array();
if($file = file_save_upload('p_file1',$validators,file_direcotry_path)){
$heating = array_keys($form_state['values']['heating']);
drupal_set_message(t('Property Saved! '.$heating));
dpm( $form_state['values']['heating']);
}
When you use #options on a FAPI element the value passed to the $form_state is the array key, so you don't need to use array_keys().
I'm not sure why you're using checkboxes for a yes/no, usually one would use a simple checkbox element. However if that's really what you want to do:
Your #options can't contain on option with 0 as the array key, it will be automatically filtered out and you'll never know if that option has been checked.
You should use $heating_options_chosen = array_filter($form_state['values']['heating'] to get the selected checkbox options.
I honestly think your code should look like this though:
$form['checklist_fieldset']['heating'] = array(
'#type' => 'checkbox',
'#title' => t('Heating options'),
'#options' => array(
'1' => t('Yes'),
'0' => t('No')
),
'#description' => t('Heating details.')
);
$heating_checked = $form_state['values']['heating'] == 1;
If I have checkbox Friends and options are like
[ ] abc
[ ] def
[ ] ghi
[ ] jkl
And I want to know which options user have marked, then use below function.
if ($form_state->getValue('friends') != NULL) {
foreach ($form_state->getValue('friends') as $key => $value) {
if ($value != 0) {
$friends = $friends . ", " . $key;
$friends = substr_replace($friends, "", 0, 1);
}
}
}
If user has chosen abc and ghi then you will get 1,3 as result in $friends
If you wanted to know the value then use $friends = $friends.", ".$value;
it worked for me..hope it will help you as well :)

drupal form_execute for repopulation

I need a working example of how to repopulate form fields with the help of drupal_execute($form_id,$form_state). I can repopulate by altering the default_values as follows:
function sample_myform($form_state){
$form['field']['name'] = array(
'#type' => 'textfield',
'#title'=> 'Name: ',
'#maxlength'=> 127,
'#default_value'=> $form_state['values']['name'],
);
$form['field']['button1'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function sample_myform_form_alter(&$form,$form_state,$form_id){
if($form_id=='sample_myform'){
$id = db_result(db_query("select max(id) from test2"));
$name = db_result(db_query("select name from test2 where id ='$id'"));
$form['field']['name']['#default_value'] = "$name";
drupal_set_message(t('Name: '.$name." id: ".$id));
}
}
However, i want to use drupal_execute for repopulation. Any suggestions?
drupal_execute calls drupal_prepare_form which calls hook_form_alter, so you should be fine. On the other hand, you could pass your values in $form_state['values'] like this:
$form_state['values']['name'] =
db_result(db_query("select name from test2 where id ='$id'"));
drupal_execute('sample_myform', $form_state);
However, if #tree is true for the element, the first line should read
$form_state['values']['field']['name'] =
db_result(db_query("select name from test2 where id ='$id'"));

Drupal filter_form form input

This drupal form snippet will give me a textarea with user able to change filter to full html/wysiwyg mode.
My Questions: How can I default to to full html mode?
function MY_MODULE_admin() {
$form = array();
$form['format'] = filter_form($form->format);
// MY_MODULE - ** Image 1 **
$form['MY_MODULE_image_1'] = array(
'#type' => 'textarea',
'#title' => t('Image 1'),
'#default_value' => variable_get('setup_image_1', 'image_1.jpg'),
'#description' => "Current value =" .variable_get('setup_image_1', 'image_1.jpg'),
'#required' => TRUE,
);
This did the trick.
$form = array();
$form['carousel_setup_image_1']['accepted_text_1'] = array(
'#type' => 'textarea',
'#title' => t('Image 1 - Carousel '),
'#default_value' => variable_get('carousel_setup_image_1', 'carousel_image_1.jpg'),
'#description' => "Current value =" .variable_get('carousel_setup_image_1', 'carousel_image_1.jpg'),
);
$form['carousel_setup_image_1']['format'] = filter_form(2, NULL, array('accepted_text_1_format'));
Well there are two ways.
One, you can set that role's default format to Full HTML under Input Formats.
Two, you can say $form['format']['#default_value'] = 2 (I think Full HTML is 2). This will preselect Full HTML.
however, I am not sure why $edit['carousel_setup_image_1']['accepted_text_1']) does not contain entered value.
function carousel_setup_block($op = 'list', $delta = 0, $edit = array())
{
switch($op)
{
// case save (save configuration values)
case 'save':
variable_set('carousel_setup_image_1',
$edit['carousel_setup_image_1']['accepted_text_1']);
break;
}
}
For Drupal 7: The best way to handle (or leverage) the Drupal text filter system is to use not 'textarea', but the 'text_format' field type, like this, which will render a textarea with the filter select below:
$form['holycrap'] = array(
'#type' => 'text_format',
'#format' => NULL, // <- Let Drupal handle the default, based upon user role
'#title' => t('The HTML for the Holy Crap block above the Main Menu.'),
'#default_value' => variable_get('caplogin_holycrap', _caplogin_holycrap_default_html()),
);
return $form;

Resources