TestFieldApplicableFormatter.php

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

Namespace

Drupal\field_test\Plugin\Field\FieldFormatter

File

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

View source
<?php

namespace Drupal\field_test\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * Plugin implementation of the 'field_test_applicable' formatter.
 *
 * It is applicable to test_field fields unless their name is 'deny_applicable'.
 *
 * @FieldFormatter(
 *   id = "field_test_applicable",
 *   label = @Translation("Applicable"),
 *   description = @Translation("Applicable formatter"),
 *   field_types = {
 *     "test_field"
 *   },
 *   weight = 15,
 * )
 */
class TestFieldApplicableFormatter extends FormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public static function isApplicable(FieldDefinitionInterface $field_definition) {
        return $field_definition->getName() != 'deny_applicable';
    }
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        return [
            '#markup' => 'Nothing to see here',
        ];
    }

}

Classes

Title Deprecated Summary
TestFieldApplicableFormatter Plugin implementation of the 'field_test_applicable' formatter.

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