Skip to content

Instantly share code, notes, and snippets.

@jorislucius
Last active November 12, 2020 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorislucius/1ff12127123f577be9d2b5be72b52171 to your computer and use it in GitHub Desktop.
Save jorislucius/1ff12127123f577be9d2b5be72b52171 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_preprocess_HOOK()
*/
function YOURTHEME_preprocess_paragraph(&$variables) {
// Populate color templates vars, based on chosen color.
$paragraph = $variables['paragraph'];
if(!empty($paragraph->field_bg_color)) {
$bg_color = $paragraph->field_bg_color->color;
if (!empty($bg_color)) {
$variables['bg_color'] = _get_color_class_suffix($bg_color);
}
}
if(!empty($paragraph->field_text_color)) {
$text_color = $paragraph->field_text_color->color;
if (!empty($text_color)) {
$variables['text_color'] = _get_color_class_suffix($text_color);
}
}
}
/**
* Helper function to provide usable classes,
* transformed from html color codes provided by 'field_color' module .
* @param $color_code
* @return string
*/
function _get_color_class_suffix($color_code){
switch ($color_code) {
case '#333333':
$color_class_suffix = 'dark-grey';
break;
case '#F9F9F9':
$color_class_suffix = 'light-grey';
break;
case '#E8AD00':
$color_class_suffix = 'dark-yellow';
break;
case '#5C2483':
$color_class_suffix = 'dark-purple';
break;
case '#EA560D':
$color_class_suffix = 'dark-orange';
break;
case '#404D23':
$color_class_suffix = 'dark-green';
break;
case '#96A499':
$color_class_suffix = 'light-green';
break;
case '#FFFFFF':
default:
$color_class_suffix = 'white';
break;
}
return $color_class_suffix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment