Ajax callback inside views plugin doesn't work.

Created on 17 May 2024, 11 months 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 9 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

  • Issue created by @musa.thomas
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

  • πŸ‡ͺπŸ‡¬Egypt Ahmed Eldesoky

    I can confirm this issue when placing the ajax callback within the same class that provides the options form

    however a simple fix would be placing the ajax callback on your .module file
    something like this

    /**
       * {@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' => '_your_module_ajax_callback',
            'wrapper' => 'test',
            'effect' => 'fade',
            '#suffix' => '<div id="test"></div>'
          ],
          '#executes_submit_callback' => FALSE,
          '#required' => TRUE,
        ];
      }

    and in your .module file

    function _your_module_ajax_callback($form,FormStateInterface $form_state ) {
        return ['#markup'=>'hello'];
      }
    
  • πŸ‡ΊπŸ‡ΎUruguay gonzalo2683

    I've been experiencing the same issue with AJAX in a Views filter plugin. I've implemented the recommendations mentioned in this thread (using static callbacks, storing the instance in form_state πŸ’¬ Strange problems with ajax in a custom views filter plugin Closed: outdated ), but I'm still encountering a problem during the first interaction.

    Specifically, when I first add the filter and select an option that should trigger the AJAX to load checkboxes, it incorrectly reloads the entire filter form instead of just updating the specific element. Interestingly, if I close the modal and reopen the filter configuration, the AJAX works correctly on subsequent interactions.

    This suggests there might be an issue with how the form is initially built or how the AJAX callback is locating the correct element to replace during that first interaction. Has anyone found a solution for this specific "first load" problem?

Production build 0.71.5 2024