Commit b927e194 authored by targoo's avatar targoo

WIP - apply new drupal 8 API

parent 1a569c87
<?php
/**
* @file
* Install, update and uninstall functions for the Color Field module.
*/
/**
* Implements hook_field_schema().
*/
function color_field_field_schema($field) {
$columns = array();
$columns['rgb'] = array(
'description' => 'The 7-character value',
'type' => 'varchar',
'length' => 7,
'not null' => FALSE,
);
$columns['alfa'] = array(
'description' => 'The alfa value',
'type' => 'float',
'size' => 'tiny',
'not null' => FALSE,
);
$indexes = array(
'rgb' => array('rgb'),
);
return array('columns' => $columns, 'indexes' => $indexes);
}
......@@ -2,32 +2,29 @@
/**
* @file
* Definition of Drupal\color_field\Plugin\field\formatter\MailToFormatter.
* Definition of Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldDefaultFormatter.
*/
namespace Drupal\color_field\Plugin\field\formatter;
namespace Drupal\color_field\Plugin\Field\FieldFormatter;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Plugin implementation of the 'color_field_hex' formatter.
* Plugin implementation of the 'color_field_default' formatter.
*
* @FieldFormatter(
* id = "color_field_hex",
* id = "color_field_default",
* module = "color_field",
* label = @Translation("Color Hex"),
* label = @Translation("Color Hex Formatter"),
* field_types = {
* "color_field"
* }
* )
*/
class ColorFieldHexFormatter extends FormatterBase {
class ColorFieldDefaultFormatter extends FormatterBase {
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
......
......@@ -2,18 +2,15 @@
/**
* @file
* Definition of Drupal\color_field\Plugin\field\formatter\MailToFormatter.
* Definition of Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldSwatchFormatter.
*/
namespace Drupal\color_field\Plugin\field\formatter;
namespace Drupal\color_field\Plugin\Field\FieldFormatter;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Field\FormatterBase;
/**
* Plugin implementation of the 'color_field_hex' formatter.
* Plugin implementation of the 'color_field_swatch' formatter.
*
* @FieldFormatter(
* id = "color_field_swatch",
......@@ -46,6 +43,9 @@ class ColorFieldSwatchFormatter extends FormatterBase {
return $summary;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$elements = parent::settingsForm($form, $form_state);
......@@ -68,7 +68,7 @@ class ColorFieldSwatchFormatter extends FormatterBase {
}
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
......
<?php
/**
* @file
* Contains \Drupal\color_field\Plugin\Field\FieldType\ColorFieldType.
*/
namespace Drupal\color_field\Plugin\Field\FieldType;
use Drupal\Core\Field\ConfigFieldItemBase;
use Drupal\field\FieldInterface;
/**
* Plugin implementation of the 'color_field' field type.
*
* @FieldType(
* id = "color_field",
* label = @Translation("Color Field"),
* description = @Translation("Create and store Color value."),
* default_widget = "color_field_default",
* default_formatter = "color_field_default"
* )
*/
class ColorFieldItem extends ConfigFieldItemBase {
/**
* {@inheritdoc}
*/
static $propertyDefinitions;
/**
* {@inheritdoc}
*/
public static function schema(FieldInterface $field) {
return array(
'columns' => array(
'rgb' => array(
'description' => 'The 7-character value',
'type' => 'varchar',
'length' => 7,
'not null' => FALSE,
),
'alfa' => array(
'description' => 'The alfa value',
'type' => 'float',
'size' => 'tiny',
'not null' => FALSE,
),
),
'indexes' => array(
'rgb' => array('rgb'),
),
);
}
/**
* {@inheritdoc}
*/
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['rgb'] = array(
'type' => 'string',
'label' => t('HEX Color value'),
);
static::$propertyDefinitions['alfa'] = array(
'type' => 'float',
'label' => t('Alfa value'),
);
}
return static::$propertyDefinitions;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('rgb')->getValue();
return $value === NULL || $value === '';
}
}
......@@ -2,14 +2,13 @@
/**
* @file
* Contains \Drupal\color_field\Plugin\field\widget\ColorFieldDefaultWidget.
* Contains \Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldDefaultWidget.
*/
namespace Drupal\color_field\Plugin\field\widget;
namespace Drupal\color_field\Plugin\Field\FieldWidget;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Widget\WidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
/**
* Plugin implementation of the 'color_field_default' widget.
......@@ -17,7 +16,7 @@ use Drupal\field\Plugin\Type\Widget\WidgetBase;
* @Plugin(
* id = "color_field_default",
* module = "color_field",
* label = @Translation("Color field"),
* label = @Translation("Color field default"),
* field_types = {
* "color_field"
* },
......@@ -31,7 +30,7 @@ class ColorFieldDefaultWidget extends WidgetBase {
/**
* Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
*/
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
$element['rgb'] = $element + array(
'#type' => 'textfield',
'#maxlength' => 7,
......
<?php
/**
* @file
* Contains \Drupal\color_field\Type\ColorFieldItem.
*/
namespace Drupal\color_field\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'color_field' entity field item.
*/
class ColorFieldItem extends FieldItemBase {
/**
* Definitions of the contained properties.
*
* @see ColorFieldItem::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;
/**
* Implements ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['rgb'] = array(
'type' => 'string',
'label' => t('Color value'),
);
static::$propertyDefinitions['alfa'] = array(
'type' => 'float',
'label' => t('Alfa value'),
);
}
return static::$propertyDefinitions;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment