TestFieldEmptySettingFormatter.php

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

Namespace

Drupal\field_test\Plugin\Field\FieldFormatter

File

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

View source
<?php

namespace Drupal\field_test\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'field_empty_setting' formatter.
 *
 * @FieldFormatter(
 *   id = "field_empty_setting",
 *   label = @Translation("Field empty setting"),
 *   field_types = {
 *     "test_field",
 *   },
 *   weight = -1
 * )
 */
class TestFieldEmptySettingFormatter extends FormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public static function defaultSettings() {
        return [
            'field_empty_setting' => '',
        ] + parent::defaultSettings();
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsForm(array $form, FormStateInterface $form_state) {
        $element['field_empty_setting'] = [
            '#title' => t('Setting'),
            '#type' => 'textfield',
            '#size' => 20,
            '#default_value' => $this->getSetting('field_empty_setting'),
            '#required' => TRUE,
        ];
        return $element;
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsSummary() {
        $summary = [];
        $setting = $this->getSetting('field_empty_setting');
        if (!empty($setting)) {
            $summary[] = t('Default empty setting now has a value.');
        }
        return $summary;
    }
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = [];
        if (!empty($items)) {
            foreach ($items as $delta => $item) {
                $elements[$delta] = [
                    '#markup' => $this->getSetting('field_empty_setting'),
                ];
            }
        }
        return $elements;
    }

}

Classes

Title Deprecated Summary
TestFieldEmptySettingFormatter Plugin implementation of the 'field_empty_setting' formatter.

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