TestFieldMultipleFormatter.php

Same filename in other branches
  1. 9 core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php
  2. 8.9.x core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php
  3. 11.x core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php

Namespace

Drupal\field_test\Plugin\Field\FieldFormatter

File

core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php

View source
<?php

namespace Drupal\field_test\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'field_test_multiple' formatter.
 */
class TestFieldMultipleFormatter extends FormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public static function defaultSettings() {
        return [
            'test_formatter_setting_multiple' => 'dummy test string',
            'alter' => FALSE,
        ] + parent::defaultSettings();
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsForm(array $form, FormStateInterface $form_state) {
        $element['test_formatter_setting_multiple'] = [
            '#title' => $this->t('Setting'),
            '#type' => 'textfield',
            '#size' => 20,
            '#default_value' => $this->getSetting('test_formatter_setting_multiple'),
            '#required' => TRUE,
        ];
        return $element;
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsSummary() {
        $summary = [];
        $summary[] = $this->t('@setting: @value', [
            '@setting' => 'test_formatter_setting_multiple',
            '@value' => $this->getSetting('test_formatter_setting_multiple'),
        ]);
        return $summary;
    }
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = [];
        if (!empty($items)) {
            $array = [];
            foreach ($items as $delta => $item) {
                $array[] = $delta . ':' . $item->value;
            }
            $elements[0] = [
                '#markup' => $this->getSetting('test_formatter_setting_multiple') . '|' . implode('|', $array),
            ];
        }
        return $elements;
    }

}

Classes

Title Deprecated Summary
TestFieldMultipleFormatter Plugin implementation of the 'field_test_multiple' formatter.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.