5 * Install, update and uninstall functions for the Crop API module.
8 use Drupal\crop\Entity\Crop;
11 * Implements hook_requirements().
13 function crop_requirements($phase) {
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) {
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) {
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,
43 * Delete orphaned crop entities.
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')
54 $items_per_batch = 100;
55 $crops = \Drupal::entityQuery('crop')
57 ->range($sandbox['current'], $items_per_batch)
61 $sandbox['#finished'] = 1;
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)
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')
76 'The orphaned crop @cid referring to image with URI @uri has been deleted.',
77 ['@cid' => $cid, 'uri' => $crop->uri->value]
81 $sandbox['current']++;
83 $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
88 * Let Drupal know that there is a new config available.
90 function crop_update_8002() {
91 \Drupal::service('config.installer')
92 ->installDefaultConfig('module', 'crop');