Repository navigation
Expand file tree
/
Copy pathapi.php
More file actions
37 lines (37 loc) · 1.43 KB
/
Copy pathapi.php
File metadata and controls
37 lines (37 loc) · 1.43 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
<?php
// replace ids with names
$app->on("collections.find.after", function ($collectionName, &$entries, $isUpdate) use ($app) {
$collection = $app->module('collections')->collection($collectionName);
foreach ($collection['fields'] as $field) {
if ($field['type'] === 'specifications' && $field['options']['template_collection']) {
$templateEntries = $app->module('collections')->find($field['options']['template_collection']);
$templates = [];
foreach ($templateEntries as $templateEntry) {
$templates[$templateEntry['id']] = [
'name' => $templateEntry['name'],
'attributes' => $templateEntry['attributes']
];
}
unset($templateEntries);
$fieldName = $field['name'];
foreach ($entries as &$entry) {
if (isset($entry[$fieldName])) {
$template = $templates[$entry[$fieldName]['templateId']];
$values = $entry[$fieldName]['values'];
$specifications = [];
foreach ($template['attributes'] as $repeaterAttribute) {
$attribute = $repeaterAttribute['value'];
if ($values[$attribute['id']]) {
$specifications[] = [
'name' => $attribute['name'],
'value' => $values[$attribute['id']]
];
}
}
$entry[$fieldName . "_template"] = $template['name'];
$entry[$fieldName] = $specifications;
}
}
}
}
}, -10);