In bulk_form ensure the triggering element is the bulk_form button

Created on 18 October 2024, 28 days ago

Problem/Motivation

Issues like

led me here, and I think they are all caused by the core implementation of #[ViewsField("bulk_form")].

The problem is the following (I think):
The form submit button is created like this:

$form['actions']['submit']['#value'] = $this->t('Apply to selected items');

which is kind of fine. But the validation and submit handler assume that there can be only this single action and don't check which is the triggering element.

For example, in the validation the following is implemented:

    $action = $form_state->getValue('action');
    if (empty($action)) {
      $form_state->setErrorByName('', $this->emptyActionMessage());
    }

but it at least should check that the bulk action was the triggering element and is empty!
Currently any action submitted there leads to the error message.

Furthermore the same happens in the submit callback. There's also no check that the submit was meant for bulk_form.

So a poor man's solution could be something like this:

$triggerdElement = $form_state->getTriggeringElement()['#value'] == $this->t('Apply to selected items');

to demonstrate the check, but of course we should better add an #id on the button and check that instead of the button label.

That would allow further buttons to be added within $form['actions'] and I think it might also be a good idea to give this submit button a unique key, like:

$form['actions']['bulk-edit-submit']['#value'] = $this->t('Apply to selected items');

Steps to reproduce

  • Add a bulk_edit field on a view
  • Install a contrib module like weight which adds its own buttons on the form
  • The the conflicts in code and unexpected behavior

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

views.module

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024