Почему можно перехватить ajax вызов из модальной друпальной формы? - PullRequest
0 голосов
/ 10 марта 2020

Я не понимаю, потому что после того, как я открыл модальную форму в drupal, используя этот метод (внутри класса Announcements)

  public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) {
    // We begin building a new ajax reponse.
    $response = new AjaxResponse();
    //$form_state = new  \Drupal\Core\Form\FormState();
    $modal_form = \Drupal::formBuilder()->getForm('Drupal\cdfsport\Form\AnnouncementsModalEventForm');

/* $this
      ->messenger()
      ->deleteAll();*/
      // Add the OpenModalDialogCommand to the response. This will cause Drupal
      // AJAX to show the modal dialog. The user can click the little X to close
      // the dialog.
     $response->addCommand(new \Drupal\Core\Ajax\OpenModalDialogCommand('Aggiungi evento', $modal_form, static::getDataDialogOptions()));


    // Finally return our response.
    return $response;
  }

, где класс для модальной формы:

class AnnouncementsModalEventForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'modal_event_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
    $form['#prefix'] = '<div id="modal_form">';
    $form['#suffix'] = '</div>';

    // The status messages that will contain any form errors.
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];

     $content['selection-wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Selezione'),
      '#attributes' => ['id' => 'selection-wrapper'],
      '#weight' => 1,
    ];
        $form['selection-wrapper']['weekday'] = [
      '#type' => 'select',
      '#title' => t('Giorno della settimana'),
      '#options'=>array('lun'=>'Lunedì','mar'=>'Martedì','mer'=>'Mercoledì','gio'=>'Giovedì','ven'=>'Venerdì','sab'=>'Sabato','dom'=>'Domenica'),
      '#size'=>1,
      '#required'=>true,
      '#weight' => 0,
       ];
      $opts=array();
      for($i=0;$i<24;$i+=1) $opts["$i"."_00"]="$i:00";
      $form['selection-wrapper']['day'] = [
      '#type' => 'date',

      '#title' => t('Giorno'),

      '#size'=>1,
      '#weight' => 1,
       ];
     $form['selection-wrapper']['fromh'] = [
      '#type' => 'select',

      '#title' => t('Da'),
       '#options'=>$opts,
      '#size'=>1,
      '#weight' => 2,
       ];
        $form['selection-wrapper']['toh'] = [
      '#type' => 'select',

      '#title' => t('A'),
       '#options'=>$opts,
      '#size'=>1,

      '#weight' => 3,
       ];

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['send'] = [
      '#type' => 'button',
      '#value' => $this->t('Aggiungi'),
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
      '#ajax' => [
        'callback' => '::submitModalFormAjax1',
        'event' => 'click',
      ],
    ];



    return $form;
  }

  /**
   * AJAX callback handler that displays any errors or a success message.
   */
  public function submitModalFormAjax1(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    // If there are any form errors, re-display the form.
    if ($form_state->hasAnyErrors()) {
      $response->addCommand(new ReplaceCommand('#modal_event_form', $form));
    }
    else {
      //Close the modal.
      $command = new CloseModalDialogCommand();
      $response->addCommand($command);
    }
    return $response;
  }
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

       return;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

      return;

  }


}

I нажал кнопку отправки в модальной форме, но метод submitModalFormAjax1 не вызывается (я наблюдаю за ним с помощью отладки). Я не могу найти пример в Интернете, как сделать эту простую вещь.

...