I can't get my buttons to index properly - drupal

I'm having some issues with form tabindexing. I have two submit buttons in my form, and the one I want to be indexed first isn't. This is currently what I have on my buttons:
$form['prev']['#attributes']['tabindex'] = '0'
$form['next']['#attributes']['tabindex'] = '1'
On this live version it shows up in the attributes they show up on both buttons.
I'm using Drupal 7 and creating my own custom form module.

$form['company'] = array(
'#title' => t('Company'),
'#type' => 'textfield',
'#required' => TRUE,
'#attributes' => array('tabindex' => '6'),
);
this is how you set your tab index programatically.

Related

Drupal: how do I retrieve the select type values from a module Config form?

I'm new to Drupal and am trying to build a module. Part of what this module does is allow you to add preset classes from a drop down field.
For the most part I've got this working but for one thing: I seem to only be able to retrieve the select options name, not it's value.
The code I have is below.
In the config form creation function I have:
$styles = array(
'None' => '',
'Blue Buttons' => 'btn blue-btn',
'Red Buttons' => 'btn red-btn',
);
$mymodule_form['style'] = array(
'#type' => 'select',
'#required' => TRUE,
'#title' => t('Style'),
'#description' => t('Style for buttons'),
'#default_value' => $form_values['style'],
'#empty_option' => t('- Select -'),
'#options' => drupal_map_assoc(array_keys($styles)),
);
But, when I run dpm($this->options['style']); later on in my code when I want to use those styles, I get the key names return (eg Button Red)
Would anyone know how I can retrieve the values?
I was using drupal_map_assoc when I didn't need to.
'#options' => $styles,
is fine.
(Also, I had my key names and values around the wrong way).

managed_file form element doesn't render correctly

I have a simple managed file form element that is the only element in my form. It looks like this:
$form['upload'] = array(
'#type' => 'managed_file',
'#title' => t('Select a YML file'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select a file to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('yml txt docx')),
'#upload_location' => $upload_dest,
);
When I render the form using the drupal_get_form callback in hook_menu I get a perfectly formed managed_file upload field with the browse and upload buttons. Things change when I decide I want to add a table of information underneath the form. This requires building a table using theme functions then adding that to the form by rendering the form and appending the table. I create my table and add it to the form:
$rows = array();
foreach($yml_files as $yml_file){
$rows[] = array($yml_file->uri, $yml_file->filename);
}
$output = drupal_render($form['upload']);
$output .= theme('table', array('header'=>$header, 'rows'=>$rows));
return $output;
When I generate the form using drupal_render, I get the nice help text, but no upload form. The table renders fine in both scenarios and I'm not seeing any errors.
If Drupal uses drupal_render to render its forms why would the form look different in the second scnenario? Is there a way to get the entire form? I've tried a variety of ways of passing the form and using dpm to print the form at various stages and I'm not sure where to go from here.
Standard file upload fields render correctly as do other form elements. It seems to be limited to the managed_file element.
When using a drupal_get_form menu callback, you should return your $form array and not an already rendered themeable array. Probably you are missing #attached js files for the managed_file field.
What you could do in your case is to add the table output on a markup field of your form.
$form['upload'] = array(
'#type' => 'managed_file',
'#title' => t('Select a YML file'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select a file to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('yml txt docx')),
'#upload_location' => $upload_dest,
);
$form['table'] = array(
'#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
);
return $form;

How to add content to user profile page in Drupal 7?

I'm trying to add a couple of sections onto the user profile in Drupal 7 under:
<div class="profile" typeof="sioc:UserAccount" about="/drupal/user/1">
I'm adding three new sections, but the problem is that, although I am using the same way to add the three sections, only one of them is rendered as a child of the div above, while the other two are rendered as siblings. What am I doing wrong?
This is how I'm creating the content:
function plan_user_user_view($account) {
//Create the markup for the events region
$account->content['events'] = array(
'#type' => 'user_profile_item',
'#theme' => 'events',
'#events' => $events);
//Create the region for the venues
$account->content['venues'] = array(
'#type' => 'user_profile_item',
'#theme' =>'venues',
'#userid' => $user->uid,
'#venues' => $venues);
//Create the region for creating an event
$account->content['creator'] = array(
'#prefix' => '<div class="user-event-item" id="quick-event-creator">',
'#suffix' => '</div>',
'#type' => 'user_profile_item',
'#title' => t('QUICK EVENT CREATOR'),
'#markup' => drupal_render(drupal_get_form('event_creation')));
}
Also, is there a better way to create that last piece of content there? The other two seem fine in a template file but the last one since it's a form I was wondering if there are better ways of doing that.
Thanks,
Maybe you should have a look on this project profil2 which is the successor of content_profil for Drupal 6. With it you will be able to add informations onto users profils and if you want to code your custom fields by yourself it should be a good starting point to read.
Best.
How about this:
// Create the category.
$account->content['mymodule'] = array(
'#type' => 'user_profile_category',
'#title' => t('My module content'),
);
// Create first item (child).
$account->content['mymodule']['events'] = array(
'#type' => 'user_profile_item',
'#title' => t('Events'),
'#markup' => t('Whatever'),
);
// Create second item (child).
$account->content['mymodule']['venues'] = array(
'#type' => 'user_profile_item',
'#title' => t('Venues'),
'#markup' => t('Whatever'),
);
and so on. The bottom line is that user_profile_item items should be children of a user_profile_category item.

WYSIWYG editor in texarea for Drupal configuration form

Is it possible to use a WYSIWYG editor in texarea
for Drupal site configuration form (system_settings_form).
This is how the configuration is coded now...
$form['my_module_text_bottom'] = array(
'#type' => 'textarea',
'#title' => t('Some text'),
'#default_value' => variable_get('my_module_text_bottom', 'This is configurable text found in the module configuration.'),
'#size' => 1024,
'#maxlength' => 1024,
'#description' => t("Some text."),
'#required' => TRUE,
);
return system_settings_form($form);
Here it is for Drupal 7 and Drupal 6.
For D7:
<?php
// Retrieve the default values for 'value' and 'format', if not readily
// available through other means:
$defaults = array(
'value' => '',
'format' => filter_default_format(),
);
$my_richtext_field = variable_get('my_richtext_field', $defaults);
// Just construct a regular #type 'text_format' form element:
$form['my_richtext_field'] = array(
'#type' => 'text_format',
'#title' => t('My richtext field'),
'#default_value' => $my_richtext_field['value'],
'#format' => $my_richtext_field['format'],
);
?>
For D6:
<?php
// Your saved or new data is supposed to have a value and a format. Just like
// $node has a $node->body and $node->format. May also come from a
// variable_get('mymodule_admin_setting', array('value' => '', 'format' => NULL));
$mydata = mymodule_data_load();
$form['myfield']['mytextarea'] = array(
'#type' => 'textarea',
'#title' => t('My textarea'),
'#default_value' => $mydata->value,
);
$form['myfield']['format'] = filter_form($mydata->format);
?>
I kept searching for this issue for about 6 hours and finally i found the reason, for your custom textarea field you must add this line, to use the default input format (Full HTML):
$form['format'] = filter_form();
be careful if you use this form element inside fieldset you must include this fieldset:
$form['donation-instructions']['format'] = filter_form();
I hope this will help you
The WYSIWYG or CKEditor modules should be able to do this.
I found this question similar to:
Drupal 6: Implement Wysiwyg on Custom Module Form
One of the answers there pointed to this drupal.org page:
http://drupal.org/node/358316
which provides fairly detailed examples of the "format" array key and filter_form(), also describing how it's used if your form has multiple textareas.
The approach given there doesn't apply to Drupal 7.
I ran into a similar situation where I'd downloaded and installed and installed CKEditor and it displayed when editing content nodes, but didn't display for the textarea on a configuration form for my module.

drupal form textfield #default_value not working

I am working on a custom module with multi-page form on drupal 6.
I found that #default_value is not working when my '#type' => 'textfield'.
However, when '#type'=>'textarea', it displays correctly with the '#default_value' specified.
Basically, I wrote a FormFactory to return different form definition ($form) based on the post parameter received. Initially, it returns the display of directories list, user then selects from radio buttos until a specific directory contains a xml file, it will become edit form. The edit form will have text fields display the data (#default_value) inside the xml file, however the type 'textarea' works here rather than 'textfield'.
How can I make my '#default_value' work in this case?
Below is the non-working field definition:
$form['pageset']['newsTitle'] = array(
'#type' => 'textfield',
'#title' => 'News Title',
'#default_value' => "{$element->newsTitle}",
'#rows' => 1,
'#required' => TRUE,
);
Then I changed it to textarea as shown below to make it work:
$form['pageset']['newsTitle'] = array(
'#type' => 'textarea',
'#title' => 'News Title',
'#default_value' => "{$element->newsTitle}",
'#rows' => 1,
'#required' => TRUE,
);
There should be no difference between a textfield and a textarea form element concerning the usage of the '#default_value' attribute, and both work for me as advertised. So if it does not work in your case, you should check for typos or other differences that might cause the faulty behavior.
Could you edit your question and add your form definition code?
What Drupal versions are you on? I'm on 6.16 and I'm having weird behaviour for defaults too. In my case, not working for textareas.
I'm a non-english speaker and my default_value had non ascii characters. It's fixed now using translation.

Resources