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

Comments

Dylan Donkersgoed created an issue. See original summary.

dylan donkersgoed’s picture

Patch attached.

dylan donkersgoed’s picture

Assigned: dylan donkersgoed » Unassigned
Status: Active » Needs review
jordan.caldwell’s picture

Status: Needs review » Reviewed & tested by the community

Latest patch #2 has solved our issue with Layout Builder. Moving to RTBC

qzmenko’s picture

Committed and pushed. Thanks!

qzmenko’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.