class TestFieldPrepareViewFormatter

Plugin implementation of the 'field_test_with_prepare_view' formatter.

Plugin annotation


@FieldFormatter(
  id = "field_test_with_prepare_view",
  module = "field_test",
  label = @Translation("With prepare step"),
  description = @Translation("Tests prepareView() method"),
  field_types = {
    "test_field"
  },
  settings = {
    "test_formatter_setting_additional" = "dummy test string"
  }
)

Hierarchy

Expanded class hierarchy of TestFieldPrepareViewFormatter

File

drupal/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/formatter/TestFieldPrepareViewFormatter.php, line 31
Contains \Drupal\field_test\Plugin\field\formatter\TestFieldPrepareViewFormatter.

Namespace

Drupal\field_test\Plugin\field\formatter
View source
class TestFieldPrepareViewFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state) {
    $element['test_formatter_setting_additional'] = array(
      '#title' => t('Setting'),
      '#type' => 'textfield',
      '#size' => 20,
      '#default_value' => $this
        ->getSetting('test_formatter_setting_additional'),
      '#required' => TRUE,
    );
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = array();
    $summary[] = t('@setting: @value', array(
      '@setting' => 'test_formatter_setting_additional',
      '@value' => $this
        ->getSetting('test_formatter_setting_additional'),
    ));
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareView(array $entities, $langcode, array &$items) {
    foreach ($items as $id => $item) {
      foreach ($item as $delta => $value) {

        // Don't add anything on empty values.
        if ($value) {
          $items[$id][$delta]['additional_formatter_value'] = $value['value'] + 1;
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(EntityInterface $entity, $langcode, array $items) {
    $elements = array();
    foreach ($items as $delta => $item) {
      $elements[$delta] = array(
        '#markup' => $this
          ->getSetting('test_formatter_setting_additional') . '|' . $item['value'] . '|' . $item['additional_formatter_value'],
      );
    }
    return $elements;
  }

}

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::view public function Builds a renderable array for one field on one entity instance. Overrides FormatterInterface::view
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. 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
TestFieldPrepareViewFormatter::prepareView public function Allows formatters to load information for field values being displayed. Overrides FormatterBase::prepareView
TestFieldPrepareViewFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
TestFieldPrepareViewFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
TestFieldPrepareViewFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements