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.
| Comment | File | Size | Author |
|---|---|---|---|
| key-fix_delete_form_interface.patch | 550 bytes | zevai |
Comments
Comment #2
rlhawkThanks for catching this. Your solution makes sense.
Comment #4
rlhawk