I'm using Maxlength to limit input in any FAPI form, not just node forms. Here's the workaround:

<?php
//
// called with drupal_get_form('mymodule_user_new_tweet');
//
function mymodule_user_new_tweet() {
  global $user;
  $field = 'tweet';
  $type  = 'user';
  $form[$field] = array(
    '#type' => 'textarea',
    )),
  );
  variable_set(MAXLENGTH_NODE_TYPE . $field .'_'. $type, 140); // limit
  variable_set(MAXLENGTH_NODE_TYPE . $field .'_js_'. $type, TRUE); // use js
  variable_set(MAXLENGTH_NODE_TYPE . $field .'_text_'. $type, '!remaining of !limit chars remaining');
  _maxlength_format_element($form[$field], '', $field, $field, $type);

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Tweet!'),
  );
  return $form;
}
?>

Ideally, this should be done via an API call or extra attributes in the FAPI element.

Comments

infojunkie’s picture

There's a typo in the $form[$field] declaration, sorry about that.

hefox’s picture

Subscribe.

(Calling variable_set like that is ugly. It invalidates the variable cache and is in general bad practice.)

hefox’s picture

Status: Active » Needs review
StatusFileSize
new14.87 KB

Here's a patch with the aim to make it damn easy for people to use it in form alter

Some notes on some of the changes

-    $list = array_keys(content_fields(NULL, $type));
+    $info = content_types($type);
+    $list = array_keys($info['fields']);
 

if content_fields is provided NULL for the first paramater, it returns all fields, even if $type is passed (common issue).

+define('MAXLENGTH_DEFAULT_TEXT', 'Content limited to !limit characters, remaining: <strong>!remaining</strong>');
+define('MAXLENGTH_DEFAULT_USE_JS', TRUE);
+define('MAXLENGTH_DEFAULT_LENGTH', 0);

Defines are nice. Everyone loves defines.

use_js feels like it would be better to default to true as that seems like the more generic case. I do not think this will effect existing sites as it's very likely they have set the variables up.

I also wanted to change max_length_properties as max_length doesn't have an underscore anywhere else and it's a hardish to remember key.

+  if (!is_array($element['#max_length_properties'])) {
+    $element['#max_length_properties'] = array(
+      'limit' => $element['#max_length_properties'],
+    );
+  }

Make it easier for them to use? Shrug, don't care if this line makes it or not.

I figured I'd test out sandboxes, so the changes are also available at a fork at http://drupal.org/sandbox/hefox/1089978

hefox’s picture

StatusFileSize
new15.51 KB

Had some bugs :(

hefox’s picture

(Duplicate comment -- clickity click click)

hefox’s picture

StatusFileSize
new15.45 KB

*sigh*

dawehner’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev
Status: Needs review » Patch (to be ported)

This patch really looks fine, added a api file and some more things.

Thanks!! this is really a helpful patch.

This has to be ported to 7.x-2.x

hefox’s picture

Status: Patch (to be ported) » Needs work
StatusFileSize
new15.13 KB

Can't get fields working without a special case (that's a hackish special case), which isn't done usually so gotta look into why that's happening, so putting this needs work.

jm.federico’s picture

Maxlength just suffered a big change.

It will now work with the code form maxlngth_js http://drupal.org/project/maxlength_js

maxlength_js byt default is an API so that any textfield can get a character counter.

Should we close this issue as won't fix?

joachim’s picture

Version: 7.x-2.x-dev » 6.x-2.x-dev
Status: Needs work » Needs review

The http://drupal.org/project/maxlength_js project takes care of the D7 branch.

But this module here is going to continue for D6, and this is still a very useful feature here.

Hence restoring the version to D6, and setting to needs review -- the patch in #6 that is.
Though #7 seems to suggest it's RTBC for D6.

joachim’s picture

Huh. Patch no longer applies to 6.x-2.x branch, and commit 82d0d487a00048af653273d07ee22ae7b171b225 looks like it adds this sort of feature.

joachim’s picture

Yup, this functionality is in the dev version. Any chance of a new beta release?

The API can be used like this:

  module_load_include('inc', 'maxlength');
  $form['pony']['#max_length_properties'] = array(
    'limit' => $limit,
    'use_js' => TRUE,
    'text' => t('Your message, where the tokens "!limit", "!remaining" and "!count" are replaced with their values.'),
  );
  

I'll file a separate issue with a docs patch :)

hefox’s picture

Status: Needs review » Fixed

Since not relavent for 7.x and fixed for 6.x marking as fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.