class ColorBackgroudFormatter

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

Plugin implementation of the 'field_example_color_background' formatter.

Plugin annotation


@FieldFormatter(
  id = "field_example_color_background",
  label = @Translation("Change the background of the output text"),
  field_types = {
    "field_example_rgb"
  }
)

Hierarchy

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

Expanded class hierarchy of ColorBackgroudFormatter

File

field_example/src/Plugin/Field/FieldFormatter/ColorBackgroudFormatter.php, line 19

Namespace

Drupal\field_example\Plugin\Field\FieldFormatter
View source
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;
    }

}

Members

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