Download & Extend

Create API for non-node forms

Project:Maxlength
Version:6.x-2.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

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

#1

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

#2

Subscribe.

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

#3

Status:active» needs review

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

<?php
-    $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).

<?php
+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.

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

AttachmentSize
984794_max_length_api.patch 14.87 KB

#4

Had some bugs :(

AttachmentSize
984794_max_length_api_4.patch 15.51 KB

#5

(Duplicate comment -- clickity click click)

#6

*sigh*

AttachmentSize
984794_max_length_api_5.patch 15.45 KB

#7

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

#8

Status:patch (to be ported)» needs work

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.

AttachmentSize
984794_maxlength_7_x.patch 15.13 KB

#9

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?

#10

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.

#11

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

#12

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 :)

#13

Status:needs review» fixed

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

#14

Status:fixed» closed (fixed)

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

nobody click here