You are here

crop.install in Crop API 8.2

Same filename and directory in other branches
  1. 8 crop.install

Install, update and uninstall functions for the Crop API module.

File

crop.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Crop API module.
 */
use Drupal\crop\CropStorageSchema;
use Drupal\crop\Entity\Crop;

/**
 * Implements hook_requirements().
 */
function crop_requirements($phase) {
  $requirements = [];

  // This branch of Crop API shouldn't be used with media_entity 1.x enabled.
  $incompatible = FALSE;
  if (\Drupal::moduleHandler()
    ->moduleExists('media_entity')) {
    $info = \Drupal::service('extension.list.module')
      ->getExtensionInfo('media_entity');
    if (version_compare($info['version'], '8.x-2') < 0) {
      $incompatible = TRUE;
    }
  }
  if ($incompatible) {
    $requirements['crop_media'] = [
      'title' => t('Crop API'),
      'value' => t('This branch of Crop API is not compatible with the version of Media Entity installed.'),
      'description' => t('This branch of Crop API is not compatible with the version of Media Entity installed.'),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
  return $requirements;
}

/**
 * Delete orphaned crop entities.
 */
function crop_update_8001(&$sandbox) {

  // Unsure we have current element set to 0.
  if (!isset($sandbox['current'])) {
    $sandbox['current'] = 0;
    $sandbox['total'] = \Drupal::entityQuery('crop')
      ->count()
      ->execute();
  }
  $items_per_batch = 100;
  $crops = \Drupal::entityQuery('crop')
    ->sort('cid', 'ASC')
    ->range($sandbox['current'], $items_per_batch)
    ->execute();
  if (empty($crops)) {
    $sandbox['#finished'] = 1;
  }
  else {
    foreach ($crops as $cid) {

      /** @var \Drupal\crop\Entity\Crop $crop */
      $crop = Crop::load($cid);
      $files = \Drupal::entityQuery('file')
        ->condition('uri', $crop
        ->get('uri')->value)
        ->count();

      // Checks if the file exist, if not exist delete this orphan crop.
      if (empty($files
        ->execute())) {

        // Lets tell the site admin what we are doing.
        \Drupal::logger('crop_api')
          ->notice('The orphaned crop @cid referring to image with URI @uri has been deleted.', [
          '@cid' => $cid,
          'uri' => $crop->uri->value,
        ]);
        $crop
          ->delete();
      }
      $sandbox['current']++;
    }
    $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
  }
}

/**
 * Let Drupal know that there is a new config available.
 */
function crop_update_8002() {
  \Drupal::service('config.installer')
    ->installDefaultConfig('module', 'crop');
}

/**
 * Uninstall deprecated sub-module from active instance.
 */
function crop_update_8004() {
  if (\Drupal::moduleHandler()
    ->moduleExists('crop_media_entity')) {
    \Drupal::service('module_installer')
      ->uninstall([
      'crop_media_entity',
    ]);
  }
}

/**
 * Add index on type and uri.
 */
function crop_update_8005() {
  $manager = \Drupal::entityDefinitionUpdateManager();

  // Get the current crop entity type definition, ensure the storage schema
  // class is set.
  $entity_type = $manager
    ->getEntityType('crop')
    ->setHandlerClass('storage_schema', CropStorageSchema::class);

  // Regenerate entity type indexes.
  $manager
    ->updateEntityType($entity_type);
}

Functions

Namesort descending Description
crop_requirements Implements hook_requirements().
crop_update_8001 Delete orphaned crop entities.
crop_update_8002 Let Drupal know that there is a new config available.
crop_update_8004 Uninstall deprecated sub-module from active instance.
crop_update_8005 Add index on type and uri.