public function EntityDisplay::getFormatter

Returns the Formatter plugin for a field.

Parameters

string $field_name: The field name.

Return value

\Drupal\field\Plugin\Type\Formatter\FormatterInterface If the field is not hidden, the Formatter plugin to use for rendering it.

Overrides EntityDisplayInterface::getFormatter

File

drupal/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php, line 48
Contains \Drupal\entity\Plugin\Core\Entity\EntityDisplay.

Class

EntityDisplay
Configuration entity that contains display options for all components of a rendered entity in a given view mode.

Namespace

Drupal\entity\Plugin\Core\Entity

Code

public function getFormatter($field_name) {
  if (isset($this->plugins[$field_name])) {
    return $this->plugins[$field_name];
  }

  // Instantiate the formatter object from the stored display properties.
  if ($configuration = $this
    ->getComponent($field_name)) {
    $instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle);
    $formatter = $this->pluginManager
      ->getInstance(array(
      'instance' => $instance,
      'view_mode' => $this->originalMode,
      // No need to prepare, defaults have been merged in setComponent().
      'prepare' => FALSE,
      'configuration' => $configuration,
    ));
  }
  else {
    $formatter = NULL;
  }

  // Persist the formatter object.
  $this->plugins[$field_name] = $formatter;
  return $formatter;
}