Commit c8688bf5 authored by targoo's avatar targoo

first color field field with simple widget and formatter

parent dbc76cf2
......@@ -27,7 +27,7 @@ function color_field_field_info() {
'label' => t('Color Field'),
'description' => t('Field using a hexadecimal notation.'),
'instance_settings' => array(
'title' => DRUPAL_OPTIONAL,
'alfa' => TRUE,
),
'default_widget' => 'color_field_default',
'default_formatter' => 'color_field_hex',
......@@ -47,15 +47,13 @@ function color_field_field_is_empty($item, $field) {
* Implements hook_field_instance_settings_form().
*/
function color_field_field_instance_settings_form($field, $instance) {
$form['title'] = array(
'#type' => 'radios',
'#title' => t('Allow link title'),
'#default_value' => isset($instance['settings']['title']) ? $instance['settings']['title'] : DRUPAL_OPTIONAL,
'#options' => array(
DRUPAL_DISABLED => t('Disabled'),
DRUPAL_OPTIONAL => t('Optional'),
DRUPAL_REQUIRED => t('Required'),
),
$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;
}
......@@ -63,7 +61,7 @@ function color_field_field_instance_settings_form($field, $instance) {
/**
* Implements hook_field_load().
*/
function color_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
/*function color_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => &$item) {
// Unserialize the attributes into an array. The value stored in the
......@@ -76,34 +74,34 @@ function color_field_field_load($entity_type, $entities, $field, $instances, $la
}
}
}
}
}*/
/**
* Implements hook_field_presave().
*/
function color_field_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
/*function color_field_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => &$item) {
// Trim any spaces around the rgb.
$item['rgb'] = trim($item['rgb']);
$item['title'] = trim($item['title']);
//$item['title'] = trim($item['title']);
}
}
}*/
/**
* Implements hook_theme().
*/
function color_field_theme() {
/*function color_field_theme() {
return array(
'color_field_formatter_color_field_separate' => array(
'variables' => array('title' => NULL, 'url_title' => NULL, 'href' => NULL, 'options' => array()),
),
);
}
}*/
/**
* Formats a link as separate title and URL elements.
*/
function theme_color_field_formatter_color_field_separate($vars) {
/*function theme_color_field_formatter_color_field_separate($vars) {
$output = '';
$output .= '<div class="link-item">';
if (!empty($vars['title'])) {
......@@ -112,7 +110,7 @@ function theme_color_field_formatter_color_field_separate($vars) {
$output .= '<div class="link-url">' . l($vars['url_title'], $vars['href'], $vars['options']) . '</div>';
$output .= '</div>';
return $output;
}
}*/
/**
* Helper: Convert RGB to HEX6
......
......@@ -2,12 +2,12 @@
/**
* @file
* Contains \Drupal\color_field\Plugin\field\formatter\ColorFieldHexFormatter.
* Definition of Drupal\color_field\Plugin\field\formatter\MailToFormatter.
*/
namespace Drupal\color_field\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,10 +15,10 @@ use Drupal\Core\Entity\EntityInterface;
/**
* Plugin implementation of the 'color_field_hex' formatter.
*
* @Plugin(
* id = "color_field_default",
* @FieldFormatter(
* id = "color_field_hex",
* module = "color_field",
* label = @Translation("Color HEX"),
* label = @Translation("Color Hex"),
* field_types = {
* "color_field"
* }
......@@ -33,11 +33,9 @@ class ColorFieldHexFormatter extends FormatterBase {
$elements = array();
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#type' => 'link',
'#title' => $item['value'],
'#href' => 'mailto:' . $item['value'],
);
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
$elements[$delta] = array('#markup' => $item['rgb']);
}
return $elements;
......
......@@ -32,21 +32,30 @@ 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) {
$element['value'] = $element + array(
$element['rgb'] = $element + array(
'#type' => 'textfield',
'#maxlength' => 6,
'#maxlength' => 7,
'#title' => t('Color Field'),
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
'#placeholder' => $this->getSetting('placeholder'),
'#default_value' => isset($items[$delta]['rgb']) ? $items[$delta]['rgb'] : NULL,
//'#placeholder' => $this->getSetting('placeholder'),
'#required' => $element['#required'],
);
/*$element['alfa'] = $element + array(
'#type' => 'textfield',
'#maxlength' => 7,
'#title' => t('Color Field'),
'#default_value' => isset($items[$delta]['alfa']) ? $items[$delta]['alfa'] : NULL,
//'#placeholder' => $this->getSetting('placeholder'),
'#required' => $element['#required'],
);*/
dpm($element);
return $element;
}
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().
*/
public function settingsForm(array $form, array &$form_state) {
/*public function settingsForm(array $form, array &$form_state) {
$element['placeholder'] = array(
'#type' => 'textfield',
'#title' => t('Placeholder'),
......@@ -54,6 +63,6 @@ class ColorFieldDefaultWidget extends WidgetBase {
'#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;
}
}*/
}
......@@ -29,11 +29,16 @@ class ColorFieldItem extends FieldItemBase {
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['value'] = array(
'type' => 'color_field',
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