abstract class FormatterBase

Base class for 'Field formatter' plugin implementations.

Hierarchy

Expanded class hierarchy of FormatterBase

18 files declare their use of FormatterBase
DefaultNumberFormatter.php in drupal/core/modules/field/modules/number/lib/Drupal/number/Plugin/field/formatter/DefaultNumberFormatter.php
Definition of Drupal\number\Plugin\field\formatter\DefaultNumberFormatter.
GenericFileFormatter.php in drupal/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php
Contains \Drupal\file\Plugin\field\formatter\GenericFileFormatter.
ImageFormatter.php in drupal/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php
Contains \Drupal\image\Plugin\field\formatter\ImageFormatter.
LegacyFormatter.php in drupal/core/modules/field/lib/Drupal/field/Plugin/field/formatter/LegacyFormatter.php
Definition of Drupal\field\Plugin\field\formatter\LegacyFormatter.
MailToFormatter.php in drupal/core/modules/field/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php
Definition of Drupal\email\Plugin\field\formatter\MailToFormatter.

... See full list

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php, line 18
Definition of Drupal\field\Plugin\Type\Formatter\FormatterBase.

Namespace

Drupal\field\Plugin\Type\Formatter
View source
abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface {

  /**
   * The field definition.
   *
   * @var array
   */
  protected $field;

  /**
   * The field instance definition.
   *
   * @var Drupal\field\FieldInstance
   */
  protected $instance;

  /**
   * The formatter settings.
   *
   * @var array
   */
  protected $settings;

  /**
   * The formatter weight.
   *
   * @var int
   */
  protected $weight;

  /**
   * The label display setting.
   *
   * @var string
   */
  protected $label;

  /**
   * The view mode.
   *
   * @var string
   */
  protected $viewMode;

  /**
   * Constructs a FormatterBase object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
   *   The Discovery class that holds access to the formatter implementation
   *   definition.
   * @param Drupal\field\FieldInstance $instance
   *   The field instance to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param int $weight
   *   The formatter weight.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   */
  public function __construct($plugin_id, DiscoveryInterface $discovery, $instance, array $settings, $weight, $label, $view_mode) {
    parent::__construct(array(), $plugin_id, $discovery);
    $this->instance = $instance;
    $this->field = field_info_field($instance['field_name']);
    $this->settings = $settings;
    $this->weight = $weight;
    $this->label = $label;
    $this->viewMode = $view_mode;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::view().
   */
  public function view(EntityInterface $entity, $langcode, array $items) {
    $field = $this->field;
    $instance = $this->instance;
    $addition = array();
    $elements = $this
      ->viewElements($entity, $langcode, $items);
    if ($elements) {
      $entity_type = $entity
        ->entityType();
      $info = array(
        '#theme' => 'field',
        '#weight' => $this->weight,
        '#title' => $instance['label'],
        '#access' => field_access('view', $field, $entity_type, $entity),
        '#label_display' => $this->label,
        '#view_mode' => $this->viewMode,
        '#language' => $langcode,
        '#field_name' => $field['field_name'],
        '#field_type' => $field['type'],
        '#field_translatable' => $field['translatable'],
        '#entity_type' => $entity_type,
        '#bundle' => $entity
          ->bundle(),
        '#object' => $entity,
        '#items' => $items,
        '#formatter' => $this
          ->getPluginId(),
      );
      $addition[$field['field_name']] = array_merge($info, $elements);
    }
    return $addition;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    return array();
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary().
   */
  public function settingsSummary() {
    return '';
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView().
   */
  public function prepareView(array $entities, $langcode, array &$items) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormatterBase::$field protected property The field definition.
FormatterBase::$instance protected property The field instance definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::$weight protected property The formatter weight.
FormatterBase::prepareView public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView(). Overrides FormatterInterface::prepareView 2
FormatterBase::settingsForm public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterInterface::settingsForm 8
FormatterBase::settingsSummary public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary(). Overrides FormatterInterface::settingsSummary 8
FormatterBase::view public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::view(). Overrides FormatterInterface::view
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct
FormatterInterface::viewElements public function Builds a renderable array for a field value. 16
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$discovery protected property The discovery object.
PluginBase::$plugin_id protected property The plugin_id.
PluginBase::getDefinition public function Implements Drupal\Component\Plugin\PluginInterface::getDefinition(). Overrides PluginInspectionInterface::getDefinition
PluginBase::getPluginId public function Implements Drupal\Component\Plugin\PluginInterface::getPluginId(). Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings