-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_promo_bar.module
86 lines (78 loc) · 2.73 KB
/
commerce_promo_bar.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* @file
* Provides a commerce promo bar entity type.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Render\Element;
use Drupal\commerce_promo_bar\Entity\PromoBarInterface;
/**
* Implements hook_theme().
*/
function commerce_promo_bar_theme() {
return [
'commerce_promo_bar' => [
'render element' => 'elements',
],
'commerce_promo_bar_block' => [
'variables' => [
'promo_bars' => [],
],
'template' => 'commerce-promo-bar-block',
],
'commerce_promo_bar_form' => [
'render element' => 'form',
],
];
}
/**
* Prepares variables for commerce promo bar templates.
*
* Default template: commerce-promo_bar.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the commerce promo bar
* information and any fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_commerce_promo_bar(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
/** @var \Drupal\commerce_promo_bar\Entity\PromoBarInterface $commerce_promo_bar */
$commerce_promo_bar = $variables['elements']['#commerce_promo_bar'];
$variables['promo_bar_entity'] = $commerce_promo_bar;
$variables['promo_bar'] = [];
foreach (Element::children($variables['elements']) as $key) {
$variables['promo_bar'][$key] = $variables['elements'][$key];
}
$variables['promo_bar']['dismissible'] = $commerce_promo_bar->isDismissible();
}
/**
* Implement template_preprocess_field().
*/
function commerce_promo_bar_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
// Replacing tokens from body with real values.
if (isset($element['#entity_type'], $element['#field_name'], $element['#object'])) {
if ($element['#entity_type'] === 'commerce_promo_bar' && $element['#field_name'] === 'body' && $element['#object'] instanceof PromoBarInterface) {
$token_service = \Drupal::token();
$delta = 0;
while (!empty($element[$delta])) {
$variables['items'][$delta]['content']['#text'] = $token_service->replace($variables['items'][$delta]['content']['#text'], [
'commerce_promo_bar' => $element['#object'],
]);
$delta++;
}
}
}
}
/**
* Implements hook_entity_base_field_info_alter().
*/
function commerce_promo_bar_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
if ($entity_type->id() === 'commerce_promo_bar') {
$config = \Drupal::config('commerce_promo_bar.settings');
$fields['text_color']['default_value'] = $config->get('text_color');
$fields['background_color']['default_value'] = $config->get('background_color');
}
}