I'm trying to use ajax in drupal 8 custom form. But it not work.
I've custom form, in which textfield is display with some value once the user click on the checkbox. Here is my code:-
public function buildForm(array $form, FormStateInterface $form_state) {
$form['del_name'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Delete users, by name'),
'#ajax' => array(
'callback' => array($this, 'my_user_callback'),
'wrapper' => 'del-name',
),
);
$form['name_placeholder'] = array(
'#type' => 'hidden',
'#prefix' => '<div id="del-name">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (!empty($form_state->getValue('del_name')) && $form_state->getValue('del_name')) {
/*
* $process_data = some stuff
*/
$form['name_placeholder']['user_role'] = array(
'#title' => $this->t('Users role'),
'#type' => 'textfield',
'#default_value' => $process_data
);
}
}
function my_user_callback(array &$form, FormStateInterface $form_state) {
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace('#del-name', render($form['name_placeholder'])),
)
);
}
Problem is that, textfield not appear when checkbox is checked.
try this:
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
function my_user_callback(array &$form, FormStateInterface $form_state {
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand(
'#wrapper-element-id',
$array_of_form_elements
));
return $response;
);
Related
i make custom form with 2 steps, i want if user click on back button on step 2 form redirect to step 1 and show the data which the user has entered at the beginning. and this is my code
function example_form($form, &$form_state) {
if (!empty($form_state['step_num'])) {
return example_step_two($form, $form_state);
}
$form_state['step_num'] = 1;
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#required' => TRUE,
);
$form['next'] = array(
'#type' => 'submit',
'#value' => 'Next',
'#submit' => array('example_form_submit'),
);
return $form;
}
and this is my submit function
function orders_order_form_submit($form, &$form_state) {
$form_state['page_values']['1'] = $form_state['values'];
if (!empty($form_state['page_values'][2])) {
$form_state['values'] = $form_state['page_values'][2];
}
$form_state['step_number'] = 2;
$form_state['rebuild'] = TRUE;
}
and this is step two of my form
function example_form_step_two($form, &$form_state){
$form['title2'] = array(
'#type' => 'textfield',
'#title' => t('Title2'),
'#required' => TRUE,
);
$form['back'] = array(
'#type' => 'submit',
'#value' => t('Back'),
'#submit' => array('example_form_step_two_back'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#submit' => array('example_form_step_two_submit'),
);
return $form;
}
and this is the problem function for back button
function example_form_step_two_back($form, &$form_state){
$form_state['rebuild'] = TRUE;
}
Looks like you need to test if 'step_num' is 2 in example_form(), not if it's not empty:
if ($form_state['step_num'] == 2) {
return example_step_two($form, $form_state);
}
Then in example_form_step_two_back(), you need to add:
$form_state['step_num'] = 1;
To display the previously user-entered value of 'title', you have to add a '#default_value' element to it which is set if that value exists.
I'm quite new to Drupal and I want to create a custom grid that has some editable columns, some other columns witch checkboxes etc.
I'm using the theme() function to create the table and render it at the settings. Since I cannot find any way to access the form/settings variables, inside the theme function, I create a custom table at the drupal database that will contain the gid values, and I render those rows. For testing purposes I fetch the 'variables' table rows. Here is the code so far:
$form['module_settings']['profile_grid'] = array(
'#type' => 'item',
'#title' => t('Profile Mapping'),
'#theme' => 'profile_mapping_grid'
);
function theme_profile_mapping_grid($sender) {
$header = array('', t('name'), t('value'));
$result = db_query('SELECT v.name, v.value from {variable} v');
while ($pair = db_fetch_object($result)) {
$format = array(
'#type' => 'textfield',
'#size' => 30,
'#value' => $pair->name
);
$hhfield = array(
'#type' => 'textfield',
'#size' => 30,
'#value' => $pair->value
);
$row = array('');
$row[] = drupal_render($format);
$row[] = drupal_render($hhfield);
$rows[] = array('data' => $row);
}
$output = theme('table', $header, $rows, array('id' => 'gridErrors'));
return $output;
}
The grid is generated correctly, but I have an issue. I cannot set the 'name' attribute to the textfield, in order to collect it's value later on, on a submit action.
Furthermore, I'm not sure if this is the best way to create a settings grid.
Any ideas, opinions etc are more than welcome.
According to Drupal 6 Form API Quickstart Guide:
Don't use the '#value' attribute for any form element that can be changed by the user. Use the '#default_value' attribute instead. Don't put values from $form_state['values'] (or $_POST) here! FormsAPI will deal with that for you; only put the original value of the field here.
You have to use '#default_value' => $pair->name not '#value' => $pair->value.
Here is the sample module with settings table.
install file:
<?php
// $Id$
/**
* #file
* The your_module module install file, which handles the install/uninstall tasks.
*
*/
function your_module_install() {
// Create tables.
drupal_install_schema('your_module');
}
/**
* Implementation of hook_schema().
*/
function your_module_schema() {
/* Settings table */
$schema['your_module_settings'] = array(
'description' => 'Stores module settings.',
'fields' => array(
'record_id' => array(
'description' => 'The primary identifier for a record.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'item_code' => array(
'type' => 'varchar',
'length' => '255',
'not null' => TRUE),
'setting_one' => array(
'type' => 'varchar',
'length' => '255',
'not null' => TRUE),
'setting_two' => array(
'type' => 'varchar',
'length' => '255')),
'unique keys' => array(
'record_id' => array('record_id')),
'primary key' => array('record_id')
);
return $schema;
}
/**
* Implementation of hook_uninstall().
*/
function your_module_uninstall() {
// Remove tables.
drupal_uninstall_schema('your_module');
}
module file:
<?php
/**
* Implementation of hook_help()
*/
function your_module_help($path, $arg)
{
switch ($path)
{
case 'admin/help#your_module':
return '<p>'. t('Module description.') .'</p>';
}
}
/**
* Implementation of hook_menu()
*/
function your_module_menu()
{
$items = array();
/* module settings page */
$items['admin/settings/your_module'] = array(
'description' => 'Administer your_module module settings.',
'title' => 'Some title',
'page callback' => 'drupal_get_form',
'page arguments' => array('your_module_admin_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function your_module_theme() {
return array(
'your_module_products_settings' => array(
'arguments' => array('form' => NULL),
),
);
}
/**
* Settings form
*/
function your_module_admin_settings(&$form_state)
{
$form['your_module'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
'#description' => t('Settings description.'),
);
$form['your_module']['products_settings_table'] = array(
'#theme' => 'your_module_products_settings',
'#tree' => TRUE,
);
$form['your_module']['products_settings_table']['products_settings'] = array();
$result = db_query('SELECT record_id, item_code, setting_one, setting_two FROM {your_module_settings} ORDER BY item_code');
while ($product_setting = db_fetch_object($result)) {
$form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id] = array();
$form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id]['item_code'] = array(
'#type' => 'textfield',
'#default_value' => $product_setting->item_code,
'#size' => 8,
'#maxlength' => 16,
);
$form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id]['setting_one'] = array(
'#type' => 'textfield',
'#default_value' => $product_setting->setting_one,
'#size' => 16,
'#maxlength' => 16,
);
$form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id]['setting_two'] = array(
'#type' => 'textfield',
'#default_value' => $product_setting->setting_two,
'#size' => 40,
'#maxlength' => 255,
);
}
/* "add new row" fields */
$form['your_module']['products_settings_table']['products_settings_new'] = array();
/* new item_code */
$form['your_module']['products_settings_table']['products_settings_new']['item_code'] = array(
'#type' => 'textfield',
'#size' => 8,
'#maxlength' => 16,
'#description' => t('description'),
);
/* new setting_one */
$form['your_module']['products_settings_table']['products_settings_new']['setting_one'] = array(
'#type' => 'textfield',
'#size' => 16,
'#maxlength' => 16,
'#description' => t('description'),
);
/* setting_two */
$form['your_module']['products_settings_table']['products_settings_new']['setting_two'] = array(
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 255,
'#description' => t('description'),
);
/* Submit button */
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
'#name' => 'SubmitButton',
);
return $form;
}
/**
* Custom theme function for a table of products settings
*/
function theme_your_module_products_settings($form) {
$header = array(t('Item Code'), t('Setting One'), t('Setting Two'));
$rows = array();
/* saved rows */
foreach (element_children($form['products_settings']) as $key) {
$row = array();
$row[] = drupal_render($form['products_settings'][$key]['item_code']);
$row[] = drupal_render($form['products_settings'][$key]['setting_one']);
$row[] = drupal_render($form['products_settings'][$key]['setting_two']);
$rows[] = $row;
}
/* new row to add */
$row = array();
$row[] = drupal_render($form['products_settings_new']['item_code']);
$row[] = drupal_render($form['products_settings_new']['setting_one']);
$row[] = drupal_render($form['products_settings_new']['setting_two']);
$rows[] = $row;
$output = theme('table', $header, $rows);
return $output;
}
/**
* Submission function for your_module_admin_settings form.
*/
function your_module_admin_settings_submit($form, &$form_state) {
/* processing changes */
if (isset($form_state['values']['products_settings_table']['products_settings'])) {
foreach ($form_state['values']['products_settings_table']['products_settings'] as $record_id => $data) {
if (empty($data['item_code']) && empty($data['setting_one']) && empty($data['setting_two'])) {
/* delete saved row if all fields are empty */
db_query("DELETE FROM {your_module_settings} WHERE record_id=%d", $record_id);
drupal_set_message(t('Deleted.'), 'status');
}
else {
/* update */
db_query("UPDATE {your_module_settings} SET item_code='%s', setting_one='%s', setting_two='%s' WHERE record_id=%d",
$data['item_code'], $data['setting_one'], $data['setting_two'], $record_id);
}
}
}
/* adding new row */
$item_code = $form_state['values']['products_settings_table']['products_settings_new']['item_code'];
$setting_one = $form_state['values']['products_settings_table']['products_settings_new']['setting_one'];
$setting_two = $form_state['values']['products_settings_table']['products_settings_new']['setting_two'];
if (!empty($item_code) && !empty($setting_one) && !empty($setting_two)) {
db_query("INSERT INTO {your_module_settings} (item_code, setting_one, setting_two) VALUES ('%s', '%s', '%s')",
$item_code, $setting_one, $setting_two);
drupal_set_message(t('Added new setting.'), 'status');
}
drupal_set_message(t('Settings updated.'), 'status');
}
You're generating the fields in the theme function and it will not work because the submit function will only act on the fields generated by your form function.
The correct way is to generate all fields in the form function (basically what you're doing now in the theme function) and render them in the theme using drupal_render.
Since I cannot find any way to access the form/settings variables, inside the theme function
The form is passed to the theme function as an argument, in your case $sender, and that's how you access them.
Here's some code, untested, but you get the idea. Remember that the call $output .= drupal_render($form); is very important because it renders the hidden fields needed by the Form API such as the form_id and the CSRF protection. Without this call submit won't work.
function mymodule_grid_form($form_state) {
$form = array();
$form['variables'] = array();
$result = db_query('SELECT v.name, v.value from {variable} v');
while ($pair = db_fetch_object($result)) {
$form['variables'][$pair->name][$pair->name . '_name'] = array(
'#type' => 'textfield',
'#size' => 30,
'#value' => $pair->name
);
$form['variables'][$pair->name][$pair->name . '_value'] = array(
'#type' => 'textfield',
'#size' => 30,
'#value' => $pair->value
);
}
return $form;
}
function theme_mymodule_grid_form($form) {
$header = array('', t('name'), t('value'));
foreach ($form['variables'] as $variable => $values) {
$row = array('');
$row[] = drupal_render($values[$variable . '_name']);
$row[] = drupal_render($values[$variable . '_value']);
$rows[] = array('data' => $row);
}
$output = theme('table', $header, $rows, array('id' => 'gridErrors'));
$output .= drupal_render($form);
return $output;
}
function mymodule_theme() {
return array(
'mymodule_grid_form' => array(
'arguments' => array('form' => NULL),
),
);
}
i'm still working on my own drupal 7 module, and i'm having some trouble. I'm trying to load database content to dropdown option (select), i have read and write the same code from drupal example, but my database still not loading, only empty option.
what i'm asking is there anything wrong on my code or is there any faster way to load database to dropdown option other than the drupal example???
here the code i'm working on
function prod_entry_load($entry = array()) {
$select = db_select('aa_1122','aa');
$select->fields('aa');
foreach ($entry as $field => $value) {
$select->condition($field, $value);
}
return $select->execute()->fetchAll();
}
function prod(){
return drupal_get_form('prod_form');
}
function prod_form($form_state){
$form = array(
'#prefix' => '<div id="updateform">',
'#suffix' => '</div>',
);
$entries = prod_entry_load();
$keyed_entries = array();
if (empty($entries)) {
$form['no_values'] = array(
'#value' => t("No entries exist in the table dbtng_example table."),
);
return $form;
}
foreach ($entries as $entry) {
$options[$entry->no] = t("#no: #name ", array('#no' => $entry->no, '#name' => $entry->name));
$keyed_entries[$entry->no] = $entry;
}
$default_entry = !empty($form_state['values']['no']) ? $keyed_entries[$form_state['values']['no']] : $entries[0];
$form_state['entries'] = $keyed_entries;
$form['no'] = array(
'#type' => 'select',
'#title' => t('Choose'),
'#default_value' => $default_entry->no,
'#ajax' => array(
'wrapper' => 'updateform',
'callback' => 'prod_form_update_callback',
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#ajax' => array(
'callback' => 'ajax_alert',
),
);
return $form;
}
function prod_form_update_callback($form, $form_state) {
$entry = $form_state['entries'][$form_state['values']['no']];
foreach (array('name') as $item) {
$form[$item]['#value'] = $entry->$item;
}
return $form;
}
You've just forgotten to add the #options key to your select element, the code should read like this:
$form['no'] = array(
'#type' => 'select',
'#title' => t('Choose'),
'#default_value' => $default_entry->no,
'#options' => $options, // This is the bit that was missing
'#ajax' => array(
'wrapper' => 'updateform',
'callback' => 'prod_form_update_callback',
),
);
You could shorten your query/options code slightly using a combination of string concatenation in MySQL and the db fetchAllKeyed() method:
$query = db_select('aa_1122', 'aa')->fields('aa', array('no'));
$query->addExpression("CONCAT(no, ': ', name)", 'display');
$options = $query->execute()->fetchAllKeyed();
I'm building a module (my_module) in Drupal 7.
It has some functionality and also will create new content type.
In my_module.install I implemented the hook_install (my_module_install).
Can I use more one implementation of hook_install to create new content type (my_cck_install) in this module?
If (yes), how should I do this?
Else: have I do this in another module? :-)
You can't use more than one implementation of hook_install in the same module; in PHP you can't have 2 function with the same name which rules this out.
You would just need to add your new content type in the same hook_install anyway (have a look at how the standard installation profile does it at /profiles/standard/standard.install). This is how I always add new content types from the install file (using the example of a testimonials module):
function testimonial_install() {
// Make sure a testimonial content type doesn't already exist
if (!in_array('testimonial', node_type_get_names())) {
$type = array(
'type' => 'testimonial',
'name' => st('Testimonial'),
'base' => 'node_content',
'custom' => 1,
'modified' => 1,
'locked' => 0,
'title_label' => 'Customer / Client Name'
);
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
}
}
The following code will create a content type called "Event" with a machine name of 'event' and a title field -
//CREATE NEW CONTENT TYPE
function orderform_node_info() {
return array(
'event' => array(
'name' => t('Event'),
'base' => 'event',
'description' => t('A event content type'),
'has_title' => TRUE
),
);
}
function event_form($node,$form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('event Title'),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5
);
return $form;
}
//END CONTENT TYPE
you should place it in your .module file... if you want do add additional fields to it, let me know and I'll patch you up with the code... good luck!
/**
* Implements hook_node_info()
*/
function mymodule_node_info() {
return array(
'news' => array(
'name' => t('News'),
'base' => 'news',
'description' => t('You can add News here'),
'has_title' => TRUE,
'title_label' => t('News title')
)
);
}
/**
* Implement hook_form()
*/
function mymodule_form($node, $form_state) {
return node_content_form($node, $form_state);
}
Add the implementation to mymodule.install is as follows:
/**
* Implements hook_install().
*/
function mymodule_install() {
node_types_rebuild();
$types = node_type_get_types();|
node_add_body_field($types['news']);
}
You can get a detailed description with code from here
/*
* Implementation in hook node info in .Module file
*/
function test_node_info() {
return array(
'product' => array(
'name' => t('Product'),
'base' => 'product',
'description' => t('Product Title'),
)
);
}
/**
* Implement hook_form()
*/
function product_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_install() in .install file.
*/
function test_install() {
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['product']);
//New way to implement to add fields in your content type
foreach (_test_installed_fields() as $field) {
field_create_field($field);
}
foreach (_test_installed_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'product';
field_create_instance($fieldinstance);
}
}
/*
* Define your fields
*/
function _test_installed_fields() {
$t = get_t();
return array(
'product_title123' => array(
'field_name' => 'product_title123',
'label' => $t('Product Title'),
'type' => 'text'
),
'description123' => array(
'field_name' => 'description123',
'label' => $t('Description'),
'type' => 'text'
),
);
}
/*
* Define your instance of fields
*/
function _test_installed_instances() {
$t = get_t();
return array(
'product_title123' => array(
'field_name' => 'product_title123',
'type' => 'text',
'label' => $t('Product Title'),
'widget' => array(
'type' => 'text_textfield'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Product Title'),
'type' => 'text'
)
)
),
'description123' => array(
'field_name' => 'description123',
'type' => 'text',
'label' => $t('Description'),
'widget' => array(
'type' => 'text_textarea_with_summary'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Description'),
'type' => 'text'
)
)
),
);
}
/**
* Implements hook_uninstall().
*/
function test_uninstall() {
$ournewtype = 'product';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $ournewtype));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
node_type_delete($ournewtype);
}
That's it.
My main question is:
Does the theme_hook() function gets called whenever the form gets rebuilt via ahah (ahah_helper) ?
I'm trying to show a select box, with some filtering options, when the user changes it, the table below it changes too.
I have this by now:
function veiculos_listar_form($form_state)
{
$form = array();
ahah_helper_register($form, $form_state);
//biulds $options
$form['listar_veics'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="listar-veics-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (!isset($form_state['values']['listar_veics']['filial']))
$form['#filial_veic'] = 1;
else
$form['#filial_veic'] = $form_state['values']['listar_veics']['filial'];
$form['listar_veics']['filial'] = array(
'#type' => 'select',
'#title' => "Listar veículos da filial",
'#options' => $filiais,
'#default_value' => $form['#filial_veic'],
'#ahah' => array(
'event' => 'change',
'path' => ahah_helper_path(array('listar_veics')),
'wrapper' => 'listar-veics-wrapper',
'method' => 'replace',
),
);
return $form;
}
function veiculos_listar_form_submit($form, &$form_state)
{
}
function _listar_veiculos_tabela($filial)
{
//builds $header and $data
$table = theme_table($header, $data);
return $table;
}
function theme_veiculos_listar_form($form)
{
$output = drupal_render($form);
$filial = $form['#filial_veic'];
$output .= '<br>' . $filial . '<br>';
$output .= _listar_veiculos_tabela($filial);
return $output;
}
function veiculos_theme() {
return array(
'veiculos_listar_form' => array(
'arguments' => array('form' => NULL),),
);
}
In my little and innocent world, it should work if theme_hook is called on every ahah event (change).
The problem is, the variable printed is always the same, like what the user is choosing isn't being stored. If the user select a different options, it shows the new option, but the $filial variable is always the same when the theme prints.
Like this:
http://img230.imageshack.us/img230/9646/62144334.jpg
Any suggestion on what i could do to make this work? I'm developing our own module, so using views module isn't a good idea.
Thanks.
You should redo code this way.
Ahah callback I do not wrote, I think you would not have problem with it.
Check some examples on drupal.org
function veiculos_listar_form($form_state)
{
$form = array();
ahah_helper_register($form, $form_state);
//biulds $options
// remove divs because we do not want to reload selector with ahah
$form['listar_veics'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
);
if (!isset($form_state['values']['listar_veics']['filial']))
$form['#filial_veic'] = 1;
else
$form['#filial_veic'] = $form_state['values']['listar_veics']['filial'];
// add cover div here, because we will reload table
$form['table'] = array(
'#prefix' => '<div id="listar-veics-wrapper">',
'#suffix' => '</div>',
'#type' => 'markup',
'#value' => _listar_veiculos_tabela($form['#filial_veic']),
);
$form['listar_veics']['filial'] = array(
'#type' => 'select',
'#title' => "Listar veículos da filial",
'#options' => $filiais,
'#default_value' => $form['#filial_veic'],
'#ahah' => array(
'event' => 'change',
'path' => ahah_helper_path(array('listar_veics')),
'wrapper' => 'listar-veics-wrapper',
'method' => 'replace',
),
);
return $form;
}
function veiculos_listar_form_submit($form, &$form_state)
{
}
function _listar_veiculos_tabela($filial)
{
//builds $header and $data
$table = theme_table($header, $data);
return $table;
}
function theme_veiculos_listar_form($form)
{
$output = drupal_render($form);
return $output;
}
function veiculos_theme() {
return array(
'veiculos_listar_form' => array(
'arguments' => array('form' => NULL),),
);
}