Problem/Motivation

I've run into a situation where I'd like to add some options (form fields) to the delete key confirmation form (key_delete_form) with my key_provider plugin on both Drupal 7 and Drupal 8 versions. On Drupal 7 it is correctly implemented with 'build delete form' plugin parameter. On Drupal 8 it supposed to be done by implementing KeyPluginDeleteFormInterface::buildDeleteForm().
That's how it looks like:

public function buildDeleteForm(array $form, FormStateInterface $form_state);

And this is the code, which calls this method in key_delete_form:

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    // Allow the plugins to modify the form.
    foreach ($this->entity->getPlugins() as $plugin) {
      if ($plugin instanceof KeyPluginDeleteFormInterface) {
        $plugin->buildDeleteForm($form, $form_state);
      }
    }

    return $form;
  }

So, if I'd like to implement this method in my plugin, according to interface I must pass $form param as [array $form]. I can't change it to reference, like [array &$form]. As result, I can't modify the original $form variable. On the other side, I can't just return the modified $form value, because as you can see, the buildForm method isn't processing the return values:

$plugin->buildDeleteForm($form, $form_state);

So the buildDeleteForm method is useless. And with current implementation of Key module I have to make this from modification in hook_form_alter.

Proposed resolution

The most simple solution imho is to update KeyPluginDeleteFormInterface by adding reference mark to $form param like this:

public function buildDeleteForm(array &$form, FormStateInterface $form_state);

I'm attaching the proposed patch with this update.

API changes

If someone tried to implement buildDeleteForm method without referenced &$form, the update may cause to php warnings on key delete form, if the key uses corresponding provider plugin.

CommentFileSizeAuthor
key-fix_delete_form_interface.patch550 byteszevai

Comments

zevai created an issue. See original summary.

rlhawk’s picture

Status: Active » Needs review

Thanks for catching this. Your solution makes sense.

  • rlhawk committed 2aaa72e on 8.x-1.x authored by zevai
    Issue #3100422 by zevai: KeyDeleteForm doesn't allow to modify itself...
rlhawk’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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