Drupal Table of form elements - php

I am trying to create a table in the config page for my module. I need the table to include some form elements, Checkbox and textfield. I want to structure the table like so.
Enable | Name | Int
Enable being the checkbox, name being a standard text value and int being the textfield. I want to be able to check a box next to a name, type in a value and then have it save when it submits. Currently I am doing this in the .admin.inc. I have actually been able to make the table and have the elements in it. However I cant put values in or get them out.
How I am creating the rows
foreach($rows as $row){
$form['table']['intvalue'] = array(
'#type' => 'textfield',
'#default_value' => 100,
'#size' => 8,
);
$form['table']['enable'] = array(
'#type' => 'checkbox',
'#default_value' => TRUE,
);
$rows[] = array(drupal_render($form['table']['enable']), $name, drupal_render($form['table']['intvalue']));
}
And this is how I am displaying the table
$table = theme('table', array(
'header' => $header,
'rows' => $rows,
'id' => 'table-articles',
'class' => 'articles',
));
$form['table'] = array(
'#type' => 'item',
'#title' => t('Table'),
'#markup' => $table,
'#weight' => -2,
);
Any idea?

Related

Drupal - changing form field type from radio to checkboxes

Currently I am working on a Drupal 8 project. I have to change a existing form field type from single single selection (radio) to multi selection (checkboxes).
I have changed below
$form['job_type'] = [
'#type' => 'radio',
'#title' => $this->t('I am looking for'),
'#options' => [
'Full-time' => $this->t('Full Time'),
'Part-time' => $this->t('Part Time'),
'Casual' => $this->t('Casual'),
'All-of-the-above'=>$this->t('All of the above')
],
'#weight' => '0',
'#required'=>true,
];
to
$form['job_type'] = [
'#type' => 'checkboxes',
'#title' => $this->t('I am looking for'),
'#options' => [
'Full-time' => $this->t('Full Time'),
'Part-time' => $this->t('Part Time'),
'Casual' => $this->t('Casual'),
'All-of-the-above'=>$this->t('All of the above')
],
'#weight' => '0',
'#required'=>true,
];
This is how save code looks like
$profile->set('field_job_type', $form_state->getValue('job_type'));
$profile->save();
When the form is submitted, $form_state->getValue('job_type') prints the correct selection but when I get the value using $profile->get('field_job_type')->getValue() it returns me empty array.
Any help would much appreciated !

Drupal Multiupload Filefield Widget conflict with mutiselect ajax dropdown

I have created a multiupload field in my node using Multiupload Filefield Widget. I am using the code below to alter the default node form.
There is an ajax based dependent dropdown field in the form i.e city dependent on country.When I select a single country the form work fine. But when I select multiple values form the country list I get the follwing error:
An illegal choice has been detected. Please contact the site administrator.
I get this particular error when there is the multiupload form element present. When I remove the element from $form its works fine with multiple selected values.
Please help me out to solve this problem.Thanks in advance!
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'mynode_node_form':
$country_list = load_countries();
$selected_country = (isset($form_state['values']['country'])) ? $form_state['values']['country'] : key($country_list);
$form['countries'] = array(
'#type' => 'select',
'#title' => t('Select Your Country'),
'#options' => $country_list,
'#default_value' => $selected_country ,
'#ajax' => array(
'callback' => 'city_dropdown_callback',
'wrapper' => 'city_wrapper',
),
'#multiple' => TRUE,
'#required' => TRUE,
);
$cities_list = load_cities($selected_country);
$selected_cities = (isset($form_state['values']['cities'])) ? $form_state['values']['cities'] : key($cities_list);
$form['cities'] = array(
'#type' => 'select',
'#title' => t('Select Your City'),
'#prefix' => '<div id="city_wrapper">',
'#suffix' => '</div>',
'#options' => $cities_list,
'#default_value' => $selected_cities,
'#multiple' => TRUE,
'#required' => TRUE,
);
}
}
function city_dropdown_callback($form,$form_state){
return $form['cities'];
}

Drupal pager on table with sort and select and search not working

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')
);

Drupal check boxes creation

I have the form in drupal7.
What i need is: i have category and sub category . I have to list these in checkboxes like
I used the code as
$form['rate_the_room']['talent'] = array(
'#prefix'=>'<h1>category:</h1>',
'#type' => 'checkboxes',
'#options' => $sub_tal,
);
Here $sub_tal is an array. If it is the single dimensional array it works fine. Here how to pass an array(The category , sub categories are came from db). How to maintain this structure with this code.
Please help me thanks.
Is there any particular reason why these all need to be in one form element? If your data is set up properly, you can simply loop through them and create elements in a somewhat organized fashion.
function my_test_form($form_state) {
// Assuming you have your data in a format like this
$categories = array(
'cat1' => array(
'#title' => 'category 1',
'sub1' => 'Subcat1',
'sub2' => 'Subcat2',
),
'cat2' => array(
'#title' => 'category 2',
'sub21' => 'Subcat21',
'sub22' => 'Subcat22',
),
'cat3' => 'category 3',
);
$elements = array();
foreach ($categories as $cat => $data) {
// If multidimensional, create parent and loop through children
if (is_array($data)) {
$elements[] = array(
'#type' => 'checkbox',
'#title' => $data['#title'],
'#return_value' => $cat,
);
foreach (element_children($data) as $subcat) {
$elements[] = array(
'#type' => 'checkbox',
'#title' => $data[$subcat],
'#attributes' => array(
'style' => 'margin-left: 2em',
),
'#return_value' => $cat . '-' . $subcat,
);
}
}
// Else, just create top level
else {
$elements[] = array(
'#type' => 'checkbox',
'#title' => $data,
'#return_value' => $cat,
);
}
}
// Group all of these elements together. They'll be submitted into the same array.
$form['categories'] = $elements;
$form['categories']['#tree'] = TRUE;
$form['categories']['#prefix'] = '<div style="font-weight:bold" class="label">' . t('Checkboxes title') . ':</div>';
$form['submit'] = array(
'#value' => t('Submit me'),
'#type' => 'submit',
);
return $form;
}
function my_test_form_submit($form, &$form_state) {
$submitted_values = array_filter($form_state['values']['categories']);
}
Renders as:
And when the form is submitted, assuming some checkboxes were checked, $submitted_values looks like this:
array('cat2', 'cat2-sub21', 'cat2-sub22')
You can change the values of the checkboxes submitted to the submit handler by changing the #return_value attribute that I set on each checkbox above.
The submitted values are grouped together not only because they are all within the $form['categories'] section, but also because #tree is set to TRUE.

Theming a form in Drupal which has to be a part of an Entity's Content

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... :-)

Categories