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

alonpeer - May 27, 2009 - 15:56
Version:5.x-1.0» 6.x-2.x-dev

I'm going to try and implement this for version 6.x.

#2

alonpeer - May 27, 2009 - 17:20
Title:Max word count?» Max word count
Status:active» needs review

Attached patch adds the words count ability. Here are a few pointers:

  • After patching, go to maxlength configuration page, where you can select for each title & body field which type of limit you want it to have - character or word.
  • My patch doesn't implement a JS auto-count like in the character count.
  • I wasn't able to automatically truncate the number of words after form submission. Personally I don't like this anyhow, but if you want to try and help with this, feel free.

Please review my code.

AttachmentSize
maxlength_max_words.patch 10.78 KB

#3

dereine - June 6, 2009 - 16:17
Status:needs review» needs work

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

fletch11 - August 30, 2009 - 07:36

It would be really great to see this feature in the future. Thanks for the work.

#5

batje - September 18, 2009 - 17:52
Version:6.x-2.x-dev» 6.x-2.0-beta1
Status:needs work» needs review

This patch was not working. I got a new one coming up that implements that feature.

AttachmentSize
maxlength_wordcount.patch 9.65 KB

#6

dereine - September 19, 2009 - 05:43
Status:needs review» needs work

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

timbit - October 8, 2009 - 18:25

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:

  • max word count
  • javascript support for displaying the remaining number of words
  • minor code fixes/cleanup
AttachmentSize
maxlength_wordcount.patch 14.88 KB

#8

dereine - October 8, 2009 - 20:07

+++ 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

andrewmacpherson - October 11, 2009 - 01:35

In the meantime, the CCK Word Count module provides this functionality. It works for CCK Textfields and Textareas, but not for the node body.

 
 

Drupal is a registered trademark of Dries Buytaert.