Ajax callback inside views plugin doesn't work.

Created on 17 May 2024, about 1 month ago

Problem/Motivation

When you make a new views plugin wiith ajax form inside, the ajax callback of form not working on the first reload.
If you open the settings of plugin in a second time everything is ok.
I tested it on row, style and filter.

Steps to reproduce

For example you can try with the module better exposed filter
or
Add new views plugin (row, style, filter)
Implement buildOptionsForm() method with an ajax callback


  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($original_form, $form_state);
    $form['test']=[
      "#type" => "select",
      '#default_value' => "test",
      "#title" => t("Component"),
      "#options" => ['test'=>'test','test2'],
      '#ajax' => [
        'callback' => __CLASS__. '::changeSelectorFormChangeAjax',
        'wrapper' => 'test',
        'effect' => 'fade',
        '#suffix' => '<div id="test"></div>'
      ],
      '#executes_submit_callback' => FALSE,
      '#required' => TRUE,
    ];
  }

  /**
   * Ajax callback for component selector change.
   */
  public static function changeSelectorFormChangeAjax(
    array $form,
    FormStateInterface $form_state,
  ) {
    return ['#markup'=>'hello'];
  }

1 Add your plugin
2 Choose your plugin inside views ui
3 After the first callback ajax the form options of plugin is loaded
4 Try the callback ajax of the form options => the ajax callback of the previous submit is trigger

Proposed resolution

The problem seems to be that the wrong form is set when the trigerring element is set.
For example a row plugin, after the click on ajax option form, the $formState->get('section') give 'row' and instead of 'row_option' (/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php) and also rebuild the previous form.

I don't know where this 'section' of formstate is set but it may be the problem

πŸ› Bug report
Status

Active

Version

10.4 ✨

Component
Views UIΒ  β†’

Last updated 5 days ago

Created by

πŸ‡«πŸ‡·France Musa.thomas France πŸ‡«πŸ‡·

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

Comments & Activities

Production build 0.69.0 2024