class TestFieldPrepareViewFormatter

Plugin implementation of the 'field_test_with_prepare_view' formatter.

Plugin annotation


@Plugin(
  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
Definition of Drupal\field_test\Plugin\field\formatter\TestFieldPrepareViewFormatter.

Namespace

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

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  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;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsSummary() {
    return t('@setting: @value', array(
      '@setting' => 'test_formatter_setting_additional',
      '@value' => $this
        ->getSetting('test_formatter_setting_additional'),
    ));
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView().
   */
  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;
        }
      }
    }
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
   */
  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::$weight protected property The formatter weight.
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
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
TestFieldPrepareViewFormatter::prepareView public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView(). Overrides FormatterBase::prepareView
TestFieldPrepareViewFormatter::settingsForm public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterBase::settingsForm
TestFieldPrepareViewFormatter::settingsSummary public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterBase::settingsSummary
TestFieldPrepareViewFormatter::viewElements public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). Overrides FormatterInterface::viewElements