I have a function autocomplete like this:
function find_noreff_auto($string=NULL){
$matches = array();
db_set_active('data');
$result = db_query("SELECT * FROM reffnum where LOWER(noreff) LIKE LOWER('%%$string%%') AND journaled='0' AND noreff NOT IN (SELECT noreff from tmpreffnum) ORDER BY noreff ASC");
while ($row=db_fetch_object($result) )
{
$matches[$row->noreff.'-'.number_format($row->nominal,2)] = t($row->noreff.': '.number_format($row->nominal, 2));
}
drupal_json($matches);
drupal_set_message($matches);
}
This function:
1. If I write some character, it search from reffnum the noreff value
2. If I select one value, it give me result like this: NOREFF123 - 1.000,00
And this is the form:
$form['input']['noreff10'] = array(
'#type' => 'textfield',
'#title' => t('No Reff10'),
'#size' => 60,
'#default_value' => $noreff10,
'#description' => 'Nominal from '.$noreff10.' is '.$nominalnoreff10,
'#autocomplete_path' =>'noreff/autocomplete',
'#ahah' => array(
'event' => 'change',
'path' => ahah_helper_path(array('inputan')),
'wrapper' => 'inputan-wrapper',
'method' => 'replace',
'progress' => 'throbber',
),
'#prefix' => '<div class="s-form">', '#suffix' => '</div>',
);
I want $nominalnoreff10. What should I have to do?
$nominalnoreff10 = $noreff10['nominal']; doesn't give me the value.
Related
I have two forms on my Drupal page, one form is populating its fields when value of the first form is changed (when you choose an item from a select box). It seems to work fine, untill I try to submit the form, which won't do much, since in my browser it says this error:
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
<?php
function my_form($form, &$form_state) {
$form['form_1'] = [
'#type' => 'fieldset',
'#title' => "Select box",
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['form_1']['id'] = [
'#title' => t('Choose:') ,
'#type' => 'select',
'#options' => [
'1' => 'ONE',
'2' => 'TWO',
'3' => 'THREE'
],
'#required' => '1',
'#ajax' => [
'callback' => 'my_form_state_ajax_callback',
'wrapper' => 'form_edit',
],
];
//--------
$form['form_2'] = [
'#type' => 'fieldset',
'#title' => "This is form",
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#prefix' => '<div id ="form_edit">',
'#suffix' => '</div>',
];
$form['form_2']['name'] = [
'#type' => 'textfield',
'#title' => 'Name',
'#required' => TRUE,
];
$form['form_2']['button'] = [
'#type' => 'submit',
'#value' => "Create",
'#submit' => ['my_form_submit'],
];
return $form;
}
function my_form_state_ajax_callback($form, &$form_state) {
$values = $form_state['values'];
$id = $values['id'];
if($id > 0) {
$form['form_2']['name']['#value'] = "this works!";
} else {
if(isset($form['form_2']['name']['#value'])) unset($form['form_2']['name']['#value']);
}
return $form['form_2'];
}
How to fix this error? It doesn't really matter what is inside the callback function, it can be empty and the error will still occur.. What am I doing wrong?
I am trying to create some settings for a module and am following the guide on Drupal.org. I am also comparing my work to existing modules.
The config menu appears in the correct place with the correct fields but when I hit save no input is saved. (I have run cache clear and registry rebuild.)
Would anyone know what I am doing wrong here? I cannot see how my stuff differs.
On my .admin.inc file I have set the form as such:
function contact_page_settings() {
$form = array();
$config = contact_page_default_settings();
$form['#tree'] = TRUE;
$form['contact_page_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Top Section'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'top-title' => array(
'#type' => 'textfield',
'#title' => t('Top title'),
'#default_value' => !empty($config['top-title']) ? $config['top-title'] : '',
),
'top-left' => array(
'#type' => 'text_format',
'#title' => t('Top left'),
'#default_value' => !empty($config['top-left']) ? $config['top-left'] : '',
),
'top-right' => array(
'#type' => 'text_format',
'#title' => t('Top right'),
'#default_value' => !empty($config['top-right']) ? $config['top-right'] : '',
),
);
return system_settings_form($form);
}
On my .module file I have:
function contact_page_menu() {
$items = array();
$items['admin/settings/contact-page'] = array(
'title' => 'Contact Page content',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('contact_page_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => '/admin/contact_page.admin.inc',
);
return $items;
}
function contact_page_default_settings() {
$defaults = array(
'top-tile' => 'Top title',
'top-left' => 'Top left',
'top-right' => 'Top right',
);
$config = variable_get('contact_page_settings', array());
return array_merge($defaults, $config);
}
OK, so I found out the values were saving - just they were not appearing in my text areas when I reloaded the config page.
The problem was that I was using text_format fields which save data as an array. I needed to get the value property of that array as my default value.
So, I just added ['value'] to the default_value ternary expression:
'#default_value' => !empty($config['top-right']['value']) ? $config['top-right']['value'] : '',
I would like to be able to count the number of text inputs that have been filled and use the result to create a drop down with the options 0-[count returned]. I am using the
ajax_command_replace function in Drupal 7 but open to suggestions of any other method.
I have a test which does count the inputs filled and displays the result but I cannot figure out how to then use this to populate a select.
Any help much appreciated.
for the form elements.
$array = array_fill(0,5,'');
$form['test'] = array(
'#type'=> 'fieldset',
'#title' => 'TEST',
);
$form['test']['value']['#tree'] = TRUE;
foreach ($array as $key => $value) {
$form['test']['value'][$key] = array(
'#type' => 'textfield',
'#ajax' => array(
'callback' => 'test_callback',
),
);
}
$test = count(array_filter($array));
$form['test']['count'] = array(
'#suffix' => "<div id='testcount'>filled inputs = $test</div>",
);
and the ajax callback
function test_callback($form, $form_state){
$text = count(array_filter($form_state['input']['value']));
$commands = array();
$commands[] = ajax_command_replace("#testcount", "<div id='testcount'>filled inputs = $text</div>");
return array('#type' => 'ajax', '#commands' => $commands);
}
Thanks
This seems to be working for me now, same method but using the render() function to return an updated form element from within the callback. I hope it helps someone else.
$array = array_fill(0,5,'');
$form['test'] = array(
'#type'=> 'fieldset',
'#title' => 'TEST',
);
$form['test']['value']['#tree'] = TRUE;
foreach ($array as $key => $value) {
$form['test']['value'][$key] = array(
'#type' => 'textfield',
'#ajax' => array(
'callback' => 'test_callback',
),
);
}
$options = range(count(array_filter($array)),0,1);
$form['test']['count'] = array(
'#type'=> 'select',
'#options' => $options,
'#prefix' => '<div id="testcount">',
'#suffix' => "</div>",
);
Callback
function test_callback($form, $form_state){
$text = count(array_filter($form_state['input']['value']));
$default = $form_state['input']['count'];
$options = range($text,0,1);
$form['test']['count'] = array(
'#type'=> 'select',
'#options' => $options,
'#prefix' => '<div id="testcount">',
'#suffix' => "</div>",
'#default_value' => $default,
);
$commands = array();
$commands[] = ajax_command_replace("#testcount", render($form['test']['count']));
return array('#type' => 'ajax', '#commands' => $commands);
}
I have a query which I want shown as a Drupal tableselect with sorting and pagination and search term which is memorized in the $_SESSION. The problem is that the pagination is not working.
My code looks kinda like this:
//build the header row (except the checkbox)
$header = array(
'status' => array('data' => t('Status')),
'name' => array('data' => t('Product name'), 'field' => 'p.name'),
'actions' => array('data' => t('Actions'))
);
//do the query for the data
$query = db_select('my_products', 'p');
$query->addField('p', 'pid');
$query->addField('p', 'name');
$query->addField('p', 'status');
//in case there is a search term then filter the table
if(isset($_SESSION['search_term_product'])) {
$query->condition('p.name', '%' . db_like($_SESSION['search_term_product']) . '%', 'LIKE');
}
//add a pager to the resulting data and a sorter
$query->extend('PagerDefault')
->limit(10);
->extend('TableSort')
->orderByHeader($header);
$result_set = $query->execute();
//structure the data accordingly
$rows = array();
$default_value = array();
foreach($result_set as $product) {
$default_value[$product->pid] = $product->status == 1 ? TRUE : FALSE; //ticked or not tiked
$rows[$product->pid] = array(//array key is needed for form submission
'status' => $product->status == 1 ? t('Active') : t('Not active'),
'name' => array(
'data' => array(
'#type' => 'link',
'#title' => $product->name,
'#href' => $base_url . '/' . $product->name . '/edit'
)
),
'actions' => array(
'data' => array(
'#type' => 'link',
'#title' => 'Delete',
'#href' => $base_url . '/' . $product->name . '/delete'
)
)
);
}
//configure the table
$form['products'] = array
(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#empty' => t('No products found'),
'#multiple' => TRUE, //TRUE -> checkboxes FALSE-> radio buttons
'#default_value' => $default_value,
'#markup' => theme('pager')
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Change status'),
);
return $form;
I tried lots of solutions and searched the web extensively but I don't know why the pagination isn't working. This code just shows all the result like my pagination code wasn't even there.
Any suggestions?
Edit: In my original code I have a few joins.
After many many tries I managed to solve it. I think there's a problem if you add the ->extend('PagerDefault') after a join. Anyway all you have to do is change
db_select('my_products', 'p');
to
db_select('my_products','p')->extend('PagerDefault')->extend('TableSort');
$query->extend('PagerDefault')
->limit(10);
->extend('TableSort')
->orderByHeader($header);
to
$query->limit(10)
->orderByHeader($header);
delete the line with '#markup' => theme('pager') and before return $form add
$form['pager'] = array(
'#markup' => theme('pager')
);
I've a form in my module (mymodule)...
function mymodule_form()
{
$form['mytext'] = array(
'#title' => 'Password',
'#type' => 'password',
'#size' => 10,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Cool!',
);
$form['cool_submit'] = array(
'#type' => 'submit',
'#value' => 'Cool Submit!',
);
return $form;
}
I've used the hook_entity_view hook to display this form under all drupal entities that are displayed.
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$entity->content['myadditionalfield'] = mymodule_form();
}
When showing this form, drupal adds a DIV tag to the mytext (password field) by itself. I want to override this and provide my own DIV tags and theme to this form. How do I do it?
Thanks :-)
I worked further on this question to solve it. And the problem got solved. I changed the above code...
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$element = array(
'start' => array(
'#type' => 'button',
'#name' => 'start',
'#button_type' => 'submit',
),
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
),
'my_submit' => array(
'#type' => 'button',
'#name' => 'Submit Discussion',
'#button_type' => 'submit',
),
);
$entity->content['disc_bar'] = $element;
}
And the problem kept creeping up whenever a textfield or a password field was rendered. I checked the type array in systems elements info function (a elements_info hook) and found that textfield and password fields come with a default value for the #theme-wrapper. I tried to override it by this way...
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
'#theme-wrapper' => '',
),
and It worked. Its not generating any additional division tags... :-)