-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpatternkit_media_library.post_update.php
More file actions
76 lines (64 loc) · 2.28 KB
/
Copy pathpatternkit_media_library.post_update.php
File metadata and controls
76 lines (64 loc) · 2.28 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
<?php
/**
* @file
* Post update functions for Patternkit Media Library.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Populate missing alt text for image fields in pattern blocks.
*/
function patternkit_media_library_post_update_populate_image_alt_text(array &$sandbox): TranslatableMarkup {
$batch_size = 20;
/** @var \Drupal\Core\Entity\EntityStorageInterface $block_storage */
$block_storage = \Drupal::entityTypeManager()->getStorage('patternkit_block');
// Initialize sandbox on first run.
if (empty($sandbox)) {
$sandbox['progress'] = 0;
$sandbox['current_id'] = 0;
$sandbox['total'] = $block_storage->getQuery()
->accessCheck(FALSE)
->count()
->execute();
$sandbox['updated'] = 0;
$sandbox['skipped'] = 0;
$sandbox['failed'] = 0;
}
if ($sandbox['total'] > 0) {
/** @var \Drupal\patternkit_media_library\BlockAltTextUpdater $updater */
$updater = \Drupal::service('patternkit_media_library.block_alt_text_updater');
// Query for blocks in batches.
$ids = $block_storage->getQuery()
->accessCheck(FALSE)
->condition('id', $sandbox['current_id'], '>')
->sort('id')
->range(0, $batch_size)
->execute();
/** @var \Drupal\patternkit\Entity\PatternkitBlock $block */
foreach ($block_storage->loadMultiple($ids) as $block) {
$result = $updater->processBlock($block);
// Update statistics based on result.
if ($result['status'] === 'updated') {
$sandbox['updated']++;
}
elseif ($result['status'] === 'failed') {
$sandbox['failed']++;
}
else {
$sandbox['skipped']++;
}
$sandbox['current_id'] = $block->id();
$sandbox['progress']++;
}
}
// Calculate progress.
if ($sandbox['progress'] != $sandbox['total']) {
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['total'];
}
// Return progress message.
return t('Processed @progress of @total pattern blocks with @updates and @failures.', [
'@progress' => $sandbox['progress'],
'@total' => $sandbox['total'],
'@updates' => \Drupal::translation()->formatPlural($sandbox['updated'], '1 update', '@count updates'),
'@failures' => \Drupal::translation()->formatPlural($sandbox['failed'], '1 failure', '@count failures'),
]);
}