-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generated_content.module
102 lines (88 loc) · 2.94 KB
/
generated_content.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @file
* Main functions.
*/
declare(strict_types=1);
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Utility\Error;
use Drupal\generated_content\GeneratedContentRepository;
use Psr\Log\LogLevel;
/**
* Implements hook_entity_delete().
*/
function generated_content_entity_delete(EntityInterface $entity): void {
try {
Database::getConnection()->delete('generated_content')
->condition('entity_type', $entity->getEntityTypeId())
->condition('bundle', $entity->bundle())
->condition('entity_id', $entity->id())
->execute();
}
catch (\Exception $exception) {
$logger = \Drupal::logger('generated_content');
$logger->log(LogLevel::ERROR, ERROR::DEFAULT_ERROR_MESSAGE, Error::decodeException($exception));
}
}
/**
* Implements hook_config_ignore_settings_alter().
*
* @phpstan-ignore-next-line
*/
function generated_content_config_ignore_settings_alter(array &$settings): void {
// Exclude module from being enabled or disabled.
$settings[] = 'core.extension:module.generated_content';
}
/**
* Implements hook_modules_installed().
*
* @phpstan-ignore-next-line
*/
function generated_content_modules_installed($modules, $is_syncing): void {
// Generate content when modules with hook implementations are enabled.
if (getenv('GENERATED_CONTENT_CREATE') && !$is_syncing) {
$repository = GeneratedContentRepository::getInstance();
$info = $repository->getInfo();
$generated_content_modules = array_map(function ($v) {
return $v['#module'];
}, $info);
if (array_intersect($modules, $generated_content_modules)) {
// Create generated content only if the repository is empty. This allows
// to avoid duplication of generated content on every run of the
// persistent operations.
if ($repository->isEmpty()) {
\Drupal::messenger()->addMessage(t('Started creation of generated content from modules: @modules.', [
'@modules' => implode(', ', $generated_content_modules),
]));
$items = [];
if (getenv('GENERATED_CONTENT_ITEMS')) {
$items = _generated_content_parse_cli_items(getenv('GENERATED_CONTENT_ITEMS'));
}
$repository->createEntities($items);
\Drupal::messenger()->addMessage(t('Finished creation of generated content'));
}
}
}
}
/**
* Parse items provided through CLI.
*
* @param string $items_string
* Items as a string to parse.
*
* @return array<mixed>
* Multidimensional array of items to process. The first level key is an
* entity type and the second is a bundle. Value is a boolean TRUE.
*/
function _generated_content_parse_cli_items(string $items_string): array {
$items = [];
$parsed = explode(',', $items_string);
foreach ($parsed as $item) {
$parts = array_values(array_filter(explode('-', trim($item))));
if (count($parts) == 2) {
$items[$parts[0]][$parts[1]] = TRUE;
}
}
return $items;
}