5 * Install, update and uninstall functions for the Crop API module.
8 use Drupal\crop\Entity\Crop;
11 * Delete orphaned crop entities.
13 function crop_update_8001(&$sandbox) {
14 // Unsure we have current element set to 0.
15 if (!isset($sandbox['current'])) {
16 $sandbox['current'] = 0;
17 $sandbox['total'] = \Drupal::entityQuery('crop')
22 $items_per_batch = 100;
23 $crops = \Drupal::entityQuery('crop')
25 ->range($sandbox['current'], $items_per_batch)
29 $sandbox['#finished'] = 1;
32 foreach ($crops as $cid) {
33 /** @var \Drupal\crop\Entity\Crop $crop */
34 $crop = Crop::load($cid);
35 $files = \Drupal::entityQuery('file')
36 ->condition('uri', $crop->get('uri')->value)
39 // Checks if the file exist, if not exist delete this orphan crop.
40 if (empty($files->execute())) {
41 // Lets tell the site admin what we are doing.
42 \Drupal::logger('crop_api')
44 'The orphaned crop @cid referring to image with URI @uri has been deleted.',
45 ['@cid' => $cid, 'uri' => $crop->uri->value]
49 $sandbox['current']++;
51 $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
56 * Let Drupal know that there is a new config available.
58 function crop_update_8002() {
59 \Drupal::service('config.installer')
60 ->installDefaultConfig('module', 'crop');