You are here

public function FieldSettingsHelper::getFieldFormatter in AJAX Comments 8

Get the active field formatter for a comment field.

Parameters

\Drupal\Core\Entity\Display\EntityDisplayInterface $view_display: The commented entity view display configuration.

string $field_name: The machine name of the comment field.

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field configuration.

string $view_mode: The current view mode.

Return value

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

1 call to FieldSettingsHelper::getFieldFormatter()
FieldSettingsHelper::getFieldFormatterFromComment in src/FieldSettingsHelper.php
Get the active field formatter for a comment entity.

File

src/FieldSettingsHelper.php, line 94

Class

FieldSettingsHelper
Class FieldSettingsHelper.

Namespace

Drupal\ajax_comments

Code

public function getFieldFormatter(EntityDisplayInterface $view_display, $field_name, FieldDefinitionInterface $field_definition, $view_mode = 'default') {

  // 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;
  }
  else {

    // Get the formatter for the current comment field.

    /** @var \Drupal\Core\Field\FormatterInterface $comment_formatter */
    $comment_formatter = $this->fieldFormatterManager
      ->getInstance([
      'field_definition' => $field_definition,
      'view_mode' => $view_mode,
      'configuration' => $display_options,
    ]);
  }
  return $comment_formatter;
}