Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
40.62% |
13 / 32 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
DisplayBuilderHelpers | |
40.62% |
13 / 32 |
|
0.00% |
0 / 5 |
94.56 | |
0.00% |
0 / 1 |
findArrayReplaceSource | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
getFixtureData | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
6.60 | |||
getFixtureDataFromExtension | |
58.33% |
7 / 12 |
|
0.00% |
0 / 1 |
8.60 | |||
formatLog | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatTime | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder; |
6 | |
7 | use Drupal\Component\Serialization\Yaml; |
8 | use Drupal\Core\Datetime\DateFormatterInterface; |
9 | use Drupal\Core\Render\Markup; |
10 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
11 | |
12 | /** |
13 | * Helpers related class for Display builder. |
14 | */ |
15 | class DisplayBuilderHelpers { |
16 | |
17 | /** |
18 | * Multi-array search and replace parent. |
19 | * |
20 | * @param array $array |
21 | * The array to search in. |
22 | * @param array $search |
23 | * The key value to replace. |
24 | * @param mixed $new_value |
25 | * The new value to set. |
26 | */ |
27 | public static function findArrayReplaceSource(array &$array, array $search, mixed $new_value): void { |
28 | foreach ($array as $key => $value) { |
29 | if (\is_array($value) && \is_array($array[$key])) { |
30 | self::findArrayReplaceSource($array[$key], $search, $new_value); |
31 | } |
32 | elseif ([$key => $value] === $search) { |
33 | $array['source'] = $new_value; |
34 | } |
35 | } |
36 | } |
37 | |
38 | /** |
39 | * Load YAML data if found in fixtures folder. |
40 | * |
41 | * @param array $filepaths |
42 | * The fixture file paths. |
43 | * @param bool $extension |
44 | * (Optional) The filepath include extension. Default FALSE. |
45 | * |
46 | * @return array |
47 | * The file content. |
48 | */ |
49 | public static function getFixtureData(array $filepaths, bool $extension = FALSE): array { |
50 | foreach ($filepaths as $filepath) { |
51 | if (!$extension) { |
52 | $filepath = $filepath . '.yml'; |
53 | } |
54 | |
55 | if (!\file_exists($filepath)) { |
56 | continue; |
57 | } |
58 | |
59 | $content = \file_get_contents($filepath); |
60 | |
61 | if (!$content) { |
62 | continue; |
63 | } |
64 | |
65 | return Yaml::decode($content); |
66 | } |
67 | |
68 | return []; |
69 | } |
70 | |
71 | /** |
72 | * Load YAML data from fixtures folder for current theme. |
73 | * |
74 | * @param string $name |
75 | * The extension name. |
76 | * @param string|null $fixture_id |
77 | * (Optional) The fixture file name. |
78 | * |
79 | * @return array |
80 | * The file content. |
81 | */ |
82 | public static function getFixtureDataFromExtension(string $name, ?string $fixture_id = NULL): array { |
83 | $path = NULL; |
84 | |
85 | try { |
86 | $path = \Drupal::moduleHandler()->getModule($name)->getPath(); |
87 | } |
88 | // @phpcs:ignore SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch.NonCapturingCatchRequired |
89 | catch (\Throwable $th) { |
90 | } |
91 | |
92 | if (!$path) { |
93 | try { |
94 | $path = \Drupal::service('theme_handler')->getTheme($name)->getPath(); |
95 | } |
96 | // @phpcs:ignore SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch.NonCapturingCatchRequired |
97 | catch (\Throwable $th) { |
98 | } |
99 | } |
100 | |
101 | if (!$path) { |
102 | return []; |
103 | } |
104 | |
105 | $filepath = \sprintf('%s/%s/fixtures/%s.yml', DRUPAL_ROOT, $path, $fixture_id); |
106 | |
107 | if (!\file_exists($filepath)) { |
108 | return []; |
109 | } |
110 | |
111 | return self::getFixtureData([$filepath], TRUE); |
112 | } |
113 | |
114 | /** |
115 | * Format the log. |
116 | * |
117 | * @param \Drupal\Core\StringTranslation\TranslatableMarkup $log |
118 | * The log to format. |
119 | * |
120 | * @return array |
121 | * The formatted log. |
122 | */ |
123 | public static function formatLog(TranslatableMarkup $log): array { |
124 | return ['#markup' => Markup::create($log->render())]; |
125 | } |
126 | |
127 | /** |
128 | * Print the date for humans. |
129 | * |
130 | * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter |
131 | * The date formatter service. |
132 | * @param int $timestamp |
133 | * The timestamp integer. |
134 | * |
135 | * @return string |
136 | * The formatted date. |
137 | */ |
138 | public static function formatTime(DateFormatterInterface $dateFormatter, int $timestamp): string { |
139 | $delta = \time() - $timestamp; |
140 | |
141 | if ($delta < 86400) { |
142 | return $dateFormatter->format($timestamp, 'custom', 'G:i'); |
143 | } |
144 | |
145 | return $dateFormatter->format($timestamp, 'short'); |
146 | } |
147 | |
148 | } |