role_aware_maxlength
Enforces different character limits on text fields based on the current user's roles. Limits are configured per field instance and enforced via a typed data constraint on the server side. A live character counter is provided via a Stimulus.js controller.
Features
The core maxlength field property enforces a single hard limit for all users. This module extends that concept by allowing site builders to define different limits per role on any string or text field. The highest limit
across all of a user's roles is applied (see resolution rules below).
Common use case: a microblogging platform where standard users can post up to 500 characters and journalists can post up to 1,200 characters.
Post-Installation
1. Open the Manage form display page for your field
Navigate to the Manage form display page for the entity type and bundle
containing the field you want to limit (e.g.
/admin/structure/comment/manage/chirp/form-display).
2. Configure the field widget settings
Click the gear icon next to the field widget and expand the
**Role-aware character limits** section.
3. Attach the constraint
The typed data constraint must be explicitly attached to the field instance.
This can be done in code:
<?php
$fieldConfig = FieldConfig::loadByName('comment', 'chirp', 'comment_body');
$fieldConfig->addConstraint('RoleAwareMaxlength');
$fieldConfig->save();
?>
Or via an update hook or hook_install() in your custom module.
- Note: Future versions of this module may attach the constraint
- automatically when limits are configured. For now, explicit attachment
- is required.
4. (Optional) Use the provided widget
Switch the field widget to Textarea (role-aware maxlength) to get the
live character counter and the correct `maxlength` attribute automatically.
For CKEditor fields, the counter is rendered via the hook_field_widget_third_party_settings_form() integration — see the project README.md.
Additional Requirements
- Drupal ^11.3
- drupal:field
- drupa:user
Supporting this Module
This module is supported by Lisa Harrison (lisa.rae), an individual contributor and long-time member of the Drupal community.