How can I specify a default value and hide this checkbox ?
[states] => Array
(
[#type] => checkboxes
[#title] => Status
[#options] => Array
(
[active] => Active users
[inactive] => Inactive users
)
[#description] => Subscriptions matching the selected states will be exported.
[#required] => 1
)
I've tried
$form['states']['#default_value'] = 'active';
but it gives me an error..
thanks
Your code as written will produce a syntax error, since you're declaring the 'states' array wrong. This is a PHP syntax thing that doesn't really have anything to do with Drupal's forms API.
Try:
$form['states'] => Array (
'#type' => 'checkboxes',
'#title' => t('Status'),
'#options' => Array (
'active' => 'Active users',
'inactive' => 'Inactive users',
),
'#description' => t('Subscriptions matching the selected states will be exported.'),
'#required' => 1,
'#default_value' => 'active',
);
Unrelated to the syntax issue, a better way to do this would be to reset the entire form element as a '#type' = 'value'. For example $form['states']['#type'] = 'value'; $form['states']['value'] = 'whatever'
Related
So Drupal Forms API has options for generating a select box.
However, the example contains static information. I would like to generate a dynamic select list, in the "Drupal" way.
Here's the code example:
$form['selected'] = array(
'#type' => 'select',
'#title' => t('Selected'),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => $category['selected'],
'#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
);
I want the array under #options to become dynamic - should I just generate something before this beforehand, pass it to a variable and put it into the array? I'm not quite sure how I can preserve the structure of this code, and insert a way for a dynamic solution.
Yes, you need to generate your options array dynamically before the $form['selected'] array definition like this:
$myOptionsArray = myOptionsCallback($param1, $param2);
$form['selected'] = array(
'#type' => 'select',
'#title' => t('Selected'),
'#options' => $myOptionsArray,
'#default_value' => $category['selected'],
'#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
);
You can do it like this:
'#options' => custom_function_for_options($key)
and then define custom_function_for_options() like this:
function custom_function_for_options($key){
$options = array(
'Key Value 1' => array(
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue'
),
'Key Value 2' => array(
'paris' => 'Paris, France',
'tokyo' => 'Tokyo, Japan',
'newyork' => 'New York, US'
),
'Key Value 3' => array(
'dog' => 'Dog',
'cat' => 'Cat',
'bird' => 'Bird'
),
);
return $options;
}
The $key is on the basis of which $options will return a set of values.
$table_name = 'tbl_users';
$data = array('datetime'=>'NOW()',
'ipadress' => $ipaddress,
'name' => $name,
'dob' => $dob,
'nationality' => $nationality,
'address' => $address,
'city' => $city,
'state' => $state,
'pincode' => $pincode,
'phone' => $phone,
'email' => $email,
'mobile' => $mobile,
'weight' => $weight,
'height'=> $height,
'marital' => $marital,
'degree' => $degree,
'institute' => $institute,
'special' => $special,
'yearofpaas' => $yearofpaas,
'grade' => $grade,
'emplyment_history' => $emplyment_history,
'merits' => $merits,
'major_achivements' => $major_achivements,
'interview_attended' => $interview_attended,
'details' => $details,
'minctc_position' => $minctc_position,
'cv_file' => $cv_file,
'declaration' => $declaration);
echo "<pre>";
print_r($data);
drupal_write_record($table_name, $data);
I have this insert query but somehow records are not going in to the table..can anyone please help me...whats the problem in the query ?????
You're passing in NOW() as a string, which won't work. Try instead:
$data = array('timestamp' => date("Y-m-d H:i:s"),
drupal_write_record only works if table schema is present in my_module.install file.
From here: drupal_write_record saves (inserts or updates) a record to the database based upon the schema.
So, make sure that schema of tbl_users table is present.
Also, as mentioned by Dan U. use date("Y-m-d H:i:s") instead of 'Now()'.
The dblog can give you a good idea about the problem in your query. The path is admin/reports/dblog.
I have a problem with drupal_render (assuming that drupal_render is the right way for me to get what I want - feel free to correct me =).
I am building a form. Since the FAPI does not provide a "table"-field, I want to make one myself. My approach: use the theme()-function, specifically theme('table', ...) or theme_table(), and fill it with the respective form fields (with the intention of adding AHAH functionality later on). This forces me to use drupal_render as the value for the table cells, which causes some problems with the form elements.
The table collects numbers of employees by year, for the organisation the user is editing at this moment. The code looks as follows:
$form['employees'] = array(
'#type' => 'fieldset',
'#title' => t('Employees'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$employee_query = db_query("SELECT * FROM {employees} WHERE id_organisation = %d", $org['idoOrganisation']);
$employee = array();
while ($row = db_fetch_array($employee_query)) {
$employee[] = $row;
}
$header = array(
t('Year'),
t('Total'),
t('Internal'),
t('External'),
t('Aerospace')
);
$em_delta = 0;
$rows = array();
foreach($employee as $em_delta => $value) {
$form['employees'][$em_delta]['year'] = array(
'#title' => '',
'#type' => 'date_select', // Comes with the date module
'#date_format' => $format_year,
'#date_label_position' => 'within',
'#date_year_range' => '-50:+3',
'#default_value' => $value[$em_delta]['year'],
'#id' => 'edit-employees-' . $em_delta . '-year', // Allready a quickfix, since the form is rendered without id
'#name' => 'employees['.$em_delta.'][year]', // Same here
);
$form['employees'][$em_delta]['total'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value['total'],,
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-total',
'#name' => 'employees['.$em_delta.'][total]'
);
$form['employees'][$em_delta]['internal'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['internal'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-internal',
'#name' => 'employees['.$em_delta.'][internal]',
);
$form['employees'][$em_delta]['external'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['external'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-external',
'#name' => 'employees['.$em_delta.'][external]',
);
$form['employees'][$em_delta]['aero'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['aero'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-aero',
'#name' => 'employees['.$em_delta.'][aero]',
);
$rows[] = array(
drupal_render($form['employees'][$em_delta]['year']),
drupal_render($form['employees'][$em_delta]['total']),
drupal_render($form['employees'][$em_delta]['internal']),
drupal_render($form['employees'][$em_delta]['external']),
drupal_render($form['employees'][$em_delta]['aero']),
);
}
$form['employees']['table'] = array (
'#value' => theme('table', $header, $rows, array(), NULL)
);
Here are the problems I am encountering:
ID- and Name-Attributes of the form elements are empty. I found something on this on the drupal site and have made my peace with it (although I don't understand it), setting those attributes manually now.
Default-values of the text fields are ignored. The fields are empty. When I let drupal_get_form render the field, the default_value shows. Someone around here suggested to set the #value-property instead, but then again I read that this is something completly different and may cause problems.
The date_select-Field is not rendered in it's entirety. The wrappers are there, the select field however appears outside of the code, just before the table (i.e. where it appears in the code).
Let's hope that's it =)
Can anybody help? What am I doing wrong?
A colleague of mine pointed out that using drupal_render within the form function is not event remotely close to being a good idea, as it removes part of the form from the whole process of validating and submitting.
Thus, figuring out why the function does not work as intended is futile. The better approach would be to simply generate the necessary amount of form fields, let them be rendered as they are within drupal_get_form(), and use the forms theme-function later on to put them into a table.
Stupid me =)
I am using Drupal 7 with ubercart shipping. My client want the cost of shipping to be ups ground quote unless it is >=$50. Then he wants to charge a flat $50.
I created a flat rate that cost $50 and a ups ground quote.
I don't see a condition that uses the shipping quote to determine shipping method. Any ideas?
Got this figured out. Not pretty but it works. Just go to the ups option and add a condition with php evaluation. note that you must have two payment methods available for conditions to work. Paste this code.
$method = array (
'id' => 'ups',
'module' => 'uc_ups',
'title' => 'UPS',
'operations' => array (
'configure' => array (
'title' => 'configure',
'href' => 'admin/store/settings/quotes/settings/ups',
),
),
'quote' => array (
'type' => 'small_package',
'callback' => 'uc_ups_quote',
'accessorials' => array (
'03' => 'UPS Ground',
),
),
'ship' => array (
'type' => 'small_package',
'callback' => 'uc_ups_fulfill_order',
'file' => 'uc_ups.ship.inc',
'pkg_types' => array (
'02' => 'Customer Supplied Package',
'01' => 'UPS Letter',
'03' => 'Tube',
'04' => 'PAK',
21 => 'UPS Express Box',
24 => 'UPS 25KG Box',
25 => 'UPS 10KG Box',
30 => 'Pallet',
'2a' => 'Small Express Box',
'2b' => 'Medium Express Box',
'2c' => 'Large Express Box',
),
),
'cancel' => 'uc_ups_void_shipment',
'enabled' => 1,
'weight' => '0',
);
$details->postal_code = $order->delivery_postal_code;
$details->zone = $order->delivery_zone;
$details->country = $order->delivery_country;
$quote = uc_ups_quote($order->products, $details , $method);
return ($quote['03']['rate'] < 50);
The method can be adjusted for other types of shipping this condition applies to only ups ground. You could modify this to automatically choose the least expensive method between ups/fedex or something like that too.
Anyway. Hope this helps someone else out there.
$instance = array(
'field_name' => $field_name,
'entity_type' => $entity,
'bundle' => $bundle,
'field types' => 'list_boolean',
'widget' => array(
'type' => 'options_onoff',
'settings' => array('display_label' => 1)
),
'default_value' => array(array('value' => 1)),
);
this is not taken, and i have to save it twice in the admin contenttype - field/edit,
until it takes it ...
i now exported the finished field with the features module,
and took the generated code - suddenly it works, with default_value
i guess i was missing the property module on the field, also field types is inexistant ..
In your field definition, you have to set the allowed_values in the settings array in order for the default_value in the instance to get picked up.
so like this assuming you are doing this in a module
$fields[] = array(
'field_name' => '$field_name',
'type' => 'list_boolean',
'settings' => array(
'allowed_values' => drupal_map_assoc(range(0, 1)),
),
);
Instead of using 'default_value', I got it to work by using 'default_value_function' and creating a function that returns array(array('value' => 1)).