Max word count
glass.dimly - June 4, 2008 - 21:10
| Project: | Maxlength |
| Version: | 6.x-2.0-beta1 |
| Component: | Miscellaneous |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs work |
Description
Great module, thanks.
I really like it, it works great.
How easy would it be to add a max word count? I have found that this feature is missing in Drupal and would greatly appreciate it.
peace
Jeremy

#1
I'm going to try and implement this for version 6.x.
#2
Attached patch adds the words count ability. Here are a few pointers:
Please review my code.
#3
mh i'm not happy that you created a patch to version1, there are quite a lot of missing features in the first version.
so develop the patch against 6-2.x
so i set it to needs work
i don't like
<?php+// case 'prepare':
+// // Truncate body field.
+// $limit = intval(variable_get($code .'_b', ''));
+// if (variable_get($code .'_bt', 'char') == 'word' && (count(explode(' ', $node->body)) > $limit)) {
+// $type_info = node_get_types('type', $node->type);
+//
?>
But i like the feature.
#4
It would be really great to see this feature in the future. Thanks for the work.
#5
This patch was not working. I got a new one coming up that implements that feature.
#6
Mostly minor.
+++ sites/all/modules/maxlength/maxlength.js 2009-09-16 23:35:23.000000000 +0300@@ -2,16 +2,33 @@
+ //alert(Drupal.t("Sorry, you reached the maximum amount of words. \n\n The last word in your text will be deleted."));
Alert is not very userfriendly. Couldn't there be a message above/below the textarea?
+++ sites/all/modules/maxlength/maxlength.js 2009-09-16 23:35:23.000000000 +0300@@ -2,16 +2,33 @@
-
+
MINOR: Whitespace
+++ sites/all/modules/maxlength/maxlength.module 2009-09-16 23:57:47.000000000 +0300@@ -199,7 +232,11 @@ function _maxlength_format_element(&$ele
+ drupal_set_message("format element limit type". $values['limittype']);
Test Output? Or not translatable :)
+++ sites/all/modules/maxlength/maxlength.module 2009-09-16 23:57:47.000000000 +0300@@ -241,12 +280,19 @@ function maxlength_nodeapi(&$node, $op,
- if (drupal_strlen($node->$field) > $limit) {
- form_set_error($field, t('The !field field has exceeded its maximum number of characters (!limit).', array('!limit' => $limit, '!field' => $field)));
+ if ($limittype == MAXLENGTH_LIMIT_CHARACTERS) {
+ if (drupal_strlen($node->$field) > $limit) {
+ form_set_error($field, t('The !field field has exceeded its maximum number of characters (!limit).', array('!limit' => $limit, '!field' => $field)));
+ }
+ } else {
+ if (str_word_count($node->$field) > $limit) {
+ form_set_error($field, t('The !field field has exceeded its maximum number of words (!limit).', array('!limit' => $limit, '!field' => $field)));
+ }
I would use here @ as placeholder, because it does the check_plain.
I'm on crack. Are you, too?
#7
I decided to build my own patch for this. This is my first time working with drupal, and also my first ever contribution to the open source community. Any feedback you could offer is appreciated.
The attached patch implements the following:
#8
+++ maxlength/maxlength.inc (revision 749)@@ -10,3 +10,7 @@
+// Types of limits
+define('LIMIT_CHAR', 'char');
+define('LIMIT_WORD', 'word');
defines should have the modulename as prefix
+++ maxlength/maxlength.module (revision 749)@@ -242,14 +255,18 @@
+ form_set_error($field, t('The !field field has exceeded its maximum number of words (!limit).', array('!limit' => $limit, '!field' => $field)));
@ is better than !. Yes this is a problem in the module everywhere :)
+++ maxlength/maxlength.module (revision 749)
@@ -277,8 +294,14 @@
+function _maxlength_count_words($value) {
+ if (drupal_strlen(trim($value)) == 0) { // required as preg_split() on empty string returns 1-element array
+ return 0;
+ }
+
+ return count(preg_split('/\s+/', trim($value)));
+}
Comments would be cool
I'm on crack. Are you, too?
#9
In the meantime, the CCK Word Count module provides this functionality. It works for CCK Textfields and Textareas, but not for the node body.