When using ajax_comments plus layout builder fatal errors will occur when rendering or submitting the comment form on some pages. This seems to be happening when ajax_comments checks whether it should fall back to the default comment system.
E.g. in AjaxCommentsForm::buildForm():
// Check to see if this comment field uses ajax comments.
$comment_formatter = $this->fieldSettingsHelper->getFieldFormatterFromComment($comment, 'full');
if (!$this->fieldSettingsHelper->isEnabled($comment_formatter)) {
// If not using Ajax Comments, return the unmodified form.
return $form;
}
but FieldSettingsHelper::getFieldFormatterFromComment() wraps FieldSettingsHelper()::getFieldFormatter() which can return FALSE if the formatter can't be loaded:
// Get the comment field display configuration from the entity's
// view mode configuration.
$display_options = $view_display
->getComponent($field_name);
// If the field is hidden on the provided view_mode, $display_options
// will be empty. Trying to get a field formatter instance will cause
// an error.
if (empty($display_options)) {
$comment_formatter = FALSE;
}
so we should check if the formatter is actually available before we pass it to FieldSettingsHelper::isEnabled(), e.g.:
// Check to see if this comment field uses ajax comments.
$comment_formatter = $this->fieldSettingsHelper->getFieldFormatterFromComment($comment, 'full');
if (empty($comment_formatter) || !$this->fieldSettingsHelper->isEnabled($comment_formatter)) {
// If not using Ajax Comments, return the unmodified form.
return $form;
}
In my case the formatter isn't being loaded because the vanilla display doesn't have a comment formatter (it's actually in a field block in layout builder). There should probably be a larger fix here to try and figure out the comment settings from layout builder alone. But it seems to be legitimate for $comment_formatter to be empty so I think the fix is applicable in any case, and it's adequate for my use case.
This patch can be combined with the workaround of making sure the display exists in the non-layout-builder display configuration for the comments field, e.g.:
field_comments:
type: comment_default
weight: 3
region: content
label: above
settings:
view_mode: default
pager_id: 0
third_party_settings: { }
Rather than just being hidden, e.g.:
hidden:
field_comments: true
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 3074600-2-fatal-errors-layout-builder.patch | 3.3 KB | dylan donkersgoed |
Comments
Comment #2
dylan donkersgoed commentedPatch attached.
Comment #3
dylan donkersgoed commentedComment #4
jordan.caldwell commentedLatest patch #2 has solved our issue with Layout Builder. Moving to RTBC
Comment #6
qzmenkoCommitted and pushed. Thanks!
Comment #7
qzmenko