Problem/Motivation

For some reason, "ImageCaptchaConstants" won't get recognized inside image_captcha.install on installation, leading to:

Error: Class "Drupal\image_captcha\Constants\ImageCaptchaConstants" not found in image_captcha_requirements() (line 20 of modules/custom/captcha/modules/image_captcha/image_captcha.install).

The correct folder and namespace structure is present and correctly applied, as the "captcha" uses the exact same folder / namespace structure, which works flawless.

So either a dedicated "Constants" folder does not work properly for submodules, or Constants should reside inside a different folder entirely.

Steps to reproduce

Proposed resolution

Temporarily move all image captcha constants in to the captcha constant class and resolve this problem later.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork captcha-3321215

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

Grevil created an issue. See original summary.

  • Grevil committed 9933634 on 2.x
    Temporarily moved image captcha constants into captcha constants related...
Grevil’s picture

Priority: Critical » Normal

Temporarily fixed the problem, by moving all image captcha constants into the captcha constants class.

Setting this back to normal, as the image_captcha module is installable again.

Grevil’s picture

The module "etracker" uses a "Helper" folder / namespace where a Constants class is defined in, but this also does not work for submodules (https://git.drupalcode.org/project/etracker/-/tree/8.x-3.x/).

Grevil’s picture

Issue summary: View changes

Note, using the Constant via "\Drupal\image_captcha\Constants\ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB" inside the install file is against the Drupal coding standards ("Namespaced classes/interfaces/traits should be referenced with use statements") and does not work either.

corE’s picture

Just throwing in info from my update experience, if needed/helpful.
Updating CAPTCHA 8.x-1.6 to 8.x-1.7
The update page downloaded the module and than I got a white page with message:

The website encountered an unexpected error. Please try again later.
AssertionError: The file specified by the given app root, relative path and file name (.../modules/captcha/image_captcha/image_captcha.info.yml) do not exist. in assert() (line 67 of core/lib/Drupal/Core/Extension/Extension.php).

Seems it wants the image_captcha folder/files, which was not in the update. There was a new folder named ".tugboat" with one file "config.yml" which was not in 8.x-1.6 version.
I reinstalled the 8.x-1.6 version to get back to normal.
- Drupal Version 9.4.8
- PHP Version 8.1.12

Thank you.

Grevil’s picture

@corE, thanks for the info! Have you tried clearing all caches and running "drush updb"?

I have not touched the module file in the newest release, as you can see here. Also, the tugboat integration is already present in 8.x-1.6. Only moved some constants for 8.x-1.7 release.

Please create a separate issue if this is still a problem.

Anybody’s picture

@corE I think you accidentially updated to 2.x. At least that would fit the error message.

https://git.drupalcode.org/project/captcha/-/tree/8.x-1.x shows, the path you mentioned exists. Anyway I don't think that's related to this issue.

@Grevil how to proceed here? Simply close this fixed and keep as-is?

corE’s picture

Thank you Grevil... & Danke Anybody (I saw the German marker).

Sorry, I may have misinterpreted this posting destination here.

I will take your suggestions and try again.

- I did clear caches before updating, but I will try after the file replacement step to see if that helps.
- I didn't see tugboat folder in the 8.x-1.6 version that I had installed prior, and reinstalled.
- Anybody mentioned "accidentally updated to 2.x", but I'm not seeing a 2.x for anything. Hum?

Thank you for your help! I will sign out of this thread here and try updating again. If the issue I'm having is still there I will do as you recommend and create a separate issue.

Grevil’s picture

Status: Active » Fixed

No worries @corE and happy coding!

@Anybody I guess so! Since it probably is once again a submodule problem...

Grevil’s picture

Grevil’s picture

Status: Fixed » Needs work
Related issues: +#3323538: [2.x] Image captcha can't be installed directly.

Reopening this issue, as it leads to a minor issue, trying to directly install the image_captcha submodule in #3321215: "ImageCaptchaConstants" won't get recognized inside image_captcha.install.

Grevil’s picture

OK, I think I know how to fix this. The "ImageCaptchaConstants" class inside the submodule's "Constants" folder is not the problem, the only problem is the Constant used in the install file. Constants inside install files should be avoided, as this file is loaded before any php files of the module are loaded in. So when the install file executes, it doesn't know about the Constant, because the ImageCaptchaConstants.php hasn't loaded yet.

Grevil’s picture

Status: Needs work » Needs review

Should be fixed now, module still works as expected while testing it manually.

Please review!

Anybody’s picture

Super cool, let's see what the tests say!

Should the constants have comments btw?

Anybody’s picture

Status: Needs review » Reviewed & tested by the community

Code looks good and tests are passing now!

But still the questions:

Should the constants have comments btw?

I think they are self-explaining but perhaps comments would make sense?

Grevil’s picture

@Anybody, originally they had none. Furthermore, the use case for a few of them is pretty "debatable", see #3324321: Replace bitwise operations in if clauses related to "_image_captcha_check_setup" calls.

I am not sure if using bitwise operations in such a high level programming language as PHP serves any purpose...

Grevil’s picture

Status: Reviewed & tested by the community » Fixed

I'll commit this as is for now.

  • Grevil committed 451e3b6 on 2.x
    Issue #3321215: "ImageCaptchaConstants" won't get recognized inside...

Status: Fixed » Closed (fixed)

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