-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_content_strategy.module
More file actions
159 lines (147 loc) · 4.45 KB
/
ai_content_strategy.module
File metadata and controls
159 lines (147 loc) · 4.45 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @file
* AI Content Strategy module.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Gets translated button texts.
*
* @return array
* Array of translated button texts.
*/
function ai_content_strategy_get_button_texts() {
$button_texts = [
'main' => [
'generate' => t('Generate recommendations'),
'refresh' => t('Regenerate report'),
'loading' => t('Adding recommendations here...'),
'loading_more' => t('Adding more recommendations here...'),
],
'generate' => [],
'generate_more' => [],
'add_more' => [],
];
// Load enabled categories and generate button texts dynamically.
$category_storage = \Drupal::entityTypeManager()->getStorage('recommendation_category');
$category_ids = $category_storage->getQuery()
->condition('status', TRUE)
->accessCheck(FALSE)
->execute();
if ($category_ids) {
$categories = $category_storage->loadMultiple($category_ids);
foreach ($categories as $category) {
$category_id = $category->id();
$button_texts['generate'][$category_id] = t('Generate AI recommendations');
$button_texts['generate_more'][$category_id] = t('Generate more AI @type', ['@type' => t('ideas')]);
$button_texts['add_more'][$category_id] = t('Generate more AI recommendations');
}
}
return $button_texts;
}
/**
* Implements hook_theme().
*/
function ai_content_strategy_theme() {
return [
'ai_content_strategy_recommendations' => [
'variables' => [
'categories' => [],
'button_text' => [],
'last_run' => NULL,
'ai_configured' => TRUE,
],
],
'ai_content_strategy_recommendations_items' => [
'variables' => [
'items' => [],
'section' => NULL,
'button_text' => [],
],
],
'ai_content_strategy_icon' => [
'variables' => [
'name' => NULL,
'class' => NULL,
'size' => NULL,
],
'template' => 'components/icon',
],
'ai_content_strategy_link_display' => [
'variables' => [
'link' => NULL,
'section' => NULL,
'uuid' => NULL,
'idea_uuid' => NULL,
],
'template' => 'components/link-display',
],
'ai_content_strategy_link_add_button' => [
'variables' => [
'section' => NULL,
'uuid' => NULL,
'idea_uuid' => NULL,
],
'template' => 'components/link-add-button',
],
'ai_content_strategy_link_input' => [
'variables' => [
'current_link' => '',
],
'template' => 'components/link-input',
],
'ai_content_strategy_idea_row' => [
'variables' => [
'section' => NULL,
'uuid' => NULL,
'idea_uuid' => NULL,
'idea_text' => NULL,
'idea_implemented' => FALSE,
'idea_link' => '',
],
'template' => 'components/idea-row',
],
];
}
/**
* Implements hook_preprocess_HOOK().
*/
function ai_content_strategy_preprocess_ai_content_strategy_recommendations(&$variables) {
// Get translated button texts.
$variables['button_text'] = ai_content_strategy_get_button_texts();
// Get last run time from state if available.
$last_run = \Drupal::state()->get('ai_content_strategy.last_run');
if ($last_run) {
$variables['last_run'] = \Drupal::service('date.formatter')->formatTimeDiffSince($last_run);
}
// Add "Configure settings" link if user has permission.
$current_user = \Drupal::currentUser();
if ($current_user->hasPermission('administer ai content strategy')) {
$settings_url = Url::fromRoute('entity.recommendation_category.collection');
if ($settings_url->access()) {
$link = Link::fromTextAndUrl(t('Configure categories'), $settings_url);
$link = $link->toRenderable();
$link['#attributes']['class'][] = 'button';
$link['#attributes']['class'][] = 'button--small';
$link['#attributes']['class'][] = 'button--primary';
$variables['configure_link'] = [
'#type' => 'container',
'#attributes' => ['class' => ['form-actions', 'configure-actions']],
'#weight' => -10,
'link' => $link,
];
}
}
// Add JavaScript settings (icons now use CSS mask-image).
$variables['#attached']['drupalSettings']['aiContentStrategy'] = [
'buttonText' => $variables['button_text'],
'translations' => [
'addLink' => t('+ Add link'),
'editLink' => t('Edit link'),
'save' => t('Save'),
'cancel' => t('Cancel'),
'enterUrl' => t('Enter URL...'),
],
];
}