Drupal 7 Custom Field Widget not saving user input - drupal

I'm building a custom field module to work with our streaming video provider. As part of this, I need to grab a video based on a selected category from the provider's API. The field will need to store a number of pieces - the selected category, the selected video, and some additional details (such as a caption) yet to be added.
I can get this form to appear on the node edit page (after attaching the field to a content type), but it doesn't appear to be saving the user's input. Here's the form in place on the edit node screen: Field on node editing screen. Workflow for the user is they select a category, which populates the list of videos to select. When they select a video, they're shown a preview in $element['teacherstv']['teacherstv_video_details']. This is working right up to the point where the user saves the form. At that point, the selected values are lost.
I've looked through the Field API and Forms API docs for D7, but can't see any clear instructions for how to set up this kind of field widget and have it save user input.
Here's the code for hook_field_widget_form()
function teacherstv_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// $value = $isset($items[$delta['teacherstv']) ? $items[$delta]['teacherstv'] : '';
$widget['#delta'] = $delta;
//lets get an array of our categories from core
$coreapi = new coreapi();
$APIsettings = variable_get('teacherstv_API_settings');
$coreapi->apiuser = $APIsettings['username'];
$coreapi->apipwd = $APIsettings['password'];
$coreapi->apiurl = $APIsettings['api_url'];
$coreapi->sortcriteria = "alpha";
//$categoriesavailable = $coreapi->get_categories();
$categories = coreapi_get_categories();
$defaults = $field['settings'];
$settings = $instance['settings'];
$category = !is_null($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_category']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_category'] : $defaults['teacherstv']['defaultcategory'];
switch ($instance['widget']['type']) {
case 'teacherstv':
$element['teacherstv'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => t('TeachersTV Video'),
'#description' => '<p>' . t('Select a video from the TeachersTV service to embed.') . '</p>',
'#delta' => $delta,
);
$element['teacherstv']['teacherstv_category'] = array(
'#type' => 'select',
'#field_parents' => 'teacherstv',
'#title' => t('Video Categories'),
'#options' => $categories,
'#description' => t('Select a video category for a list of videos'),
'#default_value' => !is_null($settings['teacherstv_category']) ? array($settings['teacherstv_category']) : array($category),
'#ajax' => array(
'callback' => 'teacherstv_ajax_videolist',
'wrapper' => 'teacherstv-videolist-' . $delta . '-div',
'method' => 'replace',
),
'#delta' => $delta,
);
$videos = coreapi_list_videos($category);
$videos[0] = "--Please select a video--";
asort($videos);
$element['teacherstv']['teacherstv_video'] = array(
'#type' => 'select',
'#title' => t('Select a video'),
'#field_parents' => 'teacherstv',
'#prefix' => '<div id="teacherstv-videolist-' . $delta . '-div">',
'#suffix' => '</div>',
'#options' => array(0 => 'Video 1'),
'#default_value' =>
isset($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_video']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_video'] : NULL,
'#description' => t('Select a video.'),
'#options' => $videos,
'#ajax' => array(
'callback' => 'teacherstv_ajax_videoselect',
'wrapper' => 'teacherstv-videodetails-' . $delta . '-div',
),
'#delta' => $delta,
);
$video_keys = array_keys($videos);
$selected_video = isset($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv_category']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv_category'] : NULL;
$element['teacherstv']['teacherstv_video_details'] = array(
'#type' => 'markup',
'#field_parents' => 'teacherstv',
'#title' => t('Video details'),
'#prefix' => '<div id="teacherstv-videodetails-' . $delta . '-div">',
'#suffix' => '</div>',
'#description' => t('Details about the video.'),
'#markup' => teacherstv_ajax_render_video($selected_video),
'#delta' => $delta,
);
break;
}
return $element;
}

Its probably the AJAX callback , in your AJAX callback, use
$form_state['rebuild'] = TRUE;
or assign the ajax value to a new element to have it in the $form_state array..
use devel and dpm($form_state). I bet your value is ""

Related

Drupal form submission

I have created a form in drupal. I don't know how to handle the submission. I want to do some selection from database with the values i get from form. Here is my code to create form
function q_search_form() {
$form['qsearch']['category'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Automotive', 2 => 'Real Estate'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<table width="470"><tr><td width="170">Select Category</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['city'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Calicut', 2 => 'Kochi'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">City</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['property'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'House', 2 => 'Land'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Property</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['wanto'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Sell', 2 => 'Buy'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Want to</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#attributes' => array('id' => 'Search', 'class' => 'srch-button'),
'#prefix' => '<tr><td><a class="adv-srch" href="#">Advance Search</a></td><td>',
'#suffix' => '</td></tr></table>'
);
return $form;
}
You can write following submit function,
function q_search_form_submit($form, &$form_state) {
// To get selected values from form
$values = $form_state['values'];
// do print_r($values); exit; to check the values
}
This is pretty simple. You just have to implement the form_submit function. From the function above, I assume the name of your module to be q_search. So, here is what the submit function would look like:
function q_search_form_submit($form, &$form_state) {
// you can get the values submitted by the users using
// `$form_state['values']`, and use the database functions
// to implement your logic.
}
If you also want to validate the user inputs before the actual submit, you should add a validation function which would look like:
function q_search_form_validate($form, &$form_state) {
// validate here
}
You can use form_set_error, in the validate function if the validation fails.
You can do pretty much anything you want in the form_submit function.
function q_search_form_submit($form, &$form_state) {
$category = $form_state['values']['category'];
$city = $form_state['values']['city'];
//etc..
//use these values to query your database tables
$query = db_select($category, 'c');
//narrow results to those only in the selected city:
$query->join($city, 'cy', 'c.city = cy.cid');
//narrow results further with more ->join statements
$results = $query
->fields('c', array(fields you want from the categories table))
//fields from other tables...
->execute();
//do whatever with the $results (print on a different page, print in a table, etc).

I want to place 2 forms in a single custom page in Drupal 7

I am completely new to Drupal am trying to create a single page that contains 2 forms (that come from a custom module). I also want this page to be a dedicated file in my theme directory.
In the custom module called "vocabt" I have vocabt.module that contains 3 needed functions:
function vocabt_menu() {
$items['scores/admin'] = array(
'title' => 'Check Your Student\'s Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_admin_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['scores/student'] = array(
'title' => 'Check My Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_student_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
}
function vocabt_admin_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} else {
$values = array();
}
$schools = vocabt_get_school_options();
$form['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_option' => 'Select',
);
$form['username'] = array(
'#type' => 'textfield',
'#title' => 'User Name',
'#required' => TRUE,
);
$form['password'] = array(
'#type' => 'password',
'#title' => 'Password',
'#required' => TRUE,
);
$form['actions'] = array();
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Log In',
'#attributes' => array('class' => array('button blue')),
);
return $form;
}
function vocabt_student_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} elseif(isset($_GET['id']) && $info = vocabt_decode_id($_GET['id'])) {
$values = $info;
} else {
$values = array();
}
$form['left'] = array(
'#prefix' => '<div class="lookup">',
'#suffix' => '</div>',
);
$form['right'] = array(
'#prefix' => '<div class="results">',
'#suffix' => '</div>',
);
$schools = vocabt_get_school_options();
$form['left']['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_value' => 'Select',
);
$form['left']['id'] = array(
'#type' => 'textfield',
'#title' => 'Enter your student ID',
'#required' => TRUE,
'#default_value' => !empty($values['id']) ? $values['id'] : '',
);
$form['left']['submit'] = array(
'#type' => 'submit',
'#value' => 'Check',
'#attributes' => array('class' => array('button blue')),
);
if(!empty($form_state['storage']['error'])) {
$results = '<div class="blackbox">';
$results .= '<div style="padding: 20px 0px; text-align: center;">'.$form_state['storage']['error'].'</div>';
$results .= '</div>';
} elseif(empty($values['id'])) {
$results = '<div class="blackbox">';
$results .= '<div class="instructions">Your score will appear here once you’ve entered your information on the left.</div>';
$results .= '</div>';
} else {
$results = vocabt_get_student_results($values['school'], $values['id']);
}
$form['right']['results'] = array('#markup' => $results);
return $form;
}
My question is I want to hit a URL such as "/scores/admin" that NOT ONLY contains the form within the function of "vocabt_admin_login_form()" but ALSO contains the form within the above function "vocabt_student_login_form()" ....how can I do this?
I also want both forms to appear in page that uses a dedicated PHP file (I can already create custom pages using dedicated PHP files via creating a new page in the CMS), but I do not know how to tie this dedicated PHP file with the 2 forms above. Please let me know! Thank you
It's not clear what you mean by "Dedicated PHP files". In Drupal, you define menu router items using hook_menu(), and then return their data in the specific page callback function. This function can be in any .module file or any included file in normal PHP scope.
Add a new item in your hook_menu() implementation like this:
$items['scores/check'] = array(
'title' => 'Check Scores',
'page callback' => 'vocabt_scores_page', // Note this is the PAGE CALLBACK!
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
)
Now, lets create the page callback function, vocabt_scores_page, to return a render array so Drupal will build both forms in this page - not just the specified one.
function vocabt_scores_page() {
$output = array();
$output['admin'] = drupal_get_form('vocabt_admin_login_form');
$output['student'] = drupal_get_form('vocabt_student_login_form');
return $output;
}
Make sure you clear your site's caches before testing the code. Once saved, go to scores/check page and you will see both forms in one page!
You can move your page callback functions to a different file from the .module file. To do so, add 'files' => 'vocabt.pages.inc' to the menu item and move the functions to a new file named vocabt.pages.inc. Drupal will assume the file is in same folder as the .modulefile. This file name can be anything but we mostly use module.pages.inc, module.admin.inc, likewise.
You could call a template page using the hook_theme function. You could place your html contents in this page.And just call the theme('page_name', $link);.Place your newly created theme file in your corresponding module.
function modulename_theme() {
return array(
'page_name' => array(
'template' => 'page_name',
'arguments' => array('link' => NULL),
),
);
}
You could find more information here
http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7
Hope this helps you... :)

D6: drupal_render in form causes various problems (default value, ID, date_select)

I have a problem with drupal_render (assuming that drupal_render is the right way for me to get what I want - feel free to correct me =).
I am building a form. Since the FAPI does not provide a "table"-field, I want to make one myself. My approach: use the theme()-function, specifically theme('table', ...) or theme_table(), and fill it with the respective form fields (with the intention of adding AHAH functionality later on). This forces me to use drupal_render as the value for the table cells, which causes some problems with the form elements.
The table collects numbers of employees by year, for the organisation the user is editing at this moment. The code looks as follows:
$form['employees'] = array(
'#type' => 'fieldset',
'#title' => t('Employees'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$employee_query = db_query("SELECT * FROM {employees} WHERE id_organisation = %d", $org['idoOrganisation']);
$employee = array();
while ($row = db_fetch_array($employee_query)) {
$employee[] = $row;
}
$header = array(
t('Year'),
t('Total'),
t('Internal'),
t('External'),
t('Aerospace')
);
$em_delta = 0;
$rows = array();
foreach($employee as $em_delta => $value) {
$form['employees'][$em_delta]['year'] = array(
'#title' => '',
'#type' => 'date_select', // Comes with the date module
'#date_format' => $format_year,
'#date_label_position' => 'within',
'#date_year_range' => '-50:+3',
'#default_value' => $value[$em_delta]['year'],
'#id' => 'edit-employees-' . $em_delta . '-year', // Allready a quickfix, since the form is rendered without id
'#name' => 'employees['.$em_delta.'][year]', // Same here
);
$form['employees'][$em_delta]['total'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value['total'],,
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-total',
'#name' => 'employees['.$em_delta.'][total]'
);
$form['employees'][$em_delta]['internal'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['internal'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-internal',
'#name' => 'employees['.$em_delta.'][internal]',
);
$form['employees'][$em_delta]['external'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['external'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-external',
'#name' => 'employees['.$em_delta.'][external]',
);
$form['employees'][$em_delta]['aero'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['aero'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-aero',
'#name' => 'employees['.$em_delta.'][aero]',
);
$rows[] = array(
drupal_render($form['employees'][$em_delta]['year']),
drupal_render($form['employees'][$em_delta]['total']),
drupal_render($form['employees'][$em_delta]['internal']),
drupal_render($form['employees'][$em_delta]['external']),
drupal_render($form['employees'][$em_delta]['aero']),
);
}
$form['employees']['table'] = array (
'#value' => theme('table', $header, $rows, array(), NULL)
);
Here are the problems I am encountering:
ID- and Name-Attributes of the form elements are empty. I found something on this on the drupal site and have made my peace with it (although I don't understand it), setting those attributes manually now.
Default-values of the text fields are ignored. The fields are empty. When I let drupal_get_form render the field, the default_value shows. Someone around here suggested to set the #value-property instead, but then again I read that this is something completly different and may cause problems.
The date_select-Field is not rendered in it's entirety. The wrappers are there, the select field however appears outside of the code, just before the table (i.e. where it appears in the code).
Let's hope that's it =)
Can anybody help? What am I doing wrong?
A colleague of mine pointed out that using drupal_render within the form function is not event remotely close to being a good idea, as it removes part of the form from the whole process of validating and submitting.
Thus, figuring out why the function does not work as intended is futile. The better approach would be to simply generate the necessary amount of form fields, let them be rendered as they are within drupal_get_form(), and use the forms theme-function later on to put them into a table.
Stupid me =)

Drupal 7, how to dynamically populate a field based on the summation of two other fields?

I have two fields
$form["field_num_males"]
$form["field_num_females"]
I need to dynamically populate the field $form["field_gender_total"] with the summation of them, before the submission (AJAX).
How can this be done with Drupal 7?
Thanks!
Yes it can be done with Ajax. The following code will automatic update the total when the text field is updated:
function gender_total_menu()
{
$items = array();
$items['test'] = array(
'title' => Gender total,
'page callback' => 'drupal_get_form',
'page arguments' => array('gender_total_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function gender_total_form($form, &$form_state)
{
$total = 0;
if (array_key_exists('values', $form_state) &&
array_key_exists('field_num_males', $form_state['values']) &&
array_key_exists('field_num_females', $form_state['values'])
) {
$total = $form_state['values']['field_num_males'] + $form_state['values']['field_num_females'];
}
$form = array();
$form["field_num_males"] = array(
'#type' => 'textfield',
'#title' => t("Number of males"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form["field_num_females"] = array(
'#type' => 'textfield',
'#title' => t("Number of females"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form['total'] = array(
'#markup' => '<p> Total: ' . $total . '</p>',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_update_callback($form, $form_state)
{
return $form['total'];
}
Try the Computed Field module
Computed Field is a very powerful CCK field module that lets you add a
custom "computed fields" to your content types. These computed fields
are populated with values that you define via PHP code. You may draw
on anything available to Drupal, including other fields...

Why is the drupal 6 status message not showing what fields are required when I first submit the form

When I submit the form without filling in one of the required fields( or any combination of required fields) there is no status message presented to let me know I have missed the required fields.
The second time I submit the form the status message shows me what fields are required.
The status message seems to be one step behind the form submission.
If after the first submit I change what fields are filled in and I submit again then the status message that should have show the previous time will show now.
When the form is filled in correctly it submits as normal.
The form is displayed using drupal_get_form( 'otherWaysToRequest' );
This is called in a template file in the theme.
Does anyone know why the status message is one step behind?
This is a sample of the code being used
function otherWaysToRequest(&$form_state)
{
global $base_url;
$pathToTheme = path_to_theme();
$form['top-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
);
$form['top-check']['gift'] = array(
'#title' => t('Included a gift'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['top-check']['contact'] = array(
'#title' => t('I would like to speak to you'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => true,
);
$form['email'] = array(
'#title' => t('Email Address'),
'#type' => 'textfield',
'#required' => true,
);
$form['bottom-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
'#description' => t('<p class="Items">If you have ...:</p><p class="Items">I have included .....</p>')
);
$form['bottom-check']['share'] = array(
'#title' => t('A Share'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => FALSE,
);
$form['submit'] = array(
'#type' => 'image_button',
'#src' => $pathToTheme.'/image.gif',
'#value' => t('Submit Form'),
);
}
function otherWaysToRequest_validate($form, &$form_state)
{
$mail_reg_ex = '/[-a-zA-Z0-9._]+[#]{1}[-a-zA-Z0-9.]+[.]{1}[a-zA-Z]{2,4}/';
if(!preg_match($mail_reg_ex, $form_state['values']['email']))
{
form_set_error('email', t('Invalid email address.'));
}
if( 0 == $form_state['values']['gift'] & 0 == $form_state['values']['contact'] )
{
form_set_error('gift', t('You must choose one of the first two options on the form'));
}
}
function otherWaysToRequest_submit($form, &$form_state)
{
//mail details
}
It's because by the time you're calling drupal_get_form in your template file the messages have already been committed for the current page; your validation messages will therefore show up the next time messages are displayed to the screen which is on the next page load.
You should build up the form in a custom module rather than the theme to get around this. The easiest way would be to create a block which you can assign to a region (using either hook_block in Drupal 6 or a combination of hook_block_info() and hook_block_view in Drupal 7).

Resources