Commit bd3ffe04 authored by M Parker's avatar M Parker

Allow overriding node, link colors in field formatter.

parent 81187c1e
......@@ -188,6 +188,65 @@ function d3_sankey_libraries_info_alter(&$libraries) {
. 'd3.sankey';
}
/* Form element validation callbacks. */
/**
* Form element validation callback: Validate a required list of colors.
*
* @param array $element
* The form element to validate.
* @param array $form_state
* The current state of the form.
*/
function d3_sankey_element_validate_required_color_list($element, &$form_state) {
$invalid_color_strings = array();
$value = $element['#value'];
// Parse the element value into a list of colors.
$colors = _d3_sankey_explode_csv_nsv_list((string) $value);
// Validate each color. If we find an invalid one, keep track of it so we can
// write a unified error message at the end.
foreach ($colors as $color) {
if (!_d3_sankey_color_string_is_valid((string) $color)) {
$invalid_color_strings[] = (string) $color;
}
}
// Set a form element error if we found any invalid color strings.
$num_invalid_color_strings = count($invalid_color_strings);
if ($num_invalid_color_strings > 0) {
$error_message = format_plural($num_invalid_color_strings,
'Invalid color string %colors.',
'Invalid color strings %colors.',
array(
'%colors' => implode(', ', $invalid_color_strings),
));
form_error($element, $error_message);
}
}
/**
* Form element validation callback: Validate an optional list of colors.
*
* @param array $element
* The form element to validate.
* @param array $form_state
* The current state of the form.
*/
function d3_sankey_element_validate_optional_color_list($element, &$form_state) {
$value = $element['#value'];
// Special case: if the value is empty, return without doing anything.
if (empty($value)) {
return;
}
// If we get here, the color list is not empty and should be validated
// normally.
return d3_sankey_element_validate_required_color_list($element, $form_state);
}
/* Helper functions. */
/**
......@@ -247,3 +306,94 @@ function _d3_sankey_js_library_path_prefix($reset = FALSE) {
return (string) $path;
}
/**
* Explode a string containing a list of comma- or newline-separated values.
*
* @param $input_string
* A string to parse.
*
* @return array
* An array of sub-strings.
*/
function _d3_sankey_explode_csv_nsv_list($input_string) {
$answer = array();
// Trim the input string.
$input_string = trim((string) $input_string);
// Separate everything that is comma-separated.
$initial_array = explode(',', $input_string);
// Handle the case when explode returns an array with one element (an empty
// string) if passed an empty string.
if (count($initial_array) === 1 && reset($initial_array) === '') {
$initial_array = array();
}
// Loop through the new array and separate everything that is newline-
// separated.
foreach ($initial_array as $substring) {
$sub_array = explode("\n", $substring);
// Handle the case when explode returns an array with one element (an empty
// string) if passed an empty string.
if (count($sub_array) === 1 && reset($sub_array) === '') {
$sub_array = array();
}
foreach ($sub_array as $item) {
$answer[] = (string) $item;
}
}
return $answer;
}
/**
* Validate that a string represents a valid color.
*
* Currently only supports hexidecimal strings.
*
* @param string $color
* The string to validate as a color.
*
* @return bool
* TRUE if the color is valid; FALSE otherwise.
*/
function _d3_sankey_color_string_is_valid($color) {
$is_valid = FALSE;
// Validate a hexidecimal color string. See color_valid_hexadecimal_string().
if (preg_match('/^#([a-f0-9]{3}){1,2}$/iD', $color) === 1) {
$is_valid = TRUE;
}
return $is_valid;
}
/**
* Modify a chart definition to include the contents of a color string.
*
* @param array &$chart
* The chart definition to modify.
* @param string $key
* The key to place the color(s) in the chart definition.
* @param string $colors_string
* The color string to parse.
*/
function _d3_sankey_add_colors_to_chart(&$chart, $key, $colors_string) {
$colors_array = _d3_sankey_explode_csv_nsv_list($colors_string);
$num_colors = count($colors_array);
// If we found one color, return it as a string.
if ($num_colors === 1) {
$chart[(string) $key] = (string) array_shift($colors_array);
}
// If we found more than one color, return them in an array.
elseif ($num_colors > 1) {
$chart[(string) $key] = $colors_array;
}
// If we get here, don't touch the chart array, because there were no colors.
}
......@@ -36,6 +36,8 @@ function d3_sankey_table_group_pp_field_formatter_info() {
'spread' => TRUE,
'iterations' => 5,
'alignLabel' => D3_SANKEY_ALIGNLABEL_AUTO,
'node_colors' => '',
'link_colors' => '',
),
);
......@@ -113,6 +115,24 @@ function d3_sankey_table_group_pp_field_formatter_settings_form($field, $instanc
'#options' => d3_sankey_options_alignlabel(),
'#default_value' => $settings['alignLabel'],
);
// Node colors.
$element['node_colors'] = array(
'#type' => 'textarea',
'#title' => t('Override node colors'),
'#description' => t('Enter a comma- or newline-separated list of colors to use for the nodes in the diagram.'),
'#default_value' => $settings['node_colors'],
'#element_validate' => array('d3_sankey_element_validate_optional_color_list'),
);
// Link colors.
$element['link_colors'] = array(
'#type' => 'textarea',
'#title' => t('Override link colors'),
'#description' => t('Enter a comma- or newline-separated list of colors to use for the links in the diagram.'),
'#default_value' => $settings['link_colors'],
'#element_validate' => array('d3_sankey_element_validate_optional_color_list'),
);
}
return $element;
......@@ -184,6 +204,10 @@ function d3_sankey_table_group_pp_field_formatter_view($entity_type, $entity, $f
'alignLabel' => (string) $settings['alignLabel'],
) + _d3_sankey_table_group_pp_default_chart_options();
// If the user chose any node or link colors, add them to the chart.
_d3_sankey_add_colors_to_chart($chart, 'node_colors', $settings['node_colors']);
_d3_sankey_add_colors_to_chart($chart, 'link_colors', $settings['link_colors']);
// Calculate an HTML ID and add that to the chart. Note this is required
// in order for the appropriate chart settings to be applied to the chart.
$id_parts = array(
......
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