You are here

public function FieldSettingsHelper::getFieldFormatterFromComment in AJAX Comments 8

Get the active field formatter for a comment entity.

Parameters

\Drupal\comment\CommentInterface $comment: The comment entity.

string $view_mode: The current view mode.

Return value

\Drupal\Core\Field\FormatterInterface The field formatter for the current comment.

File

src/FieldSettingsHelper.php, line 131

Class

FieldSettingsHelper
Class FieldSettingsHelper.

Namespace

Drupal\ajax_comments

Code

public function getFieldFormatterFromComment(CommentInterface $comment, $view_mode = 'default') {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $commented_entity */
  $commented_entity = $comment
    ->getCommentedEntity();
  $field_name = $comment
    ->getFieldName();

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $commented_entity
    ->getFieldDefinition($field_name);

  // Load the configuration entity for the entity's view display settings.

  /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface $view_display */
  $view_display = $this
    ->getEntityViewDisplay($comment, $view_mode);

  // Get the formatter for the current comment field.

  /** @var \Drupal\Core\Field\FormatterInterface $comment_formatter */
  $comment_formatter = $this
    ->getFieldFormatter($view_display, $field_name, $field_definition, $view_mode);
  return $comment_formatter;
}