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

ddrozdik created an issue. See original summary.

afi13’s picture

Sounds logical for me. hook_captcha() looks like solution from Drupal 6. We should replace legacy code by new plugin types.

Berdir’s picture

There'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.

ddrozdik’s picture

@Berdir seems you are talking about this issue https://www.drupal.org/node/2558243, which is actually about converting another hook.

Berdir’s picture

No no :)

I meant the work done in https://github.com/M1r1k/captcha

ddrozdik’s picture

Ah, ok, that code could be used as start point of converting.

couturier’s picture

joachim’s picture

> 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

Anybody’s picture

Title: Convert hook_captcha to Plugins » [2.x] Convert hook_captcha to Plugins
Category: Task » Feature request

Nice plan for 2.x and cleanup! :)

Anybody’s picture

Title: [2.x] Convert hook_captcha to Plugins » [PP-1][2.x] Convert hook_captcha to Plugins
Status: Active » Postponed
Anybody’s picture

Version: 8.x-1.x-dev » 2.x-dev
Anybody’s picture

Title: [PP-1][2.x] Convert hook_captcha to Plugins » [2.x] Convert hook_captcha to Plugins
Status: Postponed » Active

2.x is prepared now, so we can start working on this. @joachim would you like to do that as of #8?

Anybody’s picture

Title: [2.x] Convert hook_captcha to Plugins » Convert hook_captcha to Plugins

2.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)