Get selected value of a dropdown list - drupal

I try to get the selected value of my select in Drupal (7), but the value of the select is always empty in my hook_submit() :| !
Below my code :
<?php
function gestionvideos_players_form() {
//I get my list of players from my database
$aPlayers = EasyVod_db::get_players();
$options = array();
if( empty($aPlayers) ) {
$options[] = "no available player";
}else{
foreach( $aPlayers as $player ){
$options[$player->iPlayer] = ucfirst($player->sName);
}
}
$form['gestionvideos_player'] = array(
'#type' => 'fieldset',
'#title' => t('Integration par defaut des videos'),
'#description' => t('Selection du player par defaut : '),
);
$form['gestionvideos_player']['selectplayer'] = array(
'#type' => 'select',
'#options' => $options,
);
$form['gestionvideos_player']['submit'] = array(
'#type' => 'submit',
'#value' => t('Choisir ce player'),
);
return $form;
}
function gestionvideos_players_form_submit($form, &$form_state){
drupal_set_message("test ".$form_state['values']['selectplayer']);
//I set my player in my session variable
$oPlayer = EasyVod_db::get_player( intval($form_state['values']['selectplayer']) );
$_SESSION['player'] = $oPlayer ->player;
}
?>
I would really appreciate some help because I really don't understand what it doesn't work...

Have you tried putting
$form['#tree'] = true;
inside your form builder function then try dumping:
$form_state['values']['gestionvideos_player']['selectplayer']

Related

Drupal 7 Hook_forms not working

Can anyone tell me why this is not working?
The drupal_render(drupal_get_form) is dynamically created in a foreach loop and put into a table theme.
Everything loads except the form fields. I've tried debugging by adding echos and exits to each form function call, but the page continues to load. I am not sure if these functions are simply not being called or if there is some other issue.
foreach( $w as $k => $v ) {
$r[] = array(
'$'.number_format($v->amount, 2),
date('F d, Y', $v->created),
filter_xss($v->paypal_email),
drupal_render(drupal_get_form(('toefl_tutors_admin_withdrawl_request_form_'.$v->id), $v->id))
);
}
function toefl_tutors_admin_withdrawl_request_forms($form_id, $args) {
$forms = array();
if (!empty($args) && $form_id == 'toefl_tutors_admin_withdrawl_request_form_' . $args[0]) {
$forms[$form_id] = array(
'callback' => 'toefl_tutors_admin_withdrawl_request_form',
'callback arguments' => array($args[0]),
);
}
return $forms;
}
function toefl_tutors_admin_withdrawl_request_form($form, &$form_state, $id = 0) {
$form['twid'] = array(
'#type' => 'hidden',
'#value' => $id
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send Money'),
'#attributes' => array('class' => array('btn', 'btn-success'))
);
return $form;
}
I've solved the problem.
I needed to rename the hook_forms function to toefl_tutors_forms() because My module name is actually toefl_tutors not toefl_tutors_admin_withdrawl_request
Apparently and correct me if I am wrong, in order to use hook_forms you must name it mymodulename_forms, not mymodulename_xx_forms.
What confused me was hook_form works perfectly when you name the form function mymodulename_xx_form().

Content filter does not search drupal7

I have added a drop down of my courses title at the filter second of the drupal - admin - content area.
http://www.XYZ.com/demo/admin/content
But when I select any of the title and hit Filter nothing gets appear. The data which showed previously it shows again, nothing happens really.
My code for the add filter drop down:
function products_form_node_admin_content_alter(&$form, &$form_state){
$results = db_query("SELECT r.nid, r.title FROM {node} AS n
LEFT JOIN {node_revision} AS r ON r.nid = n.nid
WHERE type = 'product'")->fetchAll();
$optionsF = Array ( '[any]' => 'any' );
foreach($results as $key => $result) {
$options[$result->title] = $result->title;
}
$options = $optionsF + $options;
$course_titles['title'] = Array
(
'#type' => 'select',
'#options' => $options,
'#title' => 'title',
'#default_value' => 'any'
);
$form['filter']['filters']['status']['filters'] = $form['filter']['filters']['status']['filters'] + $course_titles;
$uid_column = array('uniqueid' => array(
'data' => 'UniqueID',
'field' => 'n.nid'
));
$form['admin']['nodes']['#header'] = $form['admin']['nodes']['#header']+$uid_column;
foreach ($form['admin']['nodes']['#options'] as $key => $row) {
$node = node_load(array('nid' => check_plain($key)));
$form['admin']['nodes']['#options'][$key]['uniqueid'] = $node->field_unique_code_course['und'][0]['value'];
}
}
Does any body have any idea what is lacking in my code or method?
Cheers!!!
I would recommend using Admin Views, it's built for this purpose.
https://drupal.org/project/admin_views

Problems with Forms => Variables are not accepted in the form

Not sure where the problem is in the following form used in a template file in Drupal7. Help is highly appreciated. The problems are the following:
1. The variables $title and $surname are not passed over to the form's default value.
=> Error Message: Notice: Undefined variable: title in form_user_information()
=> Error Message: Notice: Undefined variable: surname in form_user_information()
2. There's a Warning: strpos() expects parameter 1 to be string, array given in drupal_strip_dangerous_protocols()
Thanks in advance.
<?php
//Load User data:
global $user;
$uid = $user->uid;
$account = user_load($uid);
//Get User data:
$title = 'Mrs.';
print $title . '<br><br>'; //Result: Value is printed and not empty!
$surname = check_plain($account->field_vorname['und']['0']['value']);
//$surname = 'Tom';
print $surname . '<br><br>'; //Result: Both values are printed and are not empty!
function form_user_information($form, &$form_state) {
//Form
$form['#action'][] = request_uri();
$form['#id'][] = 'form_user_information';
$form['#validate'][] = 'form_user_information_validators';
$form['#submit'][] = 'form_user_information_submit';
$form['#prefix'] = '<div id="form_user_information">';
$form['#suffix'] = '</div>';
//Select-Field: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#select
$form['Title'] = array(
'#type' => 'select',
'#title' => t('Title'),
'#options' => array(
'Frau' => t('Mr.'),
'Herr' => t('Mrs.'),
),
'#default_value' => $title,
);
$form['surname'] = array(
'#type' => 'textfield',
'#maxlength' => 50,
'#size' => 40,
'#required' => TRUE,
'#title' => t('Surname'),
//'#attributes' => array('placeholder' => $surname),
'#default_value' => $surname,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Confirm data');
return $form;
}
//print form
$form = drupal_get_form('form_user_information');
print drupal_render($form);
//Form Validation:
function form_user_information_validators($form, &$form_state) {
if ($form_state['values']['surname'] == '') {
form_set_error('surname', t('Please enter your surname.'));
}
}
//Form Submit:
function form_user_information_submit($form, &$form_state) {
//...
}
//get form information
echo "<pre>".print_r($form,true)."</pre>";
?>
1) Set the global variables $title and $surname with global scope:
//Get User data:
$global $title = ...
$global $surname = ...
Otherwise set all these variables (including $user) inside the function form_user_information which is the best practice.
I would also suggest not use $title as variable name because it may cause problems with already defined $title of the $page variable. Instead use something like $user_title.
2) Which line does this come from?
Guess this issue can be closed. See the 2 comments. Thanks Theodoros for your help.

Drupal 7 get dropdown list selected value

I have the following code that creates a dropdown list from my database :
foreach( $aPlayers as $player ){
if( $_SESSION['player'] == $player->iPlayer ){
$playerSelected = $player->iPlayer;
}
$key = $player->iPlayer;
$value = ucfirst($player->sName);
$playersList[$key] = $value;
}
$form['gestionvideos_player']['selectplayer'] = array(
'#type' => 'select',
'#options' => $playersList,
'#default_value' => $playerSelected,
);
But then when I try to submit my form and get user's selected value with :
function gestionvideos_players_form_submit($form, &$form_state){
echo $form_state['values']['selectplayer'];
}
The return value is not the one selected.
Do you have any idea to help me?
Thank you :)

Style WordPress tables?

This is my WordPress table. I created an array so that I could try it out, but I need to add classes and IDs so I can use CSS to style it like the top level plugin page.
How can I add classes to the table elements?
<?php
if(!class_exists('WP_List_Table')){
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class TT_Example_List_Table extends WP_List_Table {
var $example_data = array(
array(
'ID' => 1,
'title' => '300',
'rating' => 'R',
'director' => 'Zach Snyder'
),
array(
'ID' => 2,
'title' => 'Eyes Wide Shut',
'rating' => 'R',
'director' => 'Stanley Kubrick'
),
array(
'ID' => 3,
'title' => 'Moulin Rouge!',
'rating' => 'PG-13',
'director' => 'Baz Luhrman'
),
array(
'ID' => 4,
'title' => 'Snow White',
'rating' => 'G',
'director' => 'Walt Disney'
),
array(
'ID' => 5,
'title' => 'Super 8',
'rating' => 'PG-13',
'director' => 'JJ Abrams'
),
array(
'ID' => 6,
'title' => 'The Fountain',
'rating' => 'PG-13',
'director' => 'Darren Aronofsky'
),
array(
'ID' => 7,
'title' => 'Watchmen',
'rating' => 'R',
'director' => 'Zach Snyder'
)
);
function __construct(){
global $status, $page;
//Set parent defaults
parent::__construct( array(
'singular' => 'movie', //singular name of the listed records
'plural' => 'movies', //plural name of the listed records
'ajax' => false //does this table support ajax?
) );
}
function column_default($item, $column_name){
switch($column_name){
case 'rating':
case 'director':
return $item[$column_name] . 'hi';
default:
return print_r($item,true) . ' hi'; //Show the whole array for troubleshooting purposes
}
}
function column_title($item){
//Build row actions
$actions = array(
'edit' => sprintf('Edit',$_REQUEST['page'],'edit',$item['ID']),
'delete' => sprintf('Delete',$_REQUEST['page'],'delete',$item['ID']),
);
//Return the title contents
return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
/*$1%s*/ $item['title'],
/*$2%s*/ $item['ID'],
/*$3%s*/ $this->row_actions($actions)
);
}
function column_cb($item){
return sprintf(
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
/*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie")
/*$2%s*/ $item['ID'] //The value of the checkbox should be the record's id
);
}
function get_columns(){
$columns = array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'title' => 'Title',
'rating' => 'Rating',
'director' => 'Director'
);
return $columns;
}
function get_sortable_columns() {
$sortable_columns = array(
'title' => array('title',true), //true means its already sorted
'rating' => array('rating',false),
'director' => array('director',false)
);
return $sortable_columns;
}
function get_bulk_actions() {
$actions = array(
'delete' => 'Delete'
);
return $actions;
}
function process_bulk_action() {
//Detect when a bulk action is being triggered...
if( 'delete'===$this->current_action() ) {
wp_die('Items deleted (or they would be if we had items to delete)!');
}
}
function prepare_items() {
$per_page = 5;
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->process_bulk_action();
$data = $this->example_data;
function usort_reorder($a,$b){
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //If no sort, default to title
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
$result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
}
usort($data, 'usort_reorder');
$current_page = $this->get_pagenum();
$total_items = count($data);
$data = array_slice($data,(($current_page-1)*$per_page),$per_page);
$this->items = $data;
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
) );
}
}
function tt_add_menu_items(){
add_menu_page('Example Plugin List Table', 'List Table Example', 'activate_plugins', 'tt_list_test', 'tt_render_list_page');
} add_action('admin_menu', 'tt_add_menu_items');
function tt_render_list_page(){
$testListTable = new TT_Example_List_Table();
$testListTable->prepare_items();
?>
<div class="wrap">
<div id="icon-users" class="icon32"><br/></div>
<h2>List Table Test</h2>
<form id="movies-filter" method="get">
<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
<?php $testListTable->display() ?>
</form>
</div>
<?php
}
The only way to do this is by overriding some of the methods of the WP_List_Table class.
I went ahead and modified your class to support conditional HTML classes for each tr/td in the table. You weren't clear enough about to which elements you want classes applied, nor how exactly you want to specify that, so excuse me if it's not what you wanted(and please specify further details).
You can see the full code(only the TT_Example_List_Table class is there - the rest is the same) here.
Basically you define a class property called $cond_classes. This property is a multidimensional array of conditions. In there you have two top-level keys which are reserved - "odd" and "even". As you can guess they will be accessed for each row that is either odd or even.
The rest of the top-level keys can be column id's or item ID's.
Each top-level key can hold either an array or a string
If the top-level key holds a string, then when this condition is met, that class is added
If the top-level key holds an array, then it's looped through.
The second-level array can have string values and key=>value pairs, where the key is the class and the value
is an array of conditions.
I guess that's quite confusing, but the example bellow should give you an idea of how this works.
var $cond_classes = array(
'odd' => array(
'odd-class', // This class will always be given to odd rows and their columns
'special-odd-class' => array( // This class will only be given to odd rows and their columns if the rows is for an item with ID 1, 4 or 7
'ID' => array( 1, 4, 7 )
)
),
'even' => array(
'even-class'
),
'title' => array(
'custom_title_class',
'special_title_class' => array(
'ID' => array( 3, 7 ), // This will only be given to the "title" column for an item with ID 3 or 7
'title' => 'The Fountain', // This will only be given to the "title" column for an item with title "The Fountain"
),
),
7 => 'id_7_class', // This will be given to a row and it's columns for item with ID 7
);
And you can see the applied classes in the resulting table:
Hope that helps! If you have any questions - go ahead :)

Resources