class SimpleTextFormatter

Same name in this branch
  1. 8.x-1.x field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_permission_example\Plugin\Field\FieldFormatter\SimpleTextFormatter
Same name in other branches
  1. 3.x modules/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_permission_example\Plugin\Field\FieldFormatter\SimpleTextFormatter
  2. 3.x modules/field_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_example\Plugin\Field\FieldFormatter\SimpleTextFormatter
  3. 4.0.x modules/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_permission_example\Plugin\Field\FieldFormatter\SimpleTextFormatter
  4. 4.0.x modules/field_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_example\Plugin\Field\FieldFormatter\SimpleTextFormatter

Plugin implementation of the 'field_example_simple_text' formatter.

Plugin annotation


@FieldFormatter(
  id = "field_example_simple_text",
  module = "field_example",
  label = @Translation("Simple text-based formatter"),
  field_types = {
    "field_example_rgb"
  }
)

Hierarchy

  • class \Drupal\field_example\Plugin\Field\FieldFormatter\SimpleTextFormatter extends \Drupal\Core\Field\FormatterBase

Expanded class hierarchy of SimpleTextFormatter

File

field_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php, line 20

Namespace

Drupal\field_example\Plugin\Field\FieldFormatter
View source
class SimpleTextFormatter extends FormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = [];
        foreach ($items as $delta => $item) {
            $elements[$delta] = [
                // We create a render array to produce the desired markup,
                // "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
                // See theme_html_tag().
'#type' => 'html_tag',
                '#tag' => 'p',
                '#attributes' => [
                    'style' => 'color: ' . $item->value,
                ],
                '#value' => $this->t('The color code in this field is @code', [
                    '@code' => $item->value,
                ]),
            ];
        }
        return $elements;
    }

}

Members

Title Sort descending Modifiers Object type Summary
SimpleTextFormatter::viewElements public function