Commit ab1290df authored by targoo's avatar targoo

add support for opacity for textfield and simple widget

parent ab1fdead
......@@ -8,7 +8,7 @@
/**
* The color plugin URI.
*/
define('COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI', 'http://recursive-design.com/projects/jquery-simple-color/jquery-simple-color.1.0.1.zip');
define('COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI', 'https://github.com/recurser/jquery-simple-color/archive/master.zip');
define('COLOR_FIELD_EYECON_COLOR_PICKER_DOWNLOAD_URI', 'http://www.eyecon.ro/colorpicker/colorpicker.zip');
define('COLOR_FIELD_DEMATTE_COLOR_PICKER_DOWNLOAD_URI', 'http://www.dematte.at/colorPicker/cP_v0.91.zip');
......@@ -62,8 +62,9 @@ function color_field_drush_help($section) {
/**
* Commands to download the Simple Color Picker plugin.
*/
function drush_color_field_color_fied_plugin_scp($path = 'sites/all/libraries') {
$filename = 'jquery-simple-color.1.0.1.zip';
function drush_color_field_plugin_scp($path = 'sites/all/libraries') {
$filename = 'master.zip';
$unzipfilename = 'jquery-simple-color-master';
$dirname = 'jquery-simple-color';
if (!drush_shell_exec('type unzip')) {
......@@ -96,13 +97,17 @@ function drush_color_field_color_fied_plugin_scp($path = 'sites/all/libraries')
drush_shell_exec('curl -kOL ' . COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI);
}
// If file has been downloaded.
if (is_file($filename)) {
// Remove old library.
if (is_dir($dirname)) {
drush_shell_exec('rm -rf ' . $dirname);
}
// Decompress the zip archive.
drush_shell_exec('unzip -qq -o ' . $filename);
drush_shell_exec('mv ' . $unzipfilename . ' ' . $dirname);
// Remove the zip archive.
drush_op('unlink', $filename);
}else {
drush_log(dt('Simple Color Picker plugin could not been downloaded from @url', array('@url' => COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI)), 'error');
}
// Set working directory back to the previous working directory.
......@@ -119,10 +124,9 @@ function drush_color_field_color_fied_plugin_scp($path = 'sites/all/libraries')
/**
* Commands to download the EyeCon Color Picker plugin.
*/
function drush_color_field_color_fied_plugin_ecp($path = 'sites/all/libraries') {
function drush_color_field_plugin_ecp($path = 'sites/all/libraries') {
$filename = 'colorpicker.zip';
$unzipfilename = 'cP_v0.91';
$dirname = 'eyecon_colorpicker';
$dirname = 'eyecon-color-picker';
if (!drush_shell_exec('type unzip')) {
return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
......@@ -154,12 +158,16 @@ function drush_color_field_color_fied_plugin_ecp($path = 'sites/all/libraries')
}
if (is_file($filename)) {
drush_shell_exec('mkdir ' . $dirname);
// Decompress the zip archive.
drush_shell_exec('unzip -qq -o ' . $filename);
// Remove old librairy.
// Remove the zip archive.
//drush_op('unlink', $filename);
drush_op('unlink', $filename);
drush_shell_exec('mv css ' . $dirname);
drush_shell_exec('mv images ' . $dirname);
drush_shell_exec('mv js ' . $dirname);
drush_shell_exec('mv index.html ' . $dirname);
}
// Set working directory back to the previous working directory.
......@@ -176,7 +184,7 @@ function drush_color_field_color_fied_plugin_ecp($path = 'sites/all/libraries')
/**
* Commands to download the Dematte Color Picker plugin.
*/
function drush_color_field_color_fied_plugin_dcp($path = 'sites/all/libraries') {
function drush_color_field_plugin_dcp($path = 'sites/all/libraries') {
$filename = 'cP_v0.91.zip';
$unzipfilename = 'cP_v0.91';
$dirname = 'dematte_colorpicker';
......
<?php
/**
* @file
* Field hooks to implement a color field.
*/
/**
* Implements hook_field_info().
*/
function color_field_field_info() {
return array(
'color_field_rgb' => array(
'label' => t('Color Field'),
'description' => t('Field using a hexadecimal notation.'),
// These settings apply to the color field field everywhere it is used.
'settings' => array(
'opacity' => FALSE,
),
// These settings apply to the color field instance.
'instance_settings' => array(
'default_colors' => '
#AC725E,#D06B64,#F83A22,#FA573C,#FF7537,#FFAD46
#42D692,#16A765,#7BD148,#B3DC6C,#FBE983
#92E1C0,#9FE1E7,#9FC6E7,#4986E7,#9A9CFF
#B99AFF,#C2C2C2,#CABDBF,#CCA6AC,#F691B2
#CD74E6,#A47AE2',
'cell_width' => 10,
'cell_height' => 10,
'cell_margin' => 1,
'box_width' => 115,
'box_height' => 20,
'columns' => 16,
),
'default_widget' => 'color_field_default_widget',
'default_formatter' => 'color_field_default_formatter',
// @todo Support hook_entity_property_info() from contrib "Entity API".
'property_type' => 'color_field_rgb',
),
);
}
/**
* Implements hook_field_validate().
*
* We want to verify that the items only contain RGB hex values like
* this: #RRGGBB. If the item validates, we do nothing. If it doesn't
* validate, we add our own error notification to the $errors parameter.
*
* @see color_field_widget_error()
*/
function color_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
// Test rgb field.
if (!empty($item['rgb'])) {
if (!preg_match('@^#[0-9a-fA-F]{6}$@', $item['rgb'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'color_field_invalid',
'message' => t('Color must be in the hexadecimal format #abcdef.'),
);
}
}
// Test opacity field.
if (!empty($item['opacity']) && filter_var(
$item['opacity'],
FILTER_VALIDATE_FLOAT,
array(
'options' => array(
'min_range' => 0,
'max_range' => 1
)
)
) === false) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'color_field_invalid',
'message' => t('Opacity need to be a float value'),
);
}
}
}
/**
* Implements hook_field_is_empty().
*/
function color_field_field_is_empty($item, $field) {
if (!is_array($item)) {
return FALSE;
}
if (empty($item['rgb'])) {
return TRUE;
}
elseif ($field['settings']['opacity'] && empty($item['opacity'])) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_widget_error().
*
* @see color_field_validate()
* @see form_error()
*/
function color_field_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'color_field_invalid':
form_error($element, $error['message']);
break;
}
}
/**
* Implements hook_field_insert().
*/
function color_field_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $value) {
if (!isset($items[$delta]['opacity'])) {
$items[$delta]['opacity'] = 1;
}
}
}
/**
* Implements hook_field_update().
*/
function color_field_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $value) {
if (!isset($items[$delta]['opacity'])) {
$items[$delta]['opacity'] = 1;
}
}
}
/**
* Implements hook_element_info().
*/
function color_field_element_info() {
$elements = array();
$elements['color_field_plain_text'] = array(
'#input' => TRUE,
'#theme' => 'color_field_plain_text',
'#theme_wrappers' => array('form_element'),
);
$elements['color_field_default_widget'] = array(
'#input' => TRUE,
'#theme' => 'color_field_default_widget',
'#theme_wrappers' => array('form_element'),
);
$elements['color_field_simple_widget'] = array(
'#input' => TRUE,
'#theme' => 'color_field_simple_widget',
'#theme_wrappers' => array('form_element'),
);
return $elements;
}
/**
* Implements hook_field_widget_info().
*
* Enable the different field widgets.
*
* @see color_field_field_settings_form()
* @see color_field_field_instance_settings_form
* @see color_field_field_widget_form()
*/
function color_field_field_widget_info() {
// If jquery-simple-color is enable so widget can be used.
$jquery_simple_color_picker_enable = TRUE;
$color_field_library_jquery_simple_color = drupal_get_library('color_field', 'jquery-simple-color');
foreach ($color_field_library_jquery_simple_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_simple_color_picker_enable = FALSE;
}
}
// If jquery_dematte_color_picker_enable is enable so widget can be used.
$jquery_dematte_color_picker_enable = TRUE;
$color_field_library_dematte_color = drupal_get_library('color_field', 'dematte-color-picker');
foreach ($color_field_library_dematte_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_dematte_color_picker_enable = FALSE;
}
}
// If jquery_eyecon_color_picker_enable is enable so widget can be used.
$jquery_eyecon_color_picker_enable = TRUE;
$color_field_library_eyecon_color = drupal_get_library('color_field', 'eyecon-color-picker');
foreach ($color_field_library_eyecon_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_eyecon_color_picker_enable = FALSE;
}
}
// If jquery_spectrum_color_picker_enable is enable so widget can be used.
$jquery_spectrum_color_picker_enable = TRUE;
$color_field_library_spectrum_color = drupal_get_library('color_field', 'bgrins-spectrum');
foreach ($color_field_library_spectrum_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_spectrum_color_picker_enable = FALSE;
}
}
foreach ($color_field_library_spectrum_color['css'] as $path => $js) {
if (!file_exists($path)) {
$jquery_spectrum_color_picker_enable = FALSE;
}
}
$widgets = array();
$widgets['color_field_default_widget'] = array(
'label' => t('Pre-selected Color Boxes'),
'field types' => array('color_field_rgb'),
);
// Color_field_farbtastic_widget need more work before release.
/*$widgets['color_field_farbtastic_widget'] = array(
'label' => t('Farbtastic Color-Picker'),
'field types' => array('color_field_rgb'),
);*/
// Check if color_field_simple_color can become color_field_simple_widget
if ($jquery_simple_color_picker_enable) {
$widgets['color_field_simple_widget'] = array(
'label' => t('Simple Color-Picker'),
'field types' => array('color_field_rgb'),
);
}
// Color_field_dematte_widget need more work before release.
/*if ($jquery_dematte_color_picker_enable) {
$widgets['color_field_dematte_widget'] = array(
'label' => t('Dematte Color-Picker'),
'field types' => array('color_field_rgb'),
);
}*/
// color_field_eyecon_widget need more work before release.
/*if ($jquery_eyecon_color_picker_enable) {
$widgets['color_field_eyecon_widget'] = array(
'label' => t('Eyecon Color-Picker'),
'field types' => array('color_field_rgb'),
);
}*/
// Dematte colorPicker.
if ($jquery_spectrum_color_picker_enable) {
$widgets['color_field_spectrum_widget'] = array(
'label' => t('Spectrum Color-Picker'),
'field types' => array('color_field_rgb'),
);
}
// Textfield.
$widgets['color_field_plain_text'] = array(
'label' => t('Plain text (RGB value as #ffffff)'),
'field types' => array('color_field_rgb'),
);
return $widgets;
}
/**
* Implements hook_field_settings_form().
*/
function color_field_field_settings_form($field, $instance, $has_data) {
module_load_include('inc', 'color_field', 'color_field_admin');
return _color_field_field_settings_form($field, $instance, $has_data);
}
/**
* Implements hook_field_instance_settings_form().
*/
function color_field_field_instance_settings_form($field, $instance) {
module_load_include('inc', 'color_field', 'color_field_admin');
return _color_field_field_instance_settings_form($field, $instance);
}
/**
* Implements hook_field_widget_form().
*/
function color_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
module_load_include('inc', 'color_field', 'color_field_admin');
return _color_field_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
}
/**
* Implements hook_field_formatter_info().
*
* @see color_field_field_formatter_view()
*/
function color_field_field_formatter_info() {
return array(
'color_field_default_formatter' => array(
'label' => t('Plain text color'),
'field types' => array('color_field_rgb'),
),
'color_field_css_declaration' => array(
'label' => t('CSS Declaration'),
'field types' => array('color_field_rgb'),
'settings' => array(
'selector' => 'body',
'property' => 'background-color',
'important' => TRUE,
),
),
'color_field_swatch' => array(
'label' => t('Color Swatch'),
'field types' => array('color_field_rgb'),
'settings' => array(
'width' => '50',
'height' => '50',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function color_field_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
switch ($display['type']) {
case 'color_field_css_declaration':
$element['selector'] = array(
'#title' => t('Selector'),
'#type' => 'textarea',
'#default_value' => $settings['selector'],
'#required' => TRUE,
'#description' => t('A valid CSS selector such as <code>.links > li > a, #logo</code>.'),
);
$element['token'] = array(
'#theme' => 'token_tree',
'#token_types' => array($instance['entity_type']),
'#dialog' => TRUE,
);
$element['property'] = array(
'#title' => t('Property'),
'#type' => 'select',
'#default_value' => $settings['property'],
'#required' => TRUE,
'#options' => array(
'background-color' => t('Background color'),
'color' => t('Text color'),
),
);
$element['important'] = array(
'#title' => t('Important'),
'#type' => 'checkbox',
'#default_value' => $settings['important'],
'#description' => t('Whenever this declaration is more important than others.'),
);
break;
case 'color_field_swatch':
$element['width'] = array(
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $settings['width'],
'#required' => TRUE,
);
$element['height'] = array(
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $settings['height'],
'#required' => TRUE,
);
break;
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function color_field_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
switch ($display['type']) {
case 'color_field_css_declaration':
$summary[] = t('CSS selector') . ': ' . $settings['selector'];
$summary[] = t('CSS property') . ': ' . $settings['property'];
$summary[] = t('!important declaration') . ': ' . (($settings['important']) ? t('Yes') : t('No'));
break;
case 'color_field_swatch':
$summary[] = t('Width') . ': ' . $settings['width'];
$summary[] = t('Height') . ': ' . $settings['height'];
break;
}
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*
* Three formatters are implemented.
* - color_field_default_formatter just outputs markup indicating the color that
* was entered and uses an inline style to set the text color to that value.
* - color_field_css_declaration does the same but also changes the
* background color or color of a region defined by the selector.
*
* @see color_field_formatter_info()
*/
function color_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'color_field_default_formatter':
foreach ($items as $delta => $item) {
$element[$delta]['#markup'] = $item['rgb'];
}
break;
case 'color_field_css_declaration':
foreach ($items as $delta => $item) {
$selector = token_replace($settings['selector'], array($entity_type => $entity), array('clear' => TRUE));
$important = ($settings['important']) ? ' !important' : '';
$inline_css = $settings['selector'] . '{' . $settings['property'] . ':' . $item['rgb'] . $important . '}';
$inline_css = $selector . '{ ' . $settings['property'] . ': ' . $item['rgb'] . $important . '; }';
drupal_add_css($inline_css, 'inline');
}
break;
case 'color_field_swatch':
foreach ($items as $delta => $item) {
$rgb = $item['rgb'];
$opacity = $item['opacity'];
$width = $settings['width'];
$height = $settings['height'];
$element[$delta] = array(
'#theme' => 'color_swatch',
'#color' => $rgb,
'#width' => $width,
'#height' => $height,
);
}
break;
}
return $element;
}
name = Color Field
description = Color Field using a hexadecimal notation (HEX)
description = Color Field storing a hexadecimal notation (HEX) and opacity
package = Fields
core = 7.x
files[] = color_field.test
dependencies[] = libraries
<?php
/**
* @file
* An color field with a custom color picker using the Field Types API.
*/
module_load_include('inc', 'color_field', 'color_field.field');
/**
* Implements hook_library().
*/
function color_field_library() {
$path = libraries_get_path('jquery-simple-color');
$libraries['jquery-simple-color'] = array(
'title' => 'color_field_jquery_simple_color',
'website' => 'http://recursive-design.com/projects/jquery-simple-color/',
'version' => '1.0.1',
'title' => 'recurser jquery simple color',
'website' => 'https://github.com/recurser/jquery-simple-color',
'version' => '1.2.1',
'js' => array(
$path . '/jquery.simple-color.min.js' => array(),
),
);
$path = libraries_get_path('dematte-color-picker');
$libraries['dematte-color-picker'] = array(
'title' => 'color_field_dematte_color_picker',
......@@ -26,6 +30,7 @@ function color_field_library() {
$path . '/colorPicker.js' => array(),
),
);
$path = libraries_get_path('bgrins-spectrum');
$libraries['bgrins-spectrum'] = array(
'title' => 'color_field_spectrum_color_picker',
......@@ -38,6 +43,7 @@ function color_field_library() {
$path . '/spectrum.css' => array(),
),
);
$path = libraries_get_path('eyecon-color-picker');
$libraries['eyecon-color-picker'] = array(
'title' => 'color_field_eyecon_color_picker',
......@@ -53,390 +59,6 @@ function color_field_library() {
return $libraries;
}
/**
* Implements hook_field_info().
*/
function color_field_field_info() {
return array(
'color_field_rgb' => array(
'label' => t('Color Field'),
'description' => t('Field using a hexadecimal notation.'),
'default_widget' => 'color_field_default_widget',
'default_formatter' => 'color_field_default_formatter',
'property_type' => 'color_field_rgb',
),
);
}
/**
* Implements hook_field_widget_info().
*
* @see color_field_field_widget_form()
*/
function color_field_field_widget_info() {
// If jquery-simple-color is enable so widget can be used.
$jquery_simple_color_picker_enable = TRUE;
$color_field_library_jquery_simple_color = drupal_get_library('color_field', 'jquery-simple-color');
foreach ($color_field_library_jquery_simple_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_simple_color_picker_enable = FALSE;
}
}
// If jquery_dematte_color_picker_enable is enable so widget can be used.
$jquery_dematte_color_picker_enable = TRUE;
$color_field_library_dematte_color = drupal_get_library('color_field', 'dematte-color-picker');
foreach ($color_field_library_dematte_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_dematte_color_picker_enable = FALSE;
}
}
// If jquery_eyecon_color_picker_enable is enable so widget can be used.
$jquery_eyecon_color_picker_enable = TRUE;
$color_field_library_eyecon_color = drupal_get_library('color_field', 'eyecon-color-picker');
foreach ($color_field_library_eyecon_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_eyecon_color_picker_enable = FALSE;
}
}
// If jquery_spectrum_color_picker_enable is enable so widget can be used.
$jquery_spectrum_color_picker_enable = TRUE;
$color_field_library_spectrum_color = drupal_get_library('color_field', 'bgrins-spectrum');
foreach ($color_field_library_spectrum_color['js'] as $path => $js) {
if (!file_exists($path)) {
$jquery_spectrum_color_picker_enable = FALSE;
}
}
foreach ($color_field_library_spectrum_color['css'] as $path => $js) {
if (!file_exists($path)) {
$jquery_spectrum_color_picker_enable = FALSE;
}
}
$widgets = array();
$widgets['color_field_default_widget'] = array(
'label' => t('Pre-selected Color Boxes'),
'field types' => array('color_field_rgb'),
);
// Color_field_farbtastic_widget need more work before release.
/*$widgets['color_field_farbtastic_widget'] = array(
'label' => t('Farbtastic Color-Picker'),
'field types' => array('color_field_rgb'),
);*/
// Check if color_field_simple_color can become color_field_simple_widget
if ($jquery_simple_color_picker_enable) {
$widgets['color_field_simple_widget'] = array(
'label' => t('Simple Color-Picker'),
'field types' => array('color_field_rgb'),
);
}
// Color_field_dematte_widget need more work before release.
/*if ($jquery_dematte_color_picker_enable) {
$widgets['color_field_dematte_widget'] = array(
'label' => t('Dematte Color-Picker'),
'field types' => array('color_field_rgb'),
);
}*/
// color_field_eyecon_widget need more work before release.
/*if ($jquery_eyecon_color_picker_enable) {
$widgets['color_field_eyecon_widget'] = array(
'label' => t('Eyecon Color-Picker'),
'field types' => array('color_field_rgb'),
);
}*/
// Color_field_eyecon_widget need more work before release.
if ($jquery_spectrum_color_picker_enable) {
$widgets['color_field_spectrum_widget'] = array(
'label' => t('Spectrum Color-Picker'),
'field types' => array('color_field_rgb'),
);
}
$widgets['color_field_plain_text'] = array(
'label' => t('Plain text (RGB value as #ffffff)'),
'field types' => array('color_field_rgb'),
);
return $widgets;
}
/**
* Implements hook_field_validate().
*
* We want to verify that the items only contain RGB hex values like
* this: #RRGGBB. If the item validates, we do nothing. If it doesn't
* validate, we add our own error notification to the $errors parameter.
*
* @see color_field_widget_error()
*/
function color_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['rgb'])) {
if (!preg_match('@^#[0-9a-fA-F]{6}$@', $item['rgb'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'color_field_invalid',
'message' => t('Color must be in the hexadecimal format #abcdef.'),
);
}
}
}
}
/**
* Implements hook_field_is_empty().
*/
function color_field_field_is_empty($item, $field) {
return empty($item['rgb']);
}
/**
* Implements hook_field_settings_form().
*
* Handle the parameters for a field.
*/
function color_field_field_settings_form($field, $instance, $has_data) {
$settings = $field['settings'];
$form = array();
switch ($instance['widget']['type']) {
case 'color_field_simple_color':
case 'color_field_simple_widget':