ColorBackgroudFormatter.php

Same filename in other branches
  1. 3.x modules/field_example/src/Plugin/Field/FieldFormatter/ColorBackgroudFormatter.php
  2. 4.0.x modules/field_example/src/Plugin/Field/FieldFormatter/ColorBackgroudFormatter.php

Namespace

Drupal\field_example\Plugin\Field\FieldFormatter

File

field_example/src/Plugin/Field/FieldFormatter/ColorBackgroudFormatter.php

View source
<?php

namespace Drupal\field_example\Plugin\Field\FieldFormatter;

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

/**
 * Plugin implementation of the 'field_example_color_background' formatter.
 *
 * @FieldFormatter(
 *   id = "field_example_color_background",
 *   label = @Translation("Change the background of the output text"),
 *   field_types = {
 *     "field_example_rgb"
 *   }
 * )
 */
class ColorBackgroudFormatter extends FormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = [];
        foreach ($items as $delta => $item) {
            $elements[$delta] = [
                '#type' => 'html_tag',
                '#tag' => 'p',
                '#value' => $this->t('The content area color has been changed to @code', [
                    '@code' => $item->value,
                ]),
                '#attributes' => [
                    'style' => 'background-color: ' . $item->value,
                ],
            ];
        }
        return $elements;
    }

}

Classes

Title Deprecated Summary
ColorBackgroudFormatter Plugin implementation of the 'field_example_color_background' formatter.