drowl_paragraphs_bs-1.x-dev/modules/drowl_paragraphs_bs_type_score/drowl_paragraphs_bs_type_score.module
modules/drowl_paragraphs_bs_type_score/drowl_paragraphs_bs_type_score.module
<?php
/**
* @file
* DROWL paragraph type score.
*/
use Drupal\Core\Template\Attribute;
/**
* Prepared variables for paragraph output.
*/
function drowl_paragraphs_bs_type_score_preprocess_paragraph(&$variables) {
if ($variables['paragraph']->bundle() == 'score') {
$variables['score_attributes'] = new Attribute();
$variables['icon_attributes'] = new Attribute();
// Set Data Attributes for Score JS init (further ones are set using
// ui_styles beyond!)
$score_start_value = $variables['elements']['#paragraph']->get('field_score_start')->value;
if ($score_start_value === 0 || !empty($score_start_value)) {
$variables['score_attributes']->setAttribute('data-score-start', $score_start_value);
}
$score_end_value = $variables['elements']['#paragraph']->get('field_score_end')->value;
if (!empty($score_end_value)) {
$variables['score_attributes']->setAttribute('data-score-end', $score_end_value);
}
$score_max_value = $variables['elements']['#paragraph']->get('field_score_max')->value;
if (!empty($score_max_value)) {
$variables['score_attributes']->setAttribute('data-score-max', $score_max_value);
}
$score_prefix_value = $variables['elements']['#paragraph']->get('field_score_prefix')->value;
if (!empty($score_prefix_value)) {
$variables['score_attributes']->setAttribute('data-score-prefix', $score_prefix_value);
}
$score_suffix_value = $variables['elements']['#paragraph']->get('field_score_suffix')->value;
if (!empty($score_suffix_value)) {
$variables['score_attributes']->setAttribute('data-score-suffix', $score_suffix_value);
}
$score_max_prefix_value = $variables['elements']['#paragraph']->get('field_score_max_prefix')->value;
if (!empty($score_max_prefix_value)) {
$variables['score_attributes']->setAttribute('data-score-max-prefix', $score_max_prefix_value);
}
$score_max_suffix_value = $variables['elements']['#paragraph']->get('field_score_max_suffix')->value;
if (!empty($score_max_suffix_value)) {
$variables['score_attributes']->setAttribute('data-score-max-suffix', $score_max_suffix_value);
}
// Get score specific ui_styles so we can apply them to
// this specific wrappers, instead the paragraph wrapper.
$inline_styles = '';
if (isset($variables['attributes']['class'])) {
foreach ($variables['attributes']['class'] as $class) {
switch (TRUE) {
case str_starts_with($class, 'score--type-'):
$variables['score_attributes']->setAttribute('data-score-type', str_replace('score--type-', '', $class));
break;
case str_starts_with($class, 'score--value-display-'):
$variables['score_attributes']->setAttribute('data-score-value-display', str_replace('score--value-display-', '', $class));
break;
case str_starts_with($class, 'score--bar-color-'):
$variables['attributes']->removeClass($class);
$inline_styles .= ' --bar-color: var(--bs-' . str_replace('score--bar-color-', '', $class) . ');';
break;
case str_starts_with($class, 'score--bar-end-color-'):
$variables['attributes']->removeClass($class);
$inline_styles .= ' --bar-end-color: var(--bs-' . str_replace('score--bar-end-color-', '', $class) . ');';
break;
case str_starts_with($class, 'score--line-color-'):
$variables['attributes']->removeClass($class);
$inline_styles .= ' --line-color: var(--bs-' . str_replace('score--line-color-', '', $class) . ');';
break;
case str_starts_with($class, 'icon__ico'):
$variables['attributes']->removeClass($class);
$variables['icon_attributes']->addClass(str_replace('icon__', '', $class));
break;
case str_starts_with($class, 'icon__text'):
$variables['attributes']->removeClass($class);
$inline_styles .= ' --icon-color: var(--bs-' . str_replace('icon__text-', '', $class) . ');';
break;
case str_starts_with($class, 'score--'):
$variables['attributes']->removeClass($class);
$variables['score_attributes']->addClass($class);
break;
default:
// Do nothing.
break;
}
}
// Add accomulated style string.
if (!empty($inline_styles)) {
$variables['score_attributes']->setAttribute('style', $inline_styles);
}
}
}
}
/**
* Implements hook_theme().
*/
function drowl_paragraphs_bs_type_score_theme($existing, $type, $theme, $path) {
$templates = $path . '/templates';
return [
'paragraph__drowl_paragraphs_bs__score' => [
'base hook' => 'paragraph',
'path' => $templates,
],
];
}
