View source
<?php
namespace Drupal\colorapi\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class ColorItem extends FieldItemBase implements ColorItemInterface {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'name' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
'color' => [
'type' => 'varchar',
'length' => 7,
'not null' => FALSE,
],
],
];
}
public function isEmpty() {
$hex_value = $this
->getHexadecimal();
return $hex_value === NULL || $hex_value === '' || $hex_value === FALSE;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$typed_data_manager = \Drupal::typedDataManager();
$string_data_type = 'string';
$string_definition_info = $typed_data_manager
->getDefinition($string_data_type);
$properties['name'] = $string_definition_info['definition_class']::create($string_data_type)
->setLabel(t('Name'))
->setDescription(t('The human readable name of the color'));
$color_data_type = 'colorapi_color';
$color_definition_info = $typed_data_manager
->getDefinition($color_data_type);
$properties['color'] = $color_definition_info['definition_class']::create($color_data_type)
->setLabel(t('Color'))
->setDescription(t('The color, in hexadecimal and RGB format.'))
->setRequired(TRUE);
return $properties;
}
public function setValue($values, $notify = TRUE) {
$values['name'] = isset($values['name']) ? $values['name'] : '';
$this
->setName($values['name'], $notify);
$color_string = isset($values['color']['hexadecimal']) ? $values['color']['hexadecimal'] : $values['color'];
$this
->setHexadecimal($color_string, $notify);
$service = \Drupal::service('colorapi.service');
$this
->setRed($service
->hexToRgb($color_string, 'red'), $notify);
$this
->setGreen($service
->hexToRgb($color_string, 'green'), $notify);
$this
->setBlue($service
->hexToRgb($color_string, 'blue'), $notify);
}
public function setName($name, $notify = TRUE) {
$this
->get('name')
->setValue($name, $notify);
}
public function getColorName() {
return $this
->get('name')
->getValue();
}
public function setColor(array $color, $notify = TRUE) {
$this
->get('color')
->setValue($color, $notify);
}
public function getColor() {
return $this
->get('color')
->getValue();
}
public function setHexadecimal($color, $notify = TRUE) {
$this
->get('color')
->setHexadecimal($color, $notify);
}
public function getHexadecimal() {
return $this
->get('color')
->getHexadecimal();
}
public function setRgb(array $rgb, $notify = TRUE) {
$this
->get('color')
->setRgb($rgb, $notify);
}
public function getRgb() {
return $this
->get('color')
->getRgb();
}
public function setRed($red, $notify = TRUE) {
$this
->get('color')
->setRed($red, $notify);
}
public function getRed() {
return $this
->get('color')
->getRed();
}
public function setGreen($green, $notify = TRUE) {
$this
->get('color')
->setGreen($green, $notify);
}
public function getGreen() {
return $this
->get('color')
->getGreen();
}
public function setBlue($blue, $notify = TRUE) {
$this
->get('color')
->setBlue($blue, $notify);
}
public function getBlue() {
return $this
->get('color')
->getBlue();
}
}