MaxLength Words extends the MaxLength module so a field limit can be counted in words instead of characters.

It adds a "Count" option (Characters / Words) to the MaxLength widget settings. With "Words", a live word counter replaces MaxLength's character counter (same markup and styles, screen reader announcements), and with "Hard limit" the limit is trimmed while typing and enforced on submit, including CKEditor 5 fields.

Similar projects: MaxLength counts characters only; this module reuses its settings, messages, and styles instead of replacing them.

Project link

https://www.drupal.org/project/maxlength_words

Comments

daniel.matcau created an issue. See original summary.

vishal.kadam’s picture

Issue summary: View changes
avpaderno’s picture

Thank you for applying!

Before giving links helpful to understand how the review process works, what to expect from a review, and what to do to avoid a review takes more time than needed, I would like to thank all the reviewers for the work they do.
These applications are volunters-driven, which also means it is not possible to predict when an application will be marked fixed and the applicant will get the permission to opt projects into security advisory policy. While we aim to make an application as quick as possible, it is also important for us that more people review the project used for an application. In this way, we make sure applications do not miss some important points that should be instead reported.
Applications are not meant to be complete debugging sessions that eliminate every existing bug, though. I apologize if sometimes applications seem to go into too-detailed reviews.

Please read Review process for security advisory coverage: What to expect for more details and Security advisory coverage application checklist to understand what reviewers look for. Tips for ensuring a smooth review gives some hints for a smoother review.

The important notes are the following.

  • If you have not done it yet, you should enable GitLab CI for the project and fix the PHP_CodeSniffer errors/warnings it reports.
  • For the time this application is open, only your commits are allowed.
  • The purpose of this application is giving you a new drupal.org role that allows you to opt projects into security advisory coverage, either projects you already created, or projects you will create. The project status will not be changed by this application; once this application is closed, you will be able to change the project status from Not covered to Opt into security advisory coverage. This is possible only 14 days after the project is created.

    Keep in mind that once the project is opted into security advisory coverage, only Security Team members may change coverage.
  • Only the person who created the application will get the permission to opt projects into security advisory coverage. No other person will get the same permission from the same application; that applies also to co-maintainers/maintainers of the project used for the application.
  • We only accept an application per user. If you change your mind about the project to use for this application, or it is necessary to use a different project for the application, please update the issue summary with the link to the correct project and the issue title with the project name and the branch to review.

To the reviewers

Please read How to review security advisory coverage applications, Application workflow, What to cover in an application review, and Tools to use for reviews.

The important notes are the following.

  • It is preferable to wait for a project moderator before posting the first comment on newly created applications. Project moderators will do some preliminary checks that are necessary before any change on the project files is suggested.
  • For the tools to use for reviewing a project, see Tools to use for reviews.
  • It may be best to have the applicant fix things before further reviews and avoid doing a review when another review is pending.

For new reviewers, I would also suggest to first read In which way the issue queue for coverage applications is different from other project queues.

vishal.kadam’s picture

Issue tags: +PAreview: project created less than ten days ago
vishal.kadam’s picture

Status: Needs review » Needs work

FILE: maxlength_words.module

A new module that aims to be compatible with latest Drupal releases is expected to implement hooks as class methods as described in Support for object oriented hook implementations using autowired services.

avpaderno’s picture

Issue tags: -PAreview: project created less than ten days ago

We no longer use PAreview: project created less than ten days ago because applicants do not know what the issue tag means. More often, it confuses them, who think something needs to be done from their side.

daniel.matcau’s picture

Status: Needs work » Needs review

@vishal.kadam Thank you for the review in #5.

The hooks are now implemented as methods of \Drupal\maxlength_words\Hook\MaxlengthWordsHooks, an autowired hook class (commit 773bc8a0 on 1.0.x).

  • Since the module supports Drupal 10.3, maxlength_words.module keeps only thin procedural wrappers marked with #[LegacyHook], which call the hook class on Drupal < 11.1 and are skipped on later versions.
  • The form alter must run after MaxLength's. This is set with #[ReorderHook] on Drupal 11.2+, and with hook_module_implements_alter() (marked #[LegacyModuleImplementsAlter]) on earlier versions.
  • The word counting logic was moved to a WordCounter class, covered by a unit test. A kernel test checks the hook order.
jamestarleton’s picture

Reviewed 1.0.x at 773bc8a. I found no security issues: the count-down message is output as HTML the same way MaxLength does it, and the hard limit is also enforced on submit (core copies #element_validate to the text_format value child). CI passes on Drupal 10 and 11.

One non-blocking bug:

WordCounter::count() and countWords() in js/maxlength-words.js always strip <…> as tags, but string and link widgets hold plain text. x < y and z > w counts as 2 words, and with "Hard limit" a 10-word title accepts any number of words placed between < and > (neither the JS trimming nor the server validation catches it).

Suggest stripping tags and decoding entities only for text* field types, e.g. a #maxwords_html flag / data-maxwords-html attribute set in convertElement(), plus a unit test case for plain text with <.

daniel.matcau’s picture

@jamestarleton Thank you for the review in #8; good catch.

Fixed in commit 22705e35 on 1.0.x (released in 1.0.0-beta2):

  • Tags are stripped and entities decoded only for formatted text fields (text, text_long, text_with_summary).
  • String and link fields are counted as plain text, both in the JavaScript counter and in the server-side validation. x < y and z > w now counts as 5 words, and the hard limit can no longer be bypassed with angle brackets.
  • Added unit tests for plain text with < and >, and a kernel test that a string field with a hard limit of 3 words rejects a <b c d e> f.

CI passes on Drupal 11 and Drupal 10.

jamestarleton’s picture

Thanks @daniel.matcau. I checked 22705e35 (1.0.0-beta2): string and link fields are now counted as plain text in both WordCounter::count() and countWords(). x < y and z > w counts as 5 words, and a 10-word hard limit now rejects one <two … eleven> twelve. CI is green, so the bypass from #8 is fixed.

avpaderno’s picture

Assigned: Unassigned » avpaderno
  /**
   * {@inheritdoc}
   */
  protected static $modules = ['maxlength_words', 'maxlength'];

  /**
   * Tests the order of hook_field_widget_single_element_form_alter().
   */
  public function testFormAlterOrder(): void {
    $modules = [];
    $this->container->get('module_handler')->invokeAllWith('field_widget_single_element_form_alter', function (callable $hook, string $module) use (&$modules) {
      $modules[] = $module;
    });
    $this->assertContains('maxlength', $modules);
    $this->assertContains('maxlength_words', $modules);
    $this->assertGreaterThan(array_search('maxlength', $modules), array_search('maxlength_words', $modules));
  }

That test is checking #[ReorderHook] works as expected, but that is already tested by Drupal core.

avpaderno’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for your contribution and for your patience with the review process!

I am going to update your account so you can opt into security advisory coverage any project you create, including the projects you already created.

These are some recommended readings to help you with maintainership:

You can find more contributors chatting on Slack or IRC in #drupal-contribute. So, come hang out and stay involved!
Anyone is welcome to participate in the review process. Please consider reviewing other projects that are pending review. I encourage you to learn more about that process and join the group of reviewers.

I thank also all the reviewers for helping with these applications.

avpaderno’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

ruslan piskarov’s picture

Hey @daniel.matcau.
Congrats on your first module.
I know this task is already fixed; however, I want to propose a few improvements.

1. Add logo.svg or png for https://www.drupal.org/project/project_browser.

2. Since all hooks are already in web/modules/contrib/maxlength_words/src/Hook/MaxlengthWordsHooks.php
Create maxlength_words.services.yml file with

parameters:
  maxlength_words.skip_procedural_hook_scan: true

It will add a little performance improvement when creating the cache and rebuilding hooks.

daniel.matcau’s picture

@ruslan-piskarov Thank you!

Both are already done in 1.0.0: the logo for Project Browser and skip_procedural_hook_scan.

Drupal 12 compatibility will come once MaxLength, which this module depends on, supports Drupal 12.