e9b055bd5dd02d2f024e28da5b706eb7542dfec2
[yaffs-website] / web / modules / contrib / crop / crop.install
1 <?php
2
3 /**
4  * @file
5  * Install, update and uninstall functions for the Crop API module.
6  */
7
8 use Drupal\crop\Entity\Crop;
9
10 /**
11  * Implements hook_requirements().
12  */
13 function crop_requirements($phase) {
14   $requirements = [];
15
16   // This branch of Crop API shouldn't be used with core < 8.4.x or with
17   // media_entity 1.x enabled.
18   $incompatible = FALSE;
19   $drupal_version = explode('.', \Drupal::VERSION);
20   if ($drupal_version[1] < 4) {
21     $incompatible = TRUE;
22   }
23   elseif (\Drupal::moduleHandler()->moduleExists('media_entity')) {
24     $info = system_get_info('module', 'media_entity');
25     if (version_compare($info['version'], '8.x-2') < 0) {
26       $incompatible = TRUE;
27     }
28   }
29
30   if ($incompatible) {
31     $requirements['crop_media'] = [
32       'title' => t('Crop API'),
33       'value' => t('This branch of Crop API is not compatible with the version of Media Entity installed.'),
34       'description' => t('This branch of Crop API is not compatible with the version of Media Entity installed.'),
35       'severity' => REQUIREMENT_ERROR,
36     ];
37   }
38
39   return $requirements;
40 }
41
42 /**
43  * Delete orphaned crop entities.
44  */
45 function crop_update_8001(&$sandbox) {
46   // Unsure we have current element set to 0.
47   if (!isset($sandbox['current'])) {
48     $sandbox['current'] = 0;
49     $sandbox['total'] = \Drupal::entityQuery('crop')
50       ->count()
51       ->execute();
52   }
53
54   $items_per_batch = 100;
55   $crops = \Drupal::entityQuery('crop')
56     ->sort('cid', 'ASC')
57     ->range($sandbox['current'], $items_per_batch)
58     ->execute();
59
60   if (empty($crops)) {
61     $sandbox['#finished'] = 1;
62   }
63   else {
64     foreach ($crops as $cid) {
65       /** @var \Drupal\crop\Entity\Crop $crop */
66       $crop = Crop::load($cid);
67       $files = \Drupal::entityQuery('file')
68         ->condition('uri', $crop->get('uri')->value)
69         ->count();
70
71       // Checks if the file exist, if not exist delete this orphan crop.
72       if (empty($files->execute())) {
73         // Lets tell the site admin what we are doing.
74         \Drupal::logger('crop_api')
75           ->notice(
76             'The orphaned crop @cid referring to image with URI @uri has been deleted.',
77             ['@cid' => $cid, 'uri' => $crop->uri->value]
78           );
79         $crop->delete();
80       }
81       $sandbox['current']++;
82     }
83     $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
84   }
85 }
86
87 /**
88  * Let Drupal know that there is a new config available.
89  */
90 function crop_update_8002() {
91   \Drupal::service('config.installer')
92     ->installDefaultConfig('module', 'crop');
93 }