rng-3.x-dev/rng.post_update.php

rng.post_update.php
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
<?php
 
/**
 * @file
 * Post update functions for RNG.
 */
 
use Drupal\Core\Entity\EntityStorageException;
 
/**
 * The number of entities to process in each batch.
 *
 * @var int
 */
const RNG_UPDATE_BATCH_SIZE = 50;
 
/**
 * Resave all registrants to populate event links.
 *
 * Inspiration for this code was taken from
 * \Drupal\Core\Config\Entity\ConfigEntityUpdater::update().
 */
function rng_post_update_resave_registrants(&$sandbox = NULL) {
  $storage = \Drupal::entityTypeManager()->getStorage('registrant');
 
  if (!isset($sandbox['registrant_update'])) {
    $sandbox['registrant_update']['entities'] = $storage->getQuery()->accessCheck(FALSE)->execute();
    $sandbox['registrant_update']['count'] = count($sandbox['registrant_update']['entities']);
    $sandbox['registrant_update']['failures'] = [];
  }
 
  /** @var \Drupal\rng\Entity\RegistrantInterface $registrant */
  $registrants = $storage->loadMultiple(array_splice($sandbox['registrant_update']['entities'], 0, RNG_UPDATE_BATCH_SIZE));
  foreach ($registrants as $registrant) {
    try {
      $registrant->save();
    }
    catch (EntityStorageException $e) {
      $sandbox['registrant_update']['failures'][] = $registrant->id();
    }
  }
 
  $sandbox['#finished'] = empty($sandbox['registrant_update']['entities']) ? 1 : ($sandbox['registrant_update']['count'] - count($sandbox['registrant_update']['entities'])) / $sandbox['registrant_update']['count'];
 
  if ($sandbox['#finished'] === 1 && !empty($sandbox['registrant_update']['failures'])) {
    return t('Some registrants could not be resaved: @registrant_ids.', [
      '@registrant_ids' => implode(', ', $sandbox['registrant_update']['failures']),
    ]);
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc