V
V
Vlad2018-10-22 16:05:14
Drupal
Vlad, 2018-10-22 16:05:14

What can be wrong with saving a comment in Drupal 8?

The comment-form form has been created in the admin panel. In mytheme.theme I change the form:

function mytheme_form_comment_comment_form_alter(&$form, FormStateInterface $form_state, $form_id = NULL) {
  $form['actions']['submit']['#validate'] = ['Drupal\mytheme\AjaxCommentForm::ajaxValidateCallback'];
  $form['actions']['submit']['#ajax'] = [
    'callback' => 'Drupal\mytheme\AjaxCommentForm::ajaxSubmitCallback',
    'event'    => 'click',
    'progress' => FALSE,
  ];
}

Part of mytheme\src\AjaxCommentForm code:
class AjaxCommentForm {
  public function ajaxSubmitCallback(array &$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    ...

    $message = [
      '#theme'           => 'status_messages',
      '#message_list'    => \Drupal::service('messenger')->all(),
      '#status_headings' => [
        'status'  => t('Status message'),
        'error'   => t('Error message'),
        'warning' => t('Warning message'),
      ],
    ];

    $messages = \Drupal::service('renderer')->render($message);
    $response->addCommand(new HtmlCommand('#' . Html::getClass($form['form_id']['#value']) . '-messages', $messages));


    return $response;
  }

  public function ajaxValidateCallback(array &$form, FormStateInterface $form_state) {
    
    ...

    if (strlen($form_state->getValue('field_comment_name')) === 0) {
      $form_state->setErrorByName('field_comment_name', 'Введите Ваше имя');
    }
  }
}

Validation works as it should, but when trying to save, an error pops up
Drupal\Core\Entity\EntityStorageException: Entity validation was skipped.

If I turn off my validation, then the form saves the new comment without problems. In which direction to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andead, 2018-10-22
@progress_man

Try
And the methods in AjaxCommentForm must be static.
And the business logic should be in the module, not in the theme.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question