Posted by jmcoder on November 4, 2009 at 12:39pm
| Project: | Maxlength |
| Version: | 6.x-2.0-beta2 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | a_c_m |
| Status: | closed (fixed) |
Issue Summary
I have noted that when there are linebreaks (return), the counter allows the user to continue till max is reached ... seems fine ... but on submission, it fails since the counter apparently hasn't been counting the linebreaks. This is likely to cause confusion in some cases.
Comments
#1
subscribe, here is the same issue.
#2
Same issue.
#3
I think it's due to the fact that Drupal check for number of character after converting the field content to filtered HTML. Line breaks all become
<br />so the count is screwed up.Just a guess but I'm in the same situation here...
#4
I just tested this and can confirm it's failing.
#3 your close, but not 100% right. The carriage return is stored as 2 or more chars (http://en.wikipedia.org/wiki/Newline)
Patch now in 6.x-2.x to fix this (http://drupal.org/cvs?commit=312470)
#5
Automatically closed -- issue fixed for 2 weeks with no activity.
#6
Just a remark: we need to do the same validation at the _maxlength_format_element function (line 175) too otherwise the module will not work properly at the edit page.
Example: if I have an enabled countdown at the node body (max length: 250) and if I fill out 240 from it than push 4 times the return and i fill out the remaining characters the module will return an error message (the body truncated to 250 charater) and cut down the end of the text.
#7
I see what you mean.
Only thing to do is count newlines at 2chars at the countdown level - but that will confuse users.
Suggestions and patches on how to fix this are welcome.
#8
Hi
My solution was - I think - simple:
start: line 175
<?php
function _maxlength_format_element(&$element, $value = '', $field, $id, $type = '') {
$values = _maxlength_get_values($field, $type);
if ($values !== FALSE AND isset($values['limit']) AND $values['limit'] AND $values['use_js']) {
$path = drupal_get_path('module', 'maxlength');
drupal_add_js($path .'/maxlength.js');
$remaining = $values['limit'] - drupal_strlen($value);
if ($remaining < 0) {
drupal_set_message(
t('%body_field_label truncated to %limit characters!',
array(
'%body_field_label' => $element['#title'],
'%limit' => $values['limit'])
),
'error'
);
$element['#default_value'] = drupal_substr($element['#default_value'], 0, $values['limit']);
$remaining = 0;
}
$js_settings = array(
'maxlength' => array(
'edit-'. $id => $values['limit'],
),
);
drupal_add_js($js_settings, 'setting');
$element['#suffix'] = '<div id="maxlength-'. $id .'"
class="maxlength-counter">'. t($values['text'], array('!limit' => $values['limit'], '!remaining' => '<span class="maxlength-counter-remaining">'. $remaining .'</span>')) .'</div>';
}
}
?>
And i put your code there
<?php
function _maxlength_format_element(&$element, $value = '', $field, $id, $type = '') {
$values = _maxlength_get_values($field, $type);
if ($values !== FALSE AND isset($values['limit']) AND $values['limit'] AND $values['use_js']) {
$path = drupal_get_path('module', 'maxlength');
drupal_add_js($path .'/maxlength.js');
// line breaks can be stored 2 or more chars, breaking the count.
$text = $value;
$text = str_replace("\r\n", '#', $text);
$text = str_replace("\n", '#', $text);
$text = str_replace("\r", '#', $text);
$remaining = $values['limit'] - drupal_strlen($text);
if ($remaining < 0) {
drupal_set_message(
t('%body_field_label truncated to %limit characters!',
array(
'%body_field_label' => $element['#title'],
'%limit' => $values['limit'])
),
'error'
);
$element['#default_value'] = drupal_substr($element['#default_value'], 0, $values['limit']);
$remaining = 0;
}
$js_settings = array(
'maxlength' => array(
'edit-'. $id => $values['limit'],
),
);
drupal_add_js($js_settings, 'setting');
$element['#suffix'] = '<div id="maxlength-'. $id .'"
class="maxlength-counter">'. t($values['text'], array('!limit' => $values['limit'], '!remaining' => '<span class="maxlength-counter-remaining">'. $remaining .'</span>')) .'</div>';
}
}
?>
Actually I really don't know how to create a patch... otherwise I would create one for my http://drupal.org/node/643828 issue :)
#9
Looks good, making patches is a worth while learning investment !
It helps module contributors apply your work - i know it may feel like a pain, but i, or any other busy module maintainer, are unlikely to get the time to review the improvements unless they are in an easy to test/implement format.
There is a lot of documentation around on the subject.
http://drupal.org/patch/create
#10
aww... oh well... sooner or later I must to learn it to be sure.
I think I will have some time for this at afternoon
#11
sorry guys... I was really lazy ^^ Patch will arive at the weekend ^^
#12
a_c_m I'd like to thank you for your work on this module and for keeping up on this issue. Maxlength is a very, very important module for sites that are picky about formatting, I am finding myself installing it standard & usually finding some reason to implement it, even in rather simple sites. Good job a_c_m.
#13
Based on lavjaman #8 I changed maxlenght.module file, but doesn't work as I hope.
Probably because I'm using the new beta1 version.
However, I had the same problem of "not counting linebreaks properly".
I added some lines in the function maxlength_nodeapi(), close to line 240:
BEFORE:
<?php$form = $a3;
if (drupal_strlen($node->$field) > $limit) {
?>
AFTER:
<?php
$form = $a3;
$text = $node->$field;
$text = str_replace("\r\n", '#', $text);
$text = str_replace("\n", '#', $text);
$text = str_replace("\r", '#', $text);
if (drupal_strlen($text) > $limit) {
?>
That counts linebreaks right for me. This solution is based on lavjaman #8, obviously.
BTW, it saves content right.
Hope that helps someone.
#14
Looks good, making patches is a worth while learning investment !
It helps module contributors apply your work - i know it may feel like a pain, but i, or any other busy module maintainer, are unlikely to get the time to review the improvements unless they are in an easy to test/implement format.
There is a lot of documentation around on the subject.
http://drupal.org/patch/create
#15
I copied the code from the dev version to get the above lines of code working but still the behavior of maxlength is a little bit strange - linebreaks are not counted correctly so that the user is told that a few character are still remaining but if the node is saved an error message appears.
#16
Hi all... I'm using Beta version right now...
I had made some quick modifications to the module file in order to:
- Count linebreaks correctly
- Work with #after_build (count characters after the form validation fails)
For me it's working, that's why I'm uploading my module file...
BTW, it works for node & title, I'm not using CCK. I tested CCK fields but it didn't work.
Hope this helps.
PD: Don't forget to delete the 'txt' extension.
#17
it seems that something like #16 is already committed to the CVS.
#18
There is support now for CCK fields: http://drupal.org/cvs?commit=501066
The thing is that i added a separate configuration field on the field edit page, so it's possible to set maxlength limit and cck max_length limit separately! So it's possible to set CCK's limit to 0 and maxlength's limit to something else, so maxlength is able to validate the length in a more sophisticated way. It's a bit not user friendly, but i had no better idea to "override" CCK's validation on the length. Any thoughts?
#19
Tested that last commit, it works as expected. This problem seems to be more of a CCK issue, if this module count linebreaks as 2 characters...
#20
Automatically closed -- issue fixed for 2 weeks with no activity.