Problem/Motivation

Main parts of the CAPTCHA 8.x-1.x module seem to be a port of the Drupal 7 CAPTCHA module, with quite similar implementation.

But since Drupal 8 form styling and overriding was improved a lot by using twig files for form elements where ever possible. Trying to reduce the need for hooks to theme Drupal forms (as it's bad for theme developers / designers).

Having a look at the resulting markup and the implementation, CAPTCHA didn't improve a lot here. The resulting markup, which is also used by CAPTCHA extending modules like

and other CAPTCHA types using hook_captcha() can also be improved.

In this task we should discuss two things:
1. How to better use twig files to replace the need for hooks and technical FAPI implementations for theming the CAPTCHA generate output
2. How to improve the currenct CAPTCHA output markup like for example:

  • Add a class for the captcha type to the wrapper element
  • Discuss if "details" is really a good choice for a required form element wrapper - I guess not ;)
  • Make the "details" title configurable in the CAPTCHA settings with default: "CAPTCHA"
  • Allow to change the order / alignment of the captcha elements, for example beside or below each other by CSS
  • Don't use "CAPTCHA" as readable label in the frontend, most of the users have no idea what this means. So this is a useless label.
  • #3258191: Surround the 'Challenge description' phrase with an html tag
  • ...?

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork captcha-3314766

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anybody created an issue. See original summary.

Anybody’s picture

Anybody’s picture

Issue summary: View changes
Anybody’s picture

Anybody’s picture

Related issues:
Anybody’s picture

Related issues:
thomas.frobieter’s picture

I think the very first thing to improve, should be the formatter CSS class on the wrapper, so the module styles / theme styles are able to address the differences.

thomas.frobieter’s picture

Issue summary: View changes
Anybody’s picture

Issue summary: View changes

Added:

  • Make the "details" title configurable in the CAPTCHA settings with default: "CAPTCHA"

This needs heavy rework, I'll take some time for a MR soon.

thomas.frobieter’s picture

My 2 cent about the markup and the details element:

I have no idea why anyone would have the CAPTCHA in a collapsed state, so the Details element doesn't make any sense for me. I wouldn't even add this as an option. If someone needs this, its easy to override the Twig file.

In my opinion, every single CAPTCHA element should dropped as variable to the Twig template. All Markup should live inside this template file.

And for sure, the text "Answer this question to verify that you are not a spam robot." needs a wrapper then.

Anybody’s picture

Title: Improve the CAPTCHA form markup and use twig files for more flexibility, where possible » [2.x] Improve the CAPTCHA form markup and use twig files for more flexibility, where possible

Okay, made a first shot. Most of the points above have been addressed. Due to the markup changes, I think this would be a good point for creating a 2.x dev version.

@thomas.frobieter:

Allow to change the order / alignment of the captcha elements, for example beside or below each other by CSS

this and allowing to hide the descriptive label (which we had hidden in our custom css) would be a nice setting I guess?

Any ideas how we could add that and how the setting could be named and look like?

  • CAPTCHA alignment?
  • Hide label?
Grevil’s picture

@thomas.frobieter please have a look at the changes once again.

Grevil’s picture

Status: Active » Needs work

These changes currently break the module with the following error:

Twig\Error\SyntaxError: Unknown "clean_class_array" filter. in Twig\ExpressionParser->getFilterNodeClass() (line 22 of modules/custom/captcha/templates/captcha.html.twig).

After a quick google search I found out, that we use the correct sanitization syntax, so I am not sure why the filter can not be found. 🤔

But I have no experience with templates, so maybe @thomas.frobieter can have a look!

thomas.frobieter’s picture

Oh yeah, the use of the clean_class_array fiter was a mistake. This filter is n/a in Drupal Core, its a contrib module: https://www.drupal.org/docs/8/modules/twig-tools/sanitization-filters

I'll change this, give me a second.

Grevil’s picture

Thx! I'll have a second look tomorrow, but can already tell from the tests and a quick peek at my local captcha login form, that this definitely still needs work!

thomas.frobieter’s picture

@thomas.frobieter:

Allow to change the order / alignment of the captcha elements, for example beside or below each other by CSS

this and allowing to hide the descriptive label (which we had hidden in our custom css) would be a nice setting I guess?

Any ideas how we could add that and how the setting could be named and look like?

CAPTCHA alignment?
Hide label?

@anybody: TBH, this sounds very unnecessary to me. This would be a global setting? But the CAPTCHA appears in multiple forms, various widths .. so I don't know, seems useless if not configurable for each CAPTCHA instance.

I think we better should ensure a solid default layout, what is very flexible/responsive by default. So for example the image captcha: Image left, input & text right while theres enough width, Image above if not.

We can do this with CSS Grid or Flexbox very easily, so no need for setting, IMO.

Anybody’s picture

I think we better should ensure a solid default layout, what is very flexible/responsive by default. So for example the image captcha: Image left, input & text right while theres enough width, Image above if not.

Very good idea! Yes please :)
Could you add this point for you?

Grevil’s picture

OK, I couldn't finish this, as I had internal debugging problems with my dev environment.

The point is, that we need to additionally regard the "persistence" setting. This is considered in our FormElement, through "_captcha_required_for_user()" but not in our preprocess hook. So we need to get the required variables for the "_captcha_required_for_user()" function inside the preprocess hook and only render title and description if it returns true. Otherwise, if somebody already fulfilled the captcha but put in a wrong username, the captcha element and type is hidden, but the title and description are still being displayed.

Anybody’s picture

Thanks @Grevil, as just discussed, we should then simply put it into the form element.

Grevil’s picture

OK all done! Please review!

@Anybody, especially check the update hook! For users which disabled the description, but still have a description string set, this update will delete that description. We could possibly notice the user about this, inside the update hook, but doubt anybody has important text inside the description worth noticing about.

Grevil’s picture

Status: Needs work » Needs review
Anybody’s picture

Status: Needs review » Reviewed & tested by the community

Great work @Grevil and a super cool foundation for 2.x!

@Maintainer: As this contains BC for Theme overrides, please do not commit this to 8.x-1.x, instead create a 2.x from latest 8.x-1.x!

Grevil’s picture

Status: Reviewed & tested by the community » Needs work

Good thing this wasn't merged yet! There is a BIG mistake using "delete($param)" for the old config entry instead of "clear($param)" which leads to deleting the ENTIRE config object instead of the single object entry!! I am really sorry for not testing this update hook beforehand (won't happen again).

Grevil’s picture

Status: Needs work » Needs review
FileSize
895 bytes

Fixed the issue, see attached interdiff.

Anybody’s picture

Status: Needs review » Needs work

#18 is still open for @thomas.frobieter. @Thomas could you please assign this issue to you and add that part?
Or tell @Grevil how to do that?

As we're no maintainers (yet), I can't assign tasks to anyone else here.

Anybody’s picture

Version: 8.x-1.x-dev » 2.x-dev
Assigned: Unassigned » thomas.frobieter

CAPTCHA 2.x is up and running and we're Co-Maintainers now. Let's prepare this for the final commit!

Assigning this to @thomas.frobieter for #18.

Grevil’s picture

Rebased the branch on 2.x, Tests are all green locally.

thomas.frobieter’s picture

I'll take care about this when if have the time.

Grevil’s picture

Rebased on current 2.x.

thomas.frobieter’s picture

Okay I am ready with the image captcha styling. I've added a further wrapper in the form, to group the image and the reload link.

I've also done some cleanup, separated admin scripts & styles, created a libraries file + CSS for the captcha module itself.

The reload link now is a simple reload-icon instead text, to reduce clutter. Maybe this will need some accessibility love, but I think the image captcha itself is an accessibility nightmare, so ...

Anybody’s picture

Assigned: thomas.frobieter » Unassigned
Status: Needs work » Needs review

@Grevil: This should then be reviewed and tested carefully for the many changes. If you see the need for tests somewhere, let's add them now!

Grevil’s picture

Rebased the branch! Twig file had a merge conflict 12 times, so I just fixed it afterwards :)

I'll check everything carefully now and see if anything broke!

Grevil’s picture

Tests are green and code looks good!

Although, we should definitely split such big issues in several smaller issues. Trying to review so many changes at once, can lead to overseeing problematic code.

Furthermore, trying to rebase this issue branch was an utter pain and leads to time-consuming actions. Note that rebasing means, that for every single commit done in dev, we have to reverse ALL commits done here, add the commit from dev and then ADD all commits from this issue branch again. Those are 31 commits now and counting. Meaning worst case, we could have 31 merge conflicts for EVERY SINGLE commit done in dev, before committing this!

So let's break it apart next time!

OK, on to the manual functionality test! :)

Anybody’s picture

Status: Needs review » Reviewed & tested by the community

Although, we should definitely split such big issues in several smaller issues. Trying to review so many changes at once, can lead to overseeing problematic code.

Agree!

So let's merge this :)

  • Anybody committed e97f2e6 on 2.x
    Issue #3314766 by Grevil, thomas.frobieter: [2.x] Improve the CAPTCHA...
Anybody’s picture

Status: Reviewed & tested by the community » Fixed

  • Grevil committed c10ea5e on 2.x
    Post-fix for the libraries.yml related to Issue #3314766

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Grevil’s picture

Just a small reminder, as I couldn't remember, why 2.x is D10 only.

The reason behind this is, that besides the type hinting introduced in the newest Symfony release, used for D10, there were also method implementation changes (See https://git.drupalcode.org/project/captcha/-/merge_requests/47/diffs#1f6...).

That's why the newest release is D10 only! 👍