Same name and namespace in other branches
  1. 8.x-2.x src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatchOptions.php \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatchOptions 1 comment

Plugin implementation of the color_field swatch formatter.

Plugin annotation


@FieldFormatter(
  id = "color_field_formatter_swatch_options",
  module = "color_field",
  label = @Translation("Color swatch options"),
  field_types = {
    "color_field_type"
  }
)

Hierarchy

  • class \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch extends \Drupal\Core\Field\FormatterBase
    • class \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatchOptions extends \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch

Expanded class hierarchy of ColorFieldFormatterSwatchOptions

File

src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatchOptions.php, line 25

Namespace

Drupal\color_field\Plugin\Field\FieldFormatter
View source
class ColorFieldFormatterSwatchOptions extends ColorFieldFormatterSwatch {
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) : array {
        $settings = $this->getSettings();
        $elements = [];
        $name = Html::getUniqueId("color-field");
        foreach ($items as $delta => $item) {
            $hex = $this->viewRawValue($item);
            $id = Html::getUniqueId("color-field-{$hex}");
            $elements[$delta] = [
                '#theme' => 'color_field_formatter_swatch_option',
                '#id' => $id,
                '#name' => $name,
                '#input_type' => $this->fieldDefinition
                    ->getFieldStorageDefinition()
                    ->isMultiple() ? 'checkbox' : 'radio',
                '#value' => $hex,
                '#shape' => $settings['shape'],
                '#height' => is_numeric($settings['height']) ? "{$settings['height']}px" : $settings['height'],
                '#width' => is_numeric($settings['width']) ? "{$settings['width']}px" : $settings['width'],
                '#color' => $this->viewValue($item),
                '#attributes' => new Attribute([
                    'class' => [
                        "color_field__swatch--{$settings['shape']}",
                    ],
                ]),
            ];
            if (!$settings['data_attribute']) {
                continue;
            }
            $elements[$delta]['#attributes']['data-color'] = $hex;
        }
        return $elements;
    }
    
    /**
     * Return the raw field value.
     *
     * @param \Drupal\color_field\Plugin\Field\FieldType\ColorFieldType $item
     *   The color field item.
     *
     * @return string
     *   The color hex value.
     */
    protected function viewRawValue(ColorFieldType $item) : string {
        return (new ColorHex($item->color, $item->opacity))
            ->toString(FALSE);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ColorFieldFormatterSwatch::defaultSettings public static function
ColorFieldFormatterSwatch::getShape protected function This is used to get the shape.
ColorFieldFormatterSwatch::settingsForm public function
ColorFieldFormatterSwatch::settingsSummary public function
ColorFieldFormatterSwatch::viewValue protected function View an individual field value.
ColorFieldFormatterSwatchOptions::viewElements public function Overrides ColorFieldFormatterSwatch::viewElements
ColorFieldFormatterSwatchOptions::viewRawValue protected function Return the raw field value.