-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdropai.module
113 lines (96 loc) · 2.65 KB
/
dropai.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
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @file
* Primary module hooks for dropai module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\dropai\Traits\DropaiEmbeddingTrait;
/**
* Implements hook_entity_type_build().
*/
function dropai_entity_type_build(array &$entity_types) {
// Iterate through all entity types to add the AI Inspector link template that
// follows the pattern: /dropai/node/{{nid}}/inspect.
foreach ($entity_types as $name => $type) {
$type->setLinkTemplate('dropai-inspect', "/dropai/$name/{{$name}}/inspect");
}
}
/**
* Implements hook_entity_insert().
*/
function dropai_entity_insert(EntityInterface $entity) {
dropai_add_item_to_queue_worker($entity, 'insert');
}
/**
* Implements hook_entity_update().
*/
function dropai_entity_update(EntityInterface $entity) {
dropai_add_item_to_queue_worker($entity, 'update');
}
/**
* Implements hook_entity_update().
*/
function dropai_entity_delete(EntityInterface $entity) {
dropai_add_item_to_queue_worker($entity, 'remove');
}
/**
* This function adds an item to the queue worker.
*/
function dropai_add_item_to_queue_worker(EntityInterface $entity, string $action) {
$entity_id = $entity->id();
$entity_type = $entity->getEntityTypeId();
$entity_bundle = $entity->bundle();
$checker = new class {
use DropaiEmbeddingTrait;
};
// Check by entity type and bundle if the entity allows indexing.
if (!$checker->dropai_check_entity_allows_indexing($entity_type, $entity_bundle)) {
return;
}
// Generates the queue data.
$item = [
'entity_id' => $entity_id,
'entity_type' => $entity_type,
'entity_bundle' => $entity_bundle,
'action' => $action,
];
// Check if the entity is already in the queue.
if (dropai_check_data_exist_in_queue('dropai_embedding_queue_worker', $item)) {
return;
}
// Insert the data into the queue.
$queue = \Drupal::service('queue')->get('dropai_embedding_queue_worker');
$queue->createItem($item);
}
/**
* This function checks if the data exists in the queue.
*/
function dropai_check_data_exist_in_queue(string $queueWorkerName, array $data) {
$data = serialize($data);
$database = \Drupal::database();
$query = $database->select('queue', 'q')
->fields('q', ['data'])
->condition('q.name', $queueWorkerName)
->condition('q.data', $data)
->range(0, 1);
$result = $query->execute()->fetchField();
return !empty($result);
}
/**
* Implements hook_theme().
*/
function dropai_theme() {
return [
'code_block' => [
'variables' => [
'language' => 'plaintext',
'code' => NULL,
],
],
'array_block' => [
'variables' => [
'items' => [],
],
],
];
}