How to clear selected values from multiple select field - drupal

I have a form with a multiple select field where a user can select several values in it
$form['export_type_section']['export_type'] = array(
'#title' => t('Export types'),
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 10,
'#options' => $options,
'#ajax' => array(
'event' => 'change',
'callback' => 'test_export_form_export_type_ajax_callback',
),
);
I have another field in the form
$form['document_type'] = array(
'#type' => 'select',
'#options' => array(0 => t('All Types'), 1 => t('Test1'), 2 => t('Test2'), 3 => t('Test3')),
'#title' => 'Document Types',
'#default' => 0,
'#ajax' => array(
'event' => 'change',
'callback' => 'test_export_form_document_type_ajax_callback',
),
);
I am trying to figure out how to reset the export_type field when the document_type is changed. I would like to clear the selected values so that the user can reselect what is needed whenever the document_type changes. I tried several formats in the test_export_form_document_type_ajax_callback but none worked.
Anybody knows how to do it?

Related

submit button appears only after selection from the dropdown list doesn't work - drupal 7

Submit button appears after selecting from the select list drop down ,
Can anyone help me understand why it is not working?
$form['user_fields']['optinal_packages'] = array(
'#type' => 'select',
'#title' => t('Optional Packages'),
'#options' => $packages_array,
//'#weight' => 15,
'#description' => t('Please press the "Push" button to update device package.'),
'#default_value' => -1,
);
$form['user_fields']['push'] = array(
'#type' => 'submit',
'#value' => t('Push'),
// '#weight' => 16,
'#prefix' => '<div id="phone_user_push_package">',
'#suffix' => '</div>',
'#states' => array(
'visible' => array( // Action to take: Make visible.
//':input[name="optinal_packages"]' => array('!value' => '-1'),
'select[name="optinal_packages"]' => array('!value' => '-1'),
),
),
);
Thanks
dana
Change the code to be
$form['user_fields']['push'] = array(
'#type' => 'submit',
'#value' => t('Push'),
// '#weight' => 16,
'#prefix' => '<div id="phone_user_push_package">',
'#suffix' => '</div>',
'#states' => array(
'invisible' => array( // edited line
':select[name="optinal_packages"]' => array('value' => '-1'), // edited line
),
),
);
Change select[name="optinal_packages"] to be :select[name="optinal_packages"]. If it doesn't work, try to change the selector to be CSS-like #edit-optinal-packages.
Hence the code will be:
'#edit-optinal-packages' => array('value' => '-1'),

Add allowed values list programmatically in drupal 7 CCK field "list_text"

I was wondering if i can create programmatically a CCK field instance and insert the "allowed_values" in a single stage. So i tried this:
field_create_instance(array(
'field_name' => 'card number',
'entity_type' => 'payment_method',
'bundle' => 'debit_card',
'label' => t('Debit/Credit card'),
'description' => t('Add card\'s number '),
'widget' => array(
'type' => 'options_select',
'weight' => 0,
'settings' => array('size' => 50),
),
'required' => TRUE,
));
I've tried some case i.e to set in 'setting' => array( 'allowed_values' => array( 1, 2, 3 ) ) but nothing happened. Any suggestions?
Solution:
function MY_MODULE_install() {
field_create_field(array(
'field_name' => 'months',
'type' => 'list_text',
'cardinality' => 1,
'settings' => array('allowed_values_function' => 'get_months'),
'entity_types' => array('user', 'node'),
));
}
function get_months() {
$months = array( '01', '02', '03',...'12');
return $months;
}
Warning: Callback function must always be in *.module file of your custom module.

View fields from the same database column

So, I have serialized data within the uc_orders table. This data has several parameters of interest each of which I want to create a unique field from.
Each one of these $data values work on their own. Obviously I am rewriting the value on the second declaration. How can I reference the same data location twice so that both of the fields can be used and I can explode all of my serialized data into separate fields?
function uc_order_views_data() {
$data['uc_orders']['data'] = array(
'group' => t('Order') . ':data',
'title' => t('Arrival Date'),
'help' => t('Arrival date choosen by customer during checkout.'),
'field' => array(
'handler' =>'uc_order_handler_field_arrive_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);
$data['uc_orders']['data'] = array(
'group' => t('Order') . ':data',
'title' => t('Ship Date'),
'help' => t('The date to ship the order by.'),
'field' => array(
'handler' =>'uc_order_handler_field_ship_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);
}
I finally found the answer.
$data['uc_orders']['data2'] = array(
'group' => t('Order') . ':data',
'title' => t('Arrival Date'),
'real field' => 'data',
'help' => t('Arrival date choosen by customer during checkout.'),
'field' => array(
'handler' =>'uc_order_handler_field_arrive_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);
$data['uc_orders']['data'] = array(
'group' => t('Order') . ':data',
'title' => t('Ship Date'),
'help' => t('The date to ship the order.'),
'field' => array(
'handler' =>'uc_order_handler_field_ship_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);
The solution is to add 'real field' => 'field name', and change the $data id.

How do I add an onchange handler to a dropdown list in Drupal?

How can I add an onchange handler to a dropdown list in Drupal? The dropdown list is added using hook_form(). I have to perform a function depending on the onchange function.
Is there a way to do this?
You can add a form like this:
hook_form()
{
$form1["dropdown"] = array(
'#type' => 'select',
'#title' => t('Preview the page with themes available'),
'#options' => $ptions,
'#default_value' => 'defalut_value',
'#attributes' => array('onchange' => "form.submit('dropdown')"),
);
//Submit button:
$form1['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit '),
'#attributes' => array('style' => 'display: none;'),
);
Now you can add submit functionality using hook_submit().
Here is the simple example using '#ajax' property
$form['select'] = array(
'#type' => 'select',
'#title' => 'Option #1',
'#options' => $option,
'#ajax' => array(
// Call function that rebuilt other field
'callback' => 'ajax_load_field',
'method' => 'replace',
// div to be get replace by function output
'wrapper' => 'chart',
'effect' => 'fade'
),
);
If you generate another form field by using this drop down.
Then use AHAH for that.
$form['my_form_submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 1,
'#submit' => array('my_form_submit'),//none JS version
'#ahah' => array(
'event' => 'click',
'path' => 'mymodule/js', //Your ajax function path
'wrapper' => 'myform-wrapper',
'method' => 'replace',
'effect' => 'fade',
'progress' => array(
'type' => 'bar',
'message' => t('Loading...')
)
),
While I am sure that Nicholas is way past this issue by now, this may help some who are searching for a solution.
I'm using D7 and getting the single quotes around dropdown encoded to ' I suppose by >check_plain. >How do I avoid that? – Nicholas Tolley Cottrell Jun 2 at 16:03
I just found the "Drupal" way of doing this.
Step 1, set a variable to contain dropdown by using drupal_add_js:
drupal_add_js(array('mymodule' => array('varname' => 'dropdown')), 'setting');
Step 2, add the attributes line as
'#attributes' => array('onchange' => "form.submit(Drupal.settings.mymodule.varname)"),

drupal 7. how to refactor a form array

I am creating a wizard from the Drupal example file and would like to refactor the segments of code that are repeated when setting up items like options and radios.
I have already tried a simple function passing "ordinary" and "preferential" but can't find a way to make it work.
Can someone give me an idea of the best way to do this?
unfactored code is as below:
function services_wizard_share_capital_classes($form, &$form_state) {
$form['share_classes']['type_of_class'] = array(
'#type' => 'select',
'#title' => t('What type of share will this class be?'),
'#options' => array(
1 => t('Ordinary'),
2 => t('Preferential'),
),
);
$form['ordinary']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Ordinary Shares"),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => '1'),
),
),
);
$form['preferential']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Preferential Shares"),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => '2'),
),
),
);
return $form;
}
I'm not sure about this being the best way but it's certainly a way to refactor your code:
function _services_wizard_share_capital_classes_add_el(&$form, $name, $description, $index) {
$form[$name]['share_type'] = array(
'#type' => 'item',
'#description' => t($description),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => "$index"),
),
)
);
}
function services_wizard_share_capital_classes($form, &$form_state) {
// Other code
_services_wizard_share_capital_classes_add_el($form, 'ordinary', 'You chose Ordinary Shares', 1);
_services_wizard_share_capital_classes_add_el($form, 'preferential', 'You chose Preferential Shares', 2);
// etc...
}

Resources