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
Comment #1
infojunkieThere's a typo in the
$form[$field]declaration, sorry about that.Comment #2
hefox commentedSubscribe.
(Calling variable_set like that is ugly. It invalidates the variable cache and is in general bad practice.)
Comment #3
hefox commentedHere'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
if content_fields is provided NULL for the first paramater, it returns all fields, even if $type is passed (common issue).
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.
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
Comment #4
hefox commentedHad some bugs :(
Comment #5
hefox commented(Duplicate comment -- clickity click click)
Comment #6
hefox commented*sigh*
Comment #7
dawehnerThis 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
Comment #8
hefox commentedCan'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.
Comment #9
jm.federico commentedMaxlength 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?
Comment #10
joachim commentedThe 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.
Comment #11
joachim commentedHuh. Patch no longer applies to 6.x-2.x branch, and commit 82d0d487a00048af653273d07ee22ae7b171b225 looks like it adds this sort of feature.
Comment #12
joachim commentedYup, this functionality is in the dev version. Any chance of a new beta release?
The API can be used like this:
I'll file a separate issue with a docs patch :)
Comment #13
hefox commentedSince not relavent for 7.x and fixed for 6.x marking as fixed