-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkm_admin.module
More file actions
67 lines (58 loc) · 2.02 KB
/
km_admin.module
File metadata and controls
67 lines (58 loc) · 2.02 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
<?php
/**
* @file
* Functions used by KM Admin tools.
*/
use Drupal\file\Entity\File;
/**
* Batch operation to match images to their products: hundred at a time.
* This is the function that is called on each operation in image_match_batch.
*/
function image_match_op($file, &$context) {
// Process the file, create it in DB.
$imgFile = File::Create(['uri' => $file->uri]);
$imgFile->save();
// Get the products by their field_ean value.
$product_storage = \Drupal::entityTypeManager()->getStorage('commerce_product');
$query = \Drupal::entityQuery('commerce_product')
->condition('type', 'default')
->condition('field_ean', $file->name);
$pids = $query->execute();
$products = $product_storage->loadMultiple($pids);
$feedback = '';
// And set their imagefields
foreach ($products as $product) {
$product->field_image = $imgFile;
$product->save();
// ksm($product, 'product in ');
$feedback .= ' '. $product->id();
}
// Save some (for) feedback. Available as $results in the 'finished' function.
$context['results'][] = $file->name . " is opgeslagen bij $feedback." ;
}
/**
* Image Match Batch 'finished' callback.
*/
function image_match_batch_finished($success, $results, $operations) {
$messenger = \Drupal::messenger();
if ($success) {
// Here we could do something meaningful with the results.
// We just display the number of nodes we processed...
$messenger->addMessage(t('@count files processed.', ['@count' => count($results)]));
$messenger->addMessage(t('The final result was "%final"', ['%final' => end($results)]));
// ksm($results, 'debug eind resultaat');
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$messenger->addMessage(
t('An error occurred while processing @operation with arguments : @args',
[
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
]
)
);
}
}