Posted by colson3 on August 11, 2008 at 4:46pm
Jump to:
| Project: | Webform |
| Version: | 6.x-2.1.3 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (duplicate) |
Issue Summary
I have questions on my form that end in a questions mark, however it seems to automatically add a colon at the end of every label, so each of my questions ends in "?:"
How do I stop it from adding the colon?
Thank you for your help.
Comments
#1
Paste this in your template.php in 'sites/all/themes/your_theme/' replacing 'theme' with the title of your theme folder.
function theme_form_element($element, $value) {
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '<div class="form-item"';
if (!empty($element['#id'])) {
$output .= ' id="'. $element['#id'] .'-wrapper"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">*</span>' : '';
if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
else {
$output .= ' <label>'. $t('!title !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
}
$output .= " $value\n";
if (!empty($element['#description'])) {
$output .= ' <div class="description">'. $element['#description'] ."</div>\n";
}
$output .= "</div>\n";
return $output;
}
Original code can be found in includes/form.inc
it looks like this
function theme_form_element($element, $value) {
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '<div class="form-item"';
if (!empty($element['#id'])) {
$output .= ' id="'. $element['#id'] .'-wrapper"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">*</span>' : '';
if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
else {
$output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
}
$output .= " $value\n";
if (!empty($element['#description'])) {
$output .= ' <div class="description">'. $element['#description'] ."</div>\n";
}
$output .= "</div>\n";
return $output;
}
This will be a site wide change affecting all forms the same way. I just add ':' when I need to. Works out well for me. I've only tried this on 5.x but this is code from 6.x and should work the same way. Hope this helps.
#2
I tried this it creates an error tells me that the function has already been declared in includes/form.inc and cannot be redeclared in template.php
#3
To follow this suggestion for making a site wide change then you would need to make the change to the includes/form.inc file itself. Just remove the colon after !title: in the theme_form_element() method (2 places).
#4
I think that you probably just didn't change the function name to replace 'theme' with your theme's name. Hacking core is never a good idea.
#5
Sorry, flavor
If I wasn't clear in the beginning. The error you received telling you that that function has already been called is because both functions are titled the same. So to be more specific you must replace the 'theme' part of theme_form_element with the title of the folder in which your active theme is kept. So if your theme folder is titled "nancy' then your function in your template.php file should look like this "nancy_form_element". If you wanted all active themes to use this function then 'theme' should be replaced by 'phptemplate' (instead of 'nancy' in this example) and I believe the template.php file should be outside the individual theme folders but inside the sites theme folder. Glancing at the Drupal handbook on theming should clarify that. Of course it's also functional to paste this in each theme's template file also, just more to manage that way.
Hope this helps
#6
Thanks, by the way.
I'd like to figure out how to apply this only to forms generated by the webforms module but it is a huge start.
#7
This method does not work for me either, to remove colons from the field labels in webforms
I hacked the includes/form.inc code to remove the colons, removing the colons from $t('!title: to make them $t('!title in function theme_form_element lines 2202 and 2205, ( http://drupal.org/node/376345 ) but of course that was lost upgrading to core 6.11
so I devised a DHTML method - paste this script into the webform body
<script type="text/javascript">function nocolons(){
doclabs = new Array;
doclabs = document.getElementsByTagName('label');
if (doclabs.length) {
for (i=0; i<doclabs.length; i++ ){
labtext = doclabs[i].innerHTML;
if (labtext.indexOf(":")) {
striptext = labtext.substr(0, labtext.indexOf(":")) + labtext.substr(labtext.indexOf(":")+1, labtext.length);
doclabs[i].innerHTML = striptext;
}
}
}
}
onload=nocolons;
</script>
I suppose this snippet could also go in a theme file, probably page.tpl.php
I only have one webform, so it is more efficient for me to put the code in the webform body
#8
#201941: How to remove punctuation from the end of a form field label?
#9
#201941: How to remove punctuation from the end of a form field label?
#10
sorry, I'm not sure what these last two posts mean
this is not a duplicate of #201941, although I posted there first about hacking the form.inc file
I pointed to the DHTML code I posted here from that thread, as this thread is more on the topic of my issue
#11
This was super helpful. I dropped it into the body and it works great. Thank you.
#12
For those where this is not working, don't forget to flush your cache!
#13
I only want to get rid of the colon that goes along with "Username:" and "Password:" in the login field not make a site wide change.
#14
Here's a javascript solution that only removes the colon if the label contains a question mark. (#7 removes all colons).
<script type="text/javascript">function nocolons(){
doclabs = new Array;
doclabs = document.getElementsByTagName('label');
if (doclabs.length) {
for (i=0; i<doclabs.length; i++ ){
labtext = doclabs[i].innerHTML;
if (labtext.indexOf("?") > 0) {
doclabs[i].innerHTML = labtext.replace(":","");
}
}
}
}
onload=nocolons;
</script>
#15
Thank you to cooperwd in #14. Works great.
If you have trouble getting it to work make sure to check your input filter settings. I had to set mine to none for the script to work properly.
#16
re: filters
enabling the PHP filter for admins then setting input format to PHP filter default usually works
@#14 nice use of replace function, but I would test for presence of ":" rather than "?" before invoking it
#17
Thanks decibel.places! But I'm confused. The module appends a colon to the end of every label, so why test for the presence of a colon? Instead, we want to test for the presence of a question mark -- only then do we want to remove the colon. Maybe I'm misunderstanding?
#18
@#17 - you're absolutely correct, cooperwd
testing for "?" is necessary to remove the colon after question mark
although we can "assume" that your module adds a colon always, I still think it's good practice to test for it too to prevent unexpected problems - I've seen assumptions break many routines
#19
The fix in #1 works as of Drupal 6.14, thank you!
#20
If you want to remove colon on specific label, here is jQuery code (example for two text field):
if (Drupal.jsEnabled) {
$(document).ready(function()
{
var lab1 = $('#edit-first-name-wrapper > label');
lab1.html(lab1.html().replace(":", ""));
var lab2 = $('#edit-second-name-wrapper > label');
lab2.html(lab2.html().replace(":", ""));
});
}
Just set $('#edit-....-wrapper > label') to appropriate id of textfield.
#21
Tracking.
#22
The script from sakiland applied on all label in a form :
if (Drupal.jsEnabled) {$(document).ready(function()
{
var lab1 = $('#myform-id label');
lab1.each(function() { $(this).html($(this).html().replace(":", "")); });
});
}
#23
I used this snippet in #7 in my webform and it worked perfectly except for one problem: i have a label without text in it (value = ). this was the main reason i was looking into this issue in the first place. this method removes all colons except for the label without any text in it, which just appears as a colon with nothing else next to it. how can i fix this?
#24
@awm6392 #23
I suspect that #7 has a problem with blank labels and length
try #22 which does not rely on length. It assumes you have the jQuery library loaded, which is added in a Drupal installation in the misc directory.
#25
#22 works great - thanks birdy! Don't forget to replace "#myform-id" with your own
#26
Here's my non-JavaScript solution to remove the colon only from webform fields. This code goes in your template.php file and you need to replace "mythemename" with your theme name in the function declaration. It works for me in Drupal 6.11
/**
* Return a themed form element.
*
* @param element
* An associative array containing the properties of the element.
* Properties used: title, description, id, required
* @param $value
* The form element's data.
* @return
* A string representing the form element.
*
* @ingroup themeable
*/
function mythemename_form_element($element, $value) {
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '<div class="form-item"';
if (!empty($element['#id'])) {
$output .= ' id="'. $element['#id'] .'-wrapper"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">*</span>' : '';
if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
if(!isset($element['#webform_validated'])){
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
else{
//removed colon
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
}
else {
$output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
}
$output .= " $value\n";
if (!empty($element['#description'])) {
$output .= ' <div class="description">'. $element['#description'] ."</div>\n";
}
$output .= "</div>\n";
return $output;
}
#27
Hello :
I solved using this jquery code:
$(document).ready(function() {$('#node-38 label').each(
function() {
var myText = $(this);
myText.text( myText.text().replace(':','') );
}
);
});
Regards,
Luis.
#28
Solution #1 worked great (using Drupal 6), thanks! And yes, as mentioned above, don't forget to do two things:
1) In the first line, replace the function name: "mytheme_form_element($element, $value)", with your theme's name like this: "mytheme_form_element($element, $value)"
2) flush your cache
Greatly appreciated,
Ben
P.S. The JQuery solutions looked good as well, except for one thing - don't they strip out colons that are supposed to be there, too?
#29
@flamingvan thanks for the code
#30
I landed here when looking to remove colons, it seems there is now a module for this
http://drupal.org/project/no_colons
#31
Nice, Very usefull posting thanks so much for share this.i used this and its working well.once again thanks so much.
#32
thanks all, this post really helped :)
i tried #1 (read #28) and #14 both worked