Problem/Motivation
Currently there is a hook_captcha() in the module:
Description of the hook
The hook_captcha() hook is the only required function if you want to integrate
with the base CAPTCHA module.
Functionality depends on the first argument $op:
- 'list': you should return an array of possible challenge types
that your module implements. - 'generate': generate a challenge.
You should return an array that offers form elements and the solution
of your challenge, defined by the second argument $captcha_type.
The returned array $captcha should have the following items:
$captcha['solution']: this is the solution of your challenge
$captcha['form']: an array of the form elements you want to add to the form.
There should be a key 'captcha_response' in this array, which points to
the form element where the user enters his answer.
An optional additional argument $captcha_sid with the captcha session ID is
available for more advanced challenges (e.g. the image CAPTCHA uses this
argument, see image_captcha_captcha()).
This way is not very comfortable for modules to provide new types of CAPTCHA and does not allow to decouple needed code for custom type of CAPTCHA from entire module.
Proposed resolution
For better architecture, and to follow a way in which Drupal provides ability to extend default functionality I suggest to convert hook_captcha to plugins architecture, where each module will provide own plugin with custom realization of captcha.
This make sense to do for version 8.2.x
User interface changes
No changes.
API changes
Yes, some API parts have to be changed.
Data model changes
No changes.
Comments
Comment #2
afi13 CreditAttribution: afi13 commentedSounds logical for me. hook_captcha() looks like solution from Drupal 6. We should replace legacy code by new plugin types.
Comment #3
BerdirThere's an existing issue or PR somewhere where someone started on this already, but it was a very long time ago and just a very basic start.
Comment #4
ddrozdik CreditAttribution: ddrozdik as a volunteer commented@Berdir seems you are talking about this issue https://www.drupal.org/node/2558243, which is actually about converting another hook.
Comment #5
BerdirNo no :)
I meant the work done in https://github.com/M1r1k/captcha
Comment #6
ddrozdik CreditAttribution: ddrozdik as a volunteer commentedAh, ok, that code could be used as start point of converting.
Comment #7
couturier CreditAttribution: couturier as a volunteer commentedComment #8
joachim CreditAttribution: joachim commented> This make sense to do for version 8.2.x
I think this could be done in a backwardly-compatible way in the current branch:
- if a captcha ID contains a '/' then then the part before the '/' is taken to be a module name:
-- if that module implements hook_captcha(), then it's a captcha type defined in a hook, where the ID is of the form 'MODULE_NAME/TYPE_IN_HOOK'
-- if that module does not implement hook_captcha(), then replace the / with a _ to make the plugin ID, and log a warning to tell the site admin to update their settings. The behaviour allows modules to convert their captcha types from hook to plugin without having to update user's settings
- otherwise, it's a captcha plugin ID
Comment #9
AnybodyNice plan for 2.x and cleanup! :)
Comment #10
AnybodyPOSTPONED on #3314766: [2.x] Improve the CAPTCHA form markup and use twig files for more flexibility, where possible as this is a 2.x issue.
Comment #11
AnybodyComment #12
Anybody2.x is prepared now, so we can start working on this. @joachim would you like to do that as of #8?
Comment #13
Anybody2.x is close and I think we shouldn't make the switch yet and it's a larger task.
Let's wait if someone likes to create a MR. Might be combined with a general refactoring of the quite old CAPTCHA implementation (also a bunch of work)