Commit 8b9f440e authored by targoo's avatar targoo

upgrade to support drupal 8 beta

parent 0e8fdc32
......@@ -55,3 +55,9 @@ Maintainer and developer: targoo
Development sponsored by Marique Calcus and written by Calcus David.
For professional support and development services contact targoo@gmail.com.
-----------------------------------------------------------------------------
More info
-----------------------------------------------------------------------------
http://www.w3.org/TR/css3-color/#color
https://github.com/mikeemoo/ColorJizz-PHP
......@@ -2,7 +2,6 @@ name: Color Field
type: module
description: 'Defines a color field type using a hexadecimal notation (HEX).'
core: 8.x
package: Core
version: VERSION
package: Field types
dependencies:
- field
......@@ -4,33 +4,34 @@
* An color field with a custom color picker using the Field Types API.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function color_field_help($path, $arg) {
switch ($path) {
case 'admin/help#color_field':
function color_field_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.color_field':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Color Field is simple field that use a hexadecimal notation (HEX) for the combination of Red, Green, and Blue color values (RGB).. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
$output .= '<p>' . t('Color Field is simple field that use a hexadecimal notation (HEX) for the combination of Red, Green, and Blue color values (RGB). See the <a href="!field">Field module help</a> and the <a href="!field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href="!link_documentation">online documentation for the Link module</a>.', array('!field' => \Drupal::url('help.page', array('name' => 'field')), '!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')), '!link_documentation' => 'https://drupal.org/documentation/modules/link')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Managing and displaying color fields') . '</dt>';
$output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the link field can be configured separately. See the <a href="!field_ui">Field UI help</a> for more information on how to manage fields and their display.', array('!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</dd>';
$output .= '<dt>' . t('Adding link text') . '</dt>';
$output .= '<dd>' . t('In the field settings you can define additional link text to be <em>optional</em> or <em>required</em> in any link field.') . '</dd>';
$output .= '<dt>' . t('Displaying link text') . '</dt>';
$output .= '<dd>' . t('If link text has been submitted for a URL, then by default this link text is displayed as a link to the URL. If you want to display both the link text <em>and</em> the URL, choose the appropriate link format from the drop-down menu in the <em>Manage display</em> page. If you only want to display the URL even if link text has been submitted, choose <em>Link</em> as the format, and then change its <em>Format settings</em> to display <em>URL only</em>.') . '</dd>';
$output .= '<dt>' . t('Adding attributes to links') . '</dt>';
$output .= '<dd>' . t('You can add attributes to links, by changing the <em>Format settings</em> in the <em>Manage display</em> page. Adding <em>rel="nofollow"</em> notifies search engines that links should not be followed.') . '</dd>';
$output .= '<dt>' . t('Validating URLs') . '</dt>';
$output .= '<dd>' . t('All links are validated after a link field is filled in. They can include anchors or query strings.') . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_field_instance_settings_form().
*/
function color_field_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$form['alfa'] = array(
'#type' => 'checkbox',
'#title' => t('Alfa'),
'#default_value' => $settings['alfa'],
'#description' => t('This allows authors to provide alfa value for the color'),
);
return $form;
}
/**
* Helper: Convert RGB to HEX6
*
......
# Schema for the configuration files of the Color Fied module.
# Widget.
entity_form_display.field.color_field_default:
type: entity_field_form_display_base
label: 'Telephone default format settings'
mapping:
settings:
type: mapping
label: 'Settings'
mapping:
placeholder_color:
type: label
label: 'Placeholder for the color property'
placeholder_opacity:
type: label
label: 'Placeholder for the opacity property'
# Formater.
entity_view_display.field.color_field_swatch:
type: entity_field_view_display_base
label: 'Telephone link format settings'
mapping:
settings:
type: mapping
label: 'Settings'
mapping:
title:
type: label
label: 'Title to replace basic numeric telephone number display'
# Formater.
entity_view_display.field.color_field_text:
type: entity_field_view_display_base
label: 'Telephone link format settings'
mapping:
settings:
type: mapping
label: 'Settings'
mapping:
title:
type: label
label: 'Title to replace basic numeric telephone number display'
<?php
/**
* @file
* Contains \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldDefaultFormatter.
*/
namespace Drupal\color_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Plugin implementation of the 'color_field_default' formatter.
*
* @FieldFormatter(
* id = "color_field_default",
* module = "color_field",
* label = @Translation("Default"),
* field_types = {
* "color_field"
* }
* )
*/
class ColorFieldDefaultFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$settings = $this->getFieldSettings();
$alfa = $this->getSetting('alfa');
foreach ($items as $delta => $item) {
$output = $item->value;
$elements[$delta] = array('#markup' => $output);
}
return $elements;
}
}
<?php
/**
* @file
* Contains Drupal\color_field\Color.
*/
namespace Drupal\color_field;
class Color {
/**
* The color.
*
* @var string
*/
protected $color;
/**
* The opacity.
*
* @var float
*/
protected $opacity;
/**
* Creates a Color instance.
*
* @param string $color The color.
* @param float $opacity The opacity.
*/
public function __construct($color, $opacity) {
$this->color = $color;
$this->opacity = $opacity;
}
/**
* {@inheritdoc}
*/
public function getColor() {
return $this->color;
}
/**
* {@inheritdoc}
*/
public function getOpacity() {
return $this->opacity;
}
/**
* Returns the string representation of the color (color, opacity).
*
* @return string
*/
public function __toString() {
return $this->color . ' ' . $this->opacity;
}
}
......@@ -2,13 +2,14 @@
/**
* @file
* Contains \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldSwatchFormatter.
* Contains Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldSwatchFormatter.
*/
namespace Drupal\color_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'color_field_swatch' formatter.
......@@ -19,10 +20,6 @@ use Drupal\Core\Field\FieldItemListInterface;
* label = @Translation("Color Swatch"),
* field_types = {
* "color_field",
* },
* settings = {
* "width" = "50",
* "height" = "50",
* }
* )
*/
......@@ -31,9 +28,17 @@ class ColorFieldSwatchFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$elements = parent::settingsForm($form, $form_state);
public static function defaultSettings() {
return array(
'width' => 50,
'height' => 50,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['width'] = array(
'#type' => 'number',
'#title' => t('Width'),
......@@ -60,7 +65,7 @@ class ColorFieldSwatchFormatter extends FormatterBase {
$settings = $this->getSettings();
$summary[] = t('Width: @width Height: @height px', array(
$summary[] = t('Width: @width Height: @height', array(
'@width' => $settings['width'],
'@height' => $settings['height']
));
......@@ -73,10 +78,20 @@ class ColorFieldSwatchFormatter extends FormatterBase {
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$settings = $this->getFieldSettings();
$settings = $this->getSettings();
$opacity = $this->getFieldSetting('opacity');
foreach ($items as $delta => $item) {
$elements[$delta] = array('#markup' => '<div style="background: ' . $item['rgb'] . '; width: ' . $settings['width'] . 'px; height: ' . $settings['height'] . 'px;"></div>');
$color = color_field_hex2rgb($item->color);
if ($opacity) {
$rgbtext = 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ',' . $item->opacity . ')';
}
else {
$rgbtext = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ')';
}
$elements[$delta] = array(
'#markup' => '<div style="background-color: ' . $rgbtext . '; width: ' . $settings['width'] . 'px; height: ' . $settings['height'] . 'px;"></div>',
);
}
return $elements;
......
<?php
/**
* @file
* Contains Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldTextFormatter.
*/
namespace Drupal\color_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'color_field_swatch' formatter.
*
* @FieldFormatter(
* id = "color_field_text",
* module = "color_field",
* label = @Translation("Color Text"),
* field_types = {
* "color_field",
* }
* )
*/
class ColorFieldTextFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'format' => 'hexadecimal',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['format'] = array(
'#type' => 'select',
'#title' => t('Format'),
'#options' => $this->getColorFormat(),
'#default_value' => $this->getSetting('format'),
);
return $elements;
}
public function getColorFormat($format = NULL) {
$formats = array();
$formats['hexadecimal'] = $this->t('Hexadecimal Colors');
$formats['rgb'] = $this->t('RGB Colors');
$formats['rgba'] = $this->t('RGBA Colors');
// $formats['hsl'] = $this->t('HSL Colors');
// $formats['hsla'] = $this->t('HSLA Colors');
if ($format) {
return $formats[$format];
}
return $formats;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$format = $this->getSetting('format');
$summary[] = t('Format: @format', array(
'@format' => $this->getColorFormat($format),
));
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$format = $this->getSetting('format');
$opacity = $this->getFieldSetting('opacity');
foreach ($items as $delta => $item) {
switch ($format) {
case 'hexadecimal':
$output = '#' . $item->color;
break;
case 'rgb':
$color = color_field_hex2rgb($item->color);
$output = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ')';
break;
case 'rgba':
$color = color_field_hex2rgb($item->color);
$item->opacity = ($opacity) ? $item->color : 1;
$output = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ',' . $item->color . ')';
break;
//case 'hsl':
// $output = 'hsl(120,100%,50%)';
// break;
//case 'hsla':
// $output = 'hsla(120,100%,50%,0.3)';
// break;
}
$elements[$delta] = array('#markup' => $output);
}
return $elements;
}
}
......@@ -2,53 +2,51 @@
/**
* @file
* Contains \Drupal\color_field\Plugin\Field\FieldType\ColorFieldItem.
* Contains Drupal\color_field\Plugin\Field\FieldType\ColorFieldItem.
*/
namespace Drupal\color_field\Plugin\Field\FieldType;
use Drupal\Core\Field\ConfigFieldItemBase;
use Drupal\field\FieldInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'color_field' field type.
*
* @FieldType(
* id = "color_field",
* label = @Translation("Color Field"),
* label = @Translation("Color"),
* description = @Translation("Create and store Color value."),
* default_widget = "color_field_default",
* default_formatter = "color_field_default"
* default_formatter = "color_field_text"
* )
*/
class ColorFieldItem extends ConfigFieldItemBase {
class ColorFieldItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
static $propertyDefinitions;
/**
* {@inheritdoc}
*/
public static function schema(FieldInterface $field) {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'rgb' => array(
'description' => 'The 7-character value',
'color' => array(
'description' => 'The RGB hex values',
'type' => 'varchar',
'length' => 7,
'not null' => FALSE,
),
'alfa' => array(
'description' => 'The alfa value',
'opacity' => array(
'description' => 'The opacity/alphavalue property',
'type' => 'float',
'size' => 'tiny',
'not null' => FALSE,
),
),
'indexes' => array(
'rgb' => array('rgb'),
'color' => array('color'),
),
);
}
......@@ -56,41 +54,44 @@ class ColorFieldItem extends ConfigFieldItemBase {
/**
* {@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;
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['color'] = DataDefinition::create('string')
->setLabel(t('Color'));
$properties['opacity'] = DataDefinition::create('float')
->setLabel(t('Opacity'));
return $properties;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('rgb')->getValue();
$value = $this->get('color')->getValue();
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state, $has_data) {
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = array();
$element['alfa'] = array(
$element['opacity'] = array(
'#type' => 'checkbox',
'#title' => t('Record Alfa'),
'#default_value' => $this->getFieldSetting('alfa'),
'#title' => t('Record opacity'),
'#default_value' => $this->getSetting('opacity'),
);
return $element;
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
return '';
}
}
......@@ -2,13 +2,14 @@
/**
* @file
* Contains \Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldDefaultWidget.
* Contains Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldDefaultWidget.
*/
namespace Drupal\color_field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'color_field_default' widget.
......@@ -19,9 +20,6 @@ use Drupal\Core\Field\WidgetBase;
* label = @Translation("Color field default"),
* field_types = {
* "color_field"
* },
* settings = {
* "placeholder" = ""
* }
* )
*/
......@@ -30,11 +28,27 @@ class ColorFieldDefaultWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$element['placeholder'] = array(
public static function defaultSettings() {
return array(
'placeholder_color' => '',
'placeholder_opacity' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['placeholder_color'] = array(
'#type' => 'textfield',
'#title' => t('Color placeholder'),
'#default_value' => $this->getSetting('placeholder_color'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
);
$element['placeholder_opacity'] = array(
'#type' => 'textfield',
'#title' => t('Placeholder'),
'#default_value' => $this->getSetting('placeholder'),
'#title' => t('Opacity placeholder'),
'#default_value' => $this->getSetting('placeholder_opacity'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
);
return $element;
......@@ -46,11 +60,18 @@ class ColorFieldDefaultWidget extends WidgetBase {
public function settingsSummary() {
$summary = array();
$placeholder = $this->getSetting('placeholder');
if (!empty($placeholder)) {
$summary[] = t('Placeholder: @placeholder', array('@placeholder' => $placeholder));
$placeholder_color = $this->getSetting('placeholder_color');
$placeholder_opacity = $this->getSetting('placeholder_opacity');
if (!empty($placeholder_color)) {
$summary[] = t('Color placeholder: @placeholder_color', array('@placeholder_color' => $placeholder_color));
}
if (!empty($placeholder_opacity)) {
$summary[] = t('Opacity placeholder: @placeholder_opacity', array('@placeholder_opacity' => $placeholder_opacity));
}
else {
if (empty($summary)) {
$summary[] = t('No placeholder');
}
......@@ -60,28 +81,30 @@ class ColorFieldDefaultWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
$element['rgb'] = array(
'#title' => t('Color Field'),
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['color'] = array(
'#title' => t('Color'),
'#type' => 'textfield',
'#maxlength' => 7,
'#size' => 7,
'#maxlength' => 6,
'#size' => 6,
'#required' => $element['#required'],
'#default_value' => isset($items[$delta]->rgb) ? $items[$delta]->rgb : NULL,
'#default_value' => isset($items[$delta]->color) ? $items[$delta]->color : NULL,
);
if ($this->getFieldSetting('alfa')) {
$element['rgb']['#prefix'] = '<div class="container-inline">';
$element['alfa'] = array(
'#title' => t('Alfa'),
if ($this->getFieldSetting('opacity')) {
$element['color']['#prefix'] = '<div class="container-inline">';
$element['opacity'] = array(
'#title' => t('Opacity'),
'#type' => 'textfield',
'#maxlength' => 3,
'#size' => 3,
'#required' => $element['#required'],
'#default_value' => isset($items[$delta]->alfa) ? $items[$delta]->alfa : NULL,
'#default_value' => isset($items[$delta]->opacity) ? $items[$delta]->opacity : NULL,
'#suffix' => '</div>',
);
}
return $element;
}
......
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