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