Extend to Title and CCK textfields/textareas?
Morbus Iff - April 29, 2007 - 18:26
| Project: | Maxlength |
| Version: | 5.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | a_c_m |
| Status: | postponed (maintainer needs more info) |
Jump to:
Description
Funny: I need this exact sort of feature for a site I'm working on, but need it for a bunch of CCK fields, not just the Body. It would seem relatively simple to extend this to Titles and/or CCK textfields/textareas, at the expense of a more complex administration screen.

#1
Very good suggestions.
I broke the title related one out into a separate issue, I will probably work on that first.
See: Set maxlength for title field as well.
#2
I was thinking some more about this. The easiest way to implement, and to use, seems to implement an alternate textarea widget for text field types.
An option would to create a feature request on the CCK module, but then they would have to rewrite the javascript used here. Probably it is simpler to just write a widget.
#3
i'mrealy interest for cck or sms oe all text area
#4
Adding a comment in order to subscribe. I would like to see maxlength available to CCK fields as well. Thanks!
#5
I would like to see this feature too and am subscribing to catch updates
#6
max length for cck as well
thanks
#7
+1 for cck fields
#8
Me too. Also would be nice to see it on any other field. Like ie excerpt module (custom teaser field).
#9
I need this for CCK text fields. My text fields where all textareas (rows > 1) and were not set to multiple.
Please note I am not accounting for normal text input fields or fields set to multiple. To make this really useful a checkbox should be added to the text field settings widget. I don’t know how possible that is with CCK. I have never tried to added settings to CCK field.
I could have put this in separate mini module instead of hacking maxlength.
I hacked my form_alter like this.
<?php
function maxlength_form_alter($form_id, &$form) {
if (isset($form['type'])) {
$type = $form['type']['#value'];
if ( $type . '_node_form' == $form_id) {
if (strlen(variable_get(MAXLENGTH_NODE_TYPE . $type . '_t', '')) > 0) {
$limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $type . '_t', ''));
$form['title']['#maxlength'] = $limit;
if (drupal_strlen($form['title']['#default_value']) > $limit) {
$form['title']['#default_value'] = drupal_substr($form['title']['#default_value'], 0, $limit);
drupal_set_message(
t('%title_field_label truncated to %limit characters!',
array(
'%title_field_label' => $form['title']['#title'],
'%limit' => $limit)
),
'error'
);
}
}
}
if (module_exists('content') && module_exists('text')) {
$fields = content_types($type);
foreach ($fields['fields'] as $field_id => $field) {
if (isset($field['max_length'])) {
if (is_numeric($field['max_length']) && (int)$field['max_length'] > 0) {
if ($field['widget']['rows'] > 1) {
// theme('maxlength_textarea', $form[$field_id]);
$form[$field_id][0]['value']['#theme'] = 'maxlength_textarea';
$form[$field_id][0]['value']['#maxlength_textarea'] = $field['max_length'];
}
}
}
}
}
}
}
?>
And In my theme I added this to override theme_maxlength_textarea()
<?php
function phptemplate_maxlength_textarea($element) {
$prefix = '';
if ($element['#name'] == 'body') {
$path = drupal_get_path('module', 'maxlength');
drupal_add_js($path . '/maxlength.js');
if (arg(1) == 'add') {
$type = arg(2);
}
else
{
$type = _maxlength_node_type_from_id(intval(arg(1)));
}
$limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $type . '_b', ''));
$remaining = $limit - drupal_strlen($element['#value']);
if ($remaining < 0) {
drupal_set_message(
t('%body_field_label truncated to %limit characters!',
array(
'%body_field_label' => $element['#title'],
'%limit' => $limit)
),
'error'
);
$element['#value'] = drupal_substr($element['#value'], 0, $limit);
$remaining = 0;
}
$element['#attributes']['onchange'] = 'maxlength_limit(this, ' . $limit . ');';
$element['#attributes']['onkeyup'] = 'maxlength_limit(this, ' . $limit . ');';
$prefix = t('<div id="maxlength-counter">Content limited to !limit characters, remaining: <strong id="maxlength-counter-remaining">!remaining</strong></div>', array('!limit' => $limit, '!remaining' => $remaining));
}
elseif (isset($element['#maxlength_textarea'])) {
$path = drupal_get_path('module', 'maxlength');
drupal_add_js($path . '/maxlength.js');
$limit = $element['#maxlength_textarea'];
$remaining = $limit - drupal_strlen($element['#value']);
$element['#attributes']['onchange'] = 'maxlength_limit(this, ' . $limit . ');';
$element['#attributes']['onkeyup'] = 'maxlength_limit(this, ' . $limit . ');';
$prefix = t('<div id="maxlength-counter">Content limited to !limit characters, remaining: <strong id="maxlength-counter-remaining">!remaining</strong></div>', array('!limit' => $limit, '!remaining' => $remaining));
}
return theme_textarea($element) . $prefix;
}
?>
Hope it helps.
#10
Thanks NaX, but I don't think I will go with this approach. As you mention, it still needs work, and I don't think it is the right solution.
As I mentioned in comment #2, the right solution is to implement an alternate textarea widget. This way you can select the widget only when you need it and specify the maximum length individually.
#11
I personally think the most flexible approach is to uses a similar method to the one used by the formfilter module. That will allow for Maxlength to be added to almost any text and textarea fields on any form.
#12
The problem, as I see it, is that you really don't want to apply this to every single textarea and also where you apply it you need specific configuration (you probably don't want to enforce the same max length on all textareas).
So, altering forms and appending the max length control to them is not too hard. But providing an interface where all possible forms and textareas are listed, so they can be configured individually, is not that easy. Would be something similar to what the captcha module does. It collects forms as you use the site. While this is doable, it is a total overkill imo.
#13
The captcha interface is very similar to the formfilter interface, the difference is that the formfilter module is doing 90% of what Maxlength length would need to do to implement my idea. When you enable the UI it places a link at the bottom of every form and when you click that link you are present with the same form fields just with a checkbox under each field that says hide. You then select each field you want to hide and submit the form and then that form field is hidden and converted into a forms api value. So most of the code need to implement this is already their.
Some extra coding would need to be done to integrate CCK fields max length values maybe as defaults to the interface, but this is very feasible. It would mean sort of a rewrite and it would turn a small module into a far more complex module.
Just an idea.
Thanks for a useful module.
#14
Looked at the formfilter project. You can probably go down that road, but I really think it is an overkill in this case.
If there was a CCK widget, together with the existing application to body fields of all nodes, would that solve your problem?
#15
This functionality is now available in the 2.x release, please all have a play with the beta and report back :)
#16
+1 for widget! widget FTW! :)
#17
subscribed