MaxLength
Allows site administrators to set hard or soft character limits on titles, text fields, and link fields with real-time character counting.
maxlength
Install
composer require 'drupal/maxlength:^3.1'
composer require 'drupal/maxlength:^2.1'
Overview
MaxLength is a comprehensive character limiting module for Drupal that enables site administrators to set character limits on various text input fields. It provides real-time character counting with customizable countdown messages displayed beneath the input fields.
The module supports two types of limits: soft limits that display a warning when exceeded (showing negative counts) and hard limits that prevent users from typing beyond the specified character count. It intelligently handles HTML content by counting only visible text characters while preserving HTML tag structure during truncation.
MaxLength integrates seamlessly with Drupal's field widget system, allowing limits to be configured on a per-field basis through the Manage form display interface. It supports standard text fields, textareas, text with summary fields (with separate limits for content and summary), link fields, and CKEditor 5 WYSIWYG editor. The module also includes accessibility features with screen reader announcements at key character count milestones.
Features
- Real-time character counting with customizable countdown messages using @limit, @remaining, and @count placeholders
- Soft limit mode that displays warning when character count is exceeded, showing negative remaining count
- Hard limit mode that prevents users from typing beyond the specified character limit
- HTML-aware character counting that only counts visible text, ignoring HTML tags and properly handling HTML entities
- HTML-safe truncation that preserves tag structure when enforcing hard limits
- Separate character limits for content and summary fields on text_textarea_with_summary widgets
- Full CKEditor 5 integration with automatic detection and character counting within the WYSIWYG editor
- Support for link field title text with automatic enforcement of the 255 character maximum
- Accessibility support with screen reader announcements at 10 characters remaining, 0 characters remaining, and when limit is exceeded
- Extensible architecture allowing other modules to add MaxLength support to custom widgets via hook_maxlength_widget_settings()
- Per-field configuration through Drupal's standard Manage form display interface
Use Cases
Limiting Article Title Length for SEO
Set a 60-character limit on article titles to ensure they display properly in search engine results. Navigate to Structure > Content Types > Article > Manage form display, configure the title widget's MaxLength settings with a 60-character limit and enable hard limit to prevent authors from exceeding this.
Social Media Post Preparation
Create a text field for social media excerpts with a 280-character limit (Twitter/X character limit). Use soft limit mode to warn authors when they exceed the limit while still allowing them to see how much they've exceeded by.
Summary Field Management
For body fields with summary, set separate limits such as 160 characters for summary (ideal for meta descriptions) and 5000 characters for the main body. The countdown messages help authors write concise summaries.
Link Text Standardization
Limit link field titles to 50 characters to ensure consistent display in navigation menus or call-to-action buttons. Enable hard limit to enforce this strictly.
Custom Widget Integration
If your site uses a custom field widget, implement hook_maxlength_widget_settings() in your module to enable MaxLength support. Return an array mapping your widget ID to the settings it should support (maxlength_setting, summary_maxlength_setting, truncate_setting).
Tips
- Use soft limits during content migration to identify content that exceeds limits without blocking the import
- The default countdown message 'Content limited to @limit characters, remaining: <strong>@remaining</strong>' can be customized per field to provide context-specific guidance
- For multilingual sites, character limits may need adjustment as translations often vary in length
- The hard limit mode intelligently handles HTML, truncating at the character limit while properly closing any open HTML tags
- Screen reader users receive announcements at 10 characters remaining, 0 characters remaining, and when the limit is exceeded for accessibility
Technical Details
Hooks 5
hook_maxlength_widget_settings
Allows modules to define additional widget types that should support MaxLength settings. Use this hook to add MaxLength functionality to custom field widgets.
hook_element_info_alter
MaxLength uses this hook to add pre_render and process callbacks to textfield, textarea, and text_format form elements for attaching the character counter functionality.
hook_field_widget_third_party_settings_form
MaxLength implements this hook to add its configuration form to supported field widgets on the Manage form display page.
hook_field_widget_settings_summary_alter
MaxLength implements this hook to display configured limits in the widget settings summary on the Manage form display page.
hook_field_widget_single_element_form_alter
MaxLength implements this hook to apply the configured character limit settings to field widget form elements at render time.
Troubleshooting 5
Ensure the MaxLength settings are configured in the Manage form display for that specific field. Check that a maximum length value is set (not empty). Verify JavaScript is enabled and no JS errors appear in the browser console.
MaxLength counts visible text only, stripping HTML tags. HTML entities are counted as single characters. This is intentional behavior to reflect the actual content length users will see.
There may be a slight delay (400ms) for CKEditor 5 initialization. If issues persist, check that CKEditor 5 module is enabled. The module uses MutationObserver to handle text format changes.
Custom widgets require explicit support via hook_maxlength_widget_settings(). Implement this hook in your module to register your widget with the appropriate settings.
Use @limit for maximum characters, @remaining for remaining characters, and @count for current character count in your custom message. Ensure you include the @ symbol before each placeholder name.