I have been working on creating a custom form with an autocomplete field that will search the database for a specific content type's field values and bring back the results so the user can select one for that input box.
My main problem is actually knowing how to target the specific field in the database.
The database information resides in a content type called 'Parts' and within that content type there is a field called 'cross_reference'.
We have thousands of parts and each one has many cross reference values as the 'cross_reference' field is set to unlimited. I need the autocomplete of the custom form to look through these 'cross_reference' values.
Menu and Page
function frontcross_menu(){
$items['frontcross'] = array(
'title' => 'Front Cross',
'page callback' => 'frontcross_page_callback',
'access arguments' => array('access content'),
'description' => t('Front cross'),
'type' => MENU_CALLBACK,
);
$items['cross/autocomplete'] = array(
'title' => 'Autocomplete for cross',
'page callback' => '_cross_autocomplete',
'access arguments' => array('use autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
function frontcross_page_callback(){
return drupal_get_form('frontcross_form');
}
Form
function frontcross_form($form,&$form_state){
$form = array(
'#method' => 'post','#default_value' => 'Status Message',
'#id' => 'frontcross_form',
'field_across_reference_value' => array(
'#type' => 'textfield',
'#required' => FALSE,
'#autocomplete_path' => 'cross/autocomplete',
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Submit'),
'#name' => t('Submit'),
'#id' => t('Submit'),
),
);
$form["#submit"][] = 'frontcross_submit';
return $form;
}
Submit
This function puts the form information into a URL query string on the catalogue results page
function frontcross_submit($form,&$form_state){
$options = array('' => TRUE);
$options['query']['field_cross_reference_value'] = $frontcrossval;
$frontcrossval = trim($form_state['values']['field_across_reference_value']);
if($frontcrossval){
$options['query']['field_cross_reference_value'] = $frontcrossval;
}
$form_state['redirect'] = url($GLOBALS['base_url'].'/cross-reference',$options);
}
Autocomplete Function
This is the part that I am stuck on. It needs to look though the database for values that belong to content type 'Part' and its field 'cross_reference' which has multiple values.
I have left the below code as the example I found.
function _cross_autocomplete($string) {
$matches = array();
$result = db_select('cities', 'c')
->fields('c', array('city'))
->condition('city', '%' . db_like($string) . '%', 'LIKE')
->execute();
foreach ($result as $row) {
$matches[$row->city] = check_plain($row->city);
}
drupal_json_output($matches);
}
I hope my explanation is concise enough. Help will be greatly appreciated.
Thanks
Jo
Related
I a stupid problem but nothing is working.
Lets say i have a url : something/4
Then i use a function users_view to display output.
Menu for users_view
$items['users/%'] = array(
'title' => 'your info',
'page arguments' => array(1),
'page callback' => 'users_view',
'access arguments' => array('view user'),
'type' => MENU_NORMAL_ITEM,
);
Now i wont to get to it ...
I used another function to get to it via a link in main menu. I created a function user_created that gives me the user_id(this is just an example)
function users_menu (){
$items=array();
$items['users/'] = array(
'title' => 'Your info',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array ('view user'),
'page callback' => 'user_created',
);
return $items;
}
function user_created(){
global $user;
$itemid=array();
$temp_user= $user->uid;
drupal_set_title('Your data');
$result = db_query("SELECT user_id FROM {user_test}
WHERE current_user=:s", array(':s' => $temp_user));
foreach($result as $item) {
$itemid = $item->user_id;
}
REDIRECT to url users/$itemid
}
I have used drupal drupal_goto , $form['redirect'] but nothing happens ...
The problem is it just says users/
You have an error, you have to fetch the result like this :
$result = db_query("SELECT user_id FROM {user_test}
WHERE current_user=:s", array(':s' => $temp_user));
$itemid = $result->fetchField();
drupal_goto('user/'.$itemid);
I created a simple user form that allows the user to give a database name and when the user submitt the drupal site will be connected to the given databasename, I defined my database in the file settings.php: myDataBase and when I try
db_set_active('myDataBase');
it works fine but when I type myDataBase in the text field and submitt and then use the submitted value to connect to the database it didnt work.
<?php
function testform_menu() {
$items = array();
$items['test'] = array(
'title' => 'connect an external database',
'page callback' => 'drupal_get_form',
'page arguments' => array('testform'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function testform($form, &$form_state) {
$form = array();
$form['Databasename'] = array(
'#type' => 'textfield',
'#title' => t('enter the database name'),
'#description' => t('a user interface to switch between databases'),
'#size' => 28,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function testform_validate($form, &$form_state) {
}
function testform_submit($form, &$form_state) {
if (db_set_active('myDataBase'))
{
drupal_set_message('database connected'); // works
}
// $database_name =$form_state['values']['Databasename']; // ius not working!!
// if ($database_name ) {
// db_set_active($database_name); // I even tried to parsing the submitted value to a string but didnt work in both case
// drupal_set_message(t('connected to the database'.' '.$database_name));
// }
db_set_active();
}
You need a function to manage your form submit, for example:
function testform_submit($form, &$form_state)
{
//Do your stuff
}
Or you can define the submit function using the #submit key in your form:
'#submit' => array('your_custom_function_name')
More info: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit_property
Inside this function you can process your form.
function MyModule_menu() {
$items['blah'] = array(
'title' = 'blah',
'page callback' => 'blah_page',
'type' => MENU_NORMAL_ITEM
);
$items['clickPath'] = array(
'title' => 'A title',
'page callback' => 'clickPath_page',
'type' => MENU_CALLBACK,
);
return $items;
}
function blah_page() {
$output = drupal_get_form(MyModule_form);
return $output;
}
function clickPath_page() {
return ('you clicked me!');
}
function MyModule_form($form,&$form_state) {
$output = '<div id="clickDiv">Click me</div>';
$form['blah'] = array(
'#type' => 'markup',
'#value' => $output,
'#ahah' => array(
'event' => 'click',
'path' => 'clickPath',
'wrapper' => 'clickDiv',
),
);
return $form;
}
Why won't the above work? Is it not possible to use ahah and events on form types of 'markup'? Do I have to use my own custom javascript?
You can stop reading here! I would like to end my sentences and question here, but stackoverflow is forcing me to input a minimum amount of characters. Apologies in advance!!!!
If you look at the Form API under "Special Elements" you can see that the #ahah attribute is not supported for the markup form type.
So I'm afraid you will have to roll your own JS in this case, or convert the markup element into a normal form element (which it doesn't look like will work for your purposes).
I am a beginner in the module development field and i am facing a problem.
I have created the .module file with the hook_menu function having following peice of code :-
$items['game/add_tournament/view_tournament']=array(
'title'=>'View Tournament',
'description'=>'Tournament View',
'page callback'=>'game_view_tournament_page',
'access callback' => 'user_access',
);
My Page callback is following :-
function game_view_tournament_page() {
$header_table_edit = array(
// first cell has the following text 'Title'
array('data' => t('Tournament Name')),
// second cell has the following text 'Link to edit'
array('data' => t('No of Weeks')),
array('data' => t('Start Date')),
array('data' => t('End Date')),
array('data' => t('Edit'))
);
$query = db_query
( "select tournament_id ,tournament_name ,tournament_no_of_weeks ,tournament_start_date ,tournament_end_date from {tournaments} ");
while ($data = db_fetch_object($query))
{
$rows_table_edit[] = array(
array('data' => l($data->tournament_name, 'game/add_tournament/view_tournament/' . $data -> tournament_id)),
array('data' => t($data->tournament_no_of_weeks)),
array('data' => t($data->tournament_start_date)),
array('data' => t($data->tournament_end_date)),
array('data' => l(t('Edit'),'game_tournament_edit/'.$data -> tournament_id.'/edit')) );
}
$caption_table_edit = t('Table for edit nodes');
return theme('table', $header_table_edit, $rows_table_edit);
}
Till this point the site works fine. Now comes the problem.
At this poinit i assume that another callback will be called when i hit edit button and that callback also should be registered in my hook_menu function.
Here i don't know if this method is being called back or not .Since everytime i hit edit button i get a Page not found error. The following is the callback
$items['game_week_edit/%game_abc/edit']=array(
'type' => MENU_LOCAL_TASK,
'access arguments' => array('access content'),
'page callback' => 'game_week_edit_page',
'page arguments' => array(1),
);
// The above code is in hook_menu function and the following code is outside hook_menu . Let me know if i am doing anything wrong.
function game_week_edit_page($abc_id){
return drupal_get_form('game_week_edit_form',$abc_id);
}
function game_week_edit_form( &$form_state, $abc_id) {
$form = array();
$options = array();
$sql = "SELECT game_week_id,tournament_id,start_time,open_time,close_time FROM {game_week} where game_week_id='$abc_id'";
$r = db_query($sql);
$sql_tournament = "SELECT tournament_name FROM {tournaments} ";
$r_tournament = db_query($sql_tournament);
while ($row = db_fetch_array($r_tournament)) {
$options[$row['tournament_name']] = $row['tournament_name'];
}
while ($row = db_fetch_object($r))
{
$form['tournament_name'] = array(
'#type' => 'select',
'#required'=>true,
'#title' => t('Select Tournament'),
'#options' => $options,
'#weight'=>1,
'#description' => t('<br>'),
);
$form['start_time'] = array(
'#type' => 'textfield',
'#required'=>true,
'#value'=>t($row->start_time),
'#title' => t('Enter Start Time'),
'#size'=>40,
'#maxlength'=>128,
'#weight'=>2,
'#description' => t('Please enter start time.'),
);
$form['open_time'] = array(
'#type' => 'textfield',
'#required'=>true,
'#value'=>t($row->open_time),
'#title' => t('Enter Tournament Start Time'),
'#size'=>40,
'#maxlength'=>128,
'#weight'=>3,
'#description' => t('Booking open time.'),
);
$form['close_time'] = array(
'#type' => 'textfield',
'#required'=>true,
'#value'=> t($row->close_time),
'#title' => t('Booking close time'),
'#size'=>40,
'#maxlength'=>128,
'#weight'=>4,
'#description' => t('Booking close time.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update Tournament'),
'#weight'=>5
);
}
return $form;
}
In hook_menu, try not naming your wildcard in the edit item. "%game_abc" is telling Drupal to call a function "game_abc_load" (that is, the name appended by "_load") and use the returned value as the first argument to your page callback "game_week_edit_page". If you don't have such a function, there will be problems.
In this case it looks like you just want to pass the integer id, in which case you can just use "%".
I have a Drupal form wherein someone inputs information, and I need to do a database query to check if it is valid before submitting. I would like to either have a button the user can click to check the validity (or have it be done automatically after the user leaves that field), and then display some information about his selection.
I know I can use hook_form_submit to review a form when it is submitted and then stop the process if there are any errors, but I would like the user to be able to confirm they have selected the correct thing before submitting the form.
I haven't personally tried this module, but it might be what you're looking for:
http://drupal.org/project/ajax
If you're just looking for a way to do real-time lookups (e.g. entering the book barcode and getting the title), you can also use Drupal's autocomplete feature, but it will require you to write your own autocomplete function to handle the database lookups.
Take a look at: Basic form with validate handler. You really just need to add a function similar to mymodule_myform_validate($form, &$form_state) { ... }. From the linked page:
"This adds a new form field and a way to validate it with a validate function, also referred to as a validate handler."
<?php
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_form() {
return drupal_get_form('my_module_my_form');
}
function my_module_my_form($form_state) {
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE,
'#default_value' => "First name",
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE,
);
// New form field added to permit entry of year of birth.
// The data entered into this field will be validated with
// the default validation function.
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => "Year of birth",
'#description' => 'Format is "YYYY"',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
// This adds a handler/function to validate the data entered into the
// "year of birth" field to make sure it's between the values of 1900
// and 2000. If not, it displays an error. The value report is // $form_state['values'] (see http://drupal.org/node/144132#form-state).
//
// Notice the name of the function. It is simply the name of the form
// followed by '_validate'. This is the default validation function.
function my_module_my_form_validate($form, &$form_state) {
$year_of_birth = $form_state['values']['year_of_birth'];
if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
}
}
?>