EnforcedResponseException while trying to render item entity:node/1000019:de with view mode default for search index Global search

Created on 17 July 2024, 3 months ago
Updated 22 July 2024, 3 months ago

Problem/Motivation

When indexing, I get the following errors for nodes being indexed that contain a certain custom form:

Drupal\Core\Form\EnforcedResponseException while trying to render item entity:node/1000019:de with view mode default for search index Global search: in Drupal\Core\Form\FormBuilder->buildForm() (line 357 of /web/core/lib/Drupal/Core/Form/FormBuilder.php).

Backtrace:

#0 /web/core/lib/Drupal/Core/Form/FormBuilder.php(224): Drupal\Core\Form\FormBuilder->buildForm()
#1 /web/modules/custom/example/src/Plugin/Block/SimpleRegisterBlock.php(52): Drupal\Core\Form\FormBuilder->getForm()
#2 /web/modules/contrib/block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php(240): Drupal\example\Plugin\Block\SimpleRegisterBlock->build()
#3 [internal function]: Drupal\block_field\Plugin\Field\FieldFormatter\BlockFieldFormatter->preRender()
#4 /web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(113): call_user_func_array()

The form is quite simple:

namespace Drupal\example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 *
 */
class SimpleRegisterForm extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['mail'] = [
      '#type' => 'email',
      '#title' => $this->t('Sign up now with your email address'),
      '#placeholder' => $this->t('Sign up by email'),
      '#required' => TRUE,
    ];

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Try free now!'),
      '#button_type' => 'primary',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Redirect to the trial registration form on submit.
    $form_state->setRedirect('user.register', [], [
      'query' => [
        'mail' => $form_state->getValue('mail'),
      ],
    ]);
    return;
  }
}

It's a quick registration prefill form, redirecting the user to the register page with email address prefilled.
This works totally fine, just the search_api indexing throws the error. Opening the page and using the form works perfectly well.
Redirecting like that is suggested in many blog entries and I think that should be fine and should only happen on form submit?!

The form code isn't too complex and I can't see any issue there. Furthermore I can't really understand why the Exception is being thrown only on indexing.

This happens here:

        // If the form returns a response, skip subsequent page construction by
        // throwing an exception.
        // @see Drupal\Core\EventSubscriber\EnforcedFormResponseSubscriber
        //
        // @todo Exceptions should not be used for code flow control. However, the
        //   Form API does not integrate with the HTTP Kernel based architecture of
        //   Drupal 8. In order to resolve this issue properly it is necessary to
        //   completely separate form submission from rendering.
        //   @see https://www.drupal.org/node/2367555
        if ($response instanceof Response) {
            throw new EnforcedResponseException($response);
        }

(https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21Fo...)

Here's the related core issue to remove this code-flow Exception workaround: #2367555: Deprecate EnforcedResponseException support
The only search_api issue with this exception I could find was: 🐛 View recalculated with wrong data from cache Needs review But is that really related? (I don't think so).

So my main question is: Why does this happen with search_api?
Or is it the combination with block_field module? (See backtrace)
Then the next one is: What can I do about it?

Thank you very much for your ideas!

Steps to reproduce

See above

Proposed resolution

Remaining tasks

💬 Support request
Status

Closed: cannot reproduce

Version

1.0

Component

General code

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Comments & Activities

  • Issue created by @Anybody
  • 🇩🇪Germany Anybody Porta Westfalica
  • Status changed to Postponed: needs info 3 months ago
  • 🇦🇹Austria drunken monkey Vienna, Austria

    Sorry, I don’t really understand why this is happening, either. For some reason the Drupal Form API seems to be thinking that the form is being submitted.
    Does this always happen when indexing, or maybe only when indexing via the “Index now” button in the UI?
    In any case, for some reason the $form_state->isProcessingInput() check in FormBuilder::processForm() seems to return TRUE, probably because $form_state->setProcessInput() was called in doBuildForm(). But I think you’d have to debug yourself why exactly this is happening, seems pretty specific to your setup.

    In case it turns out that there are some simple changes we could make in this module to fix that error, we could still do that, of course. (Especially if you could manage to write a failing test case demonstrating the problem.) It just doesn’t seem like an actual bug in the Search API.

    I agree, 🐛 View recalculated with wrong data from cache Needs review doesn’t seem at all related.

  • Status changed to Closed: cannot reproduce 3 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks for the feedback @drunken monkey - this also happened on cron run. Really really strange thing and wild side effect I think.

    This didn't happen again now, so let's for now close this cannot reproduce and if someone runs into the same issue, let's reopen this. I guess it's a very very specific case and not really the fault of this module.

Production build 0.71.5 2024