You are here

smart_date_recur.install in Smart Date 3.2.x

File

modules/smart_date_recur/smart_date_recur.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for smart_date_recur module.
 */
declare (strict_types=1);

/**
 * Implements hook_requirements().
 */
function smart_date_recur_requirements($phase) {
  $requirements = [];
  if (!class_exists('\\Recurr\\Rule')) {
    $requirements['smart_date_recur_dependencies'] = [
      'title' => t('Smart date recur'),
      'description' => \t('Smart Date Recur has unmet Composer dependencies. Read the <a href="@url">documentation</a> on how to install them.', [
        '@url' => 'https://www.drupal.org/node/2627292',
      ]),
      'severity' => \REQUIREMENT_ERROR,
    ];
  }
  return $requirements;
}

/**
 * Increase the length of "freq" to 8 characters.
 */
function smart_date_recur_update_8301() {
  db_change_varchar_field('smart_date_rule', 'freq', 8);
}
if (!function_exists('db_change_varchar_field')) {

  /**
   * Change length of a varchar entity field with data, safe with entity-updates.
   *
   * This updates the storage schema, the database schema, and the last
   * installed schema.
   *
   * The entity schema must also be changed in code in the entities
   * baseFieldDefinitions() or in an alter.
   *
   * @param string $entity_type_id
   *   The entity type.
   * @param string $field_name
   *   The field name to change.
   * @param int $field_length
   *   The new length of the field, must be larger than the previous value.
   */
  function db_change_varchar_field($entity_type_id, $field_name, $field_length) {

    /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */
    $schema_repository = \Drupal::service('entity.last_installed_schema.repository');

    /** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
    $entity_field_manager = \Drupal::service('entity_field.manager');
    $base_field_definitions = $entity_field_manager
      ->getBaseFieldDefinitions($entity_type_id);
    $schema_repository
      ->setLastInstalledFieldStorageDefinition($base_field_definitions[$field_name]);
    $field_storage_definitions = $schema_repository
      ->getLastInstalledFieldStorageDefinitions($entity_type_id);
    $field_storage_definitions[$field_name]
      ->setSetting('max_length', $field_length);

    // Update the field definition in the last installed schema repository.
    $schema_repository
      ->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);

    // Update the storage schema.
    $key_value = \Drupal::keyValue('entity.storage_schema.sql');
    $key_name = $entity_type_id . '.field_schema_data.' . $field_name;
    $storage_schema = $key_value
      ->get($key_name);

    // Update all tables where the field is present.
    foreach ($storage_schema as &$table_schema) {
      $table_schema['fields'][$field_name]['length'] = $field_length;
    }
    $key_value
      ->set($key_name, $storage_schema);

    // Update the database tables where the field is part of.
    $db = Drupal::database();
    foreach ($storage_schema as $table_name => $table_schema) {
      $db
        ->schema()
        ->changeField($table_name, $field_name, $field_name, $table_schema['fields'][$field_name]);
    }
  }
}

Functions

Namesort descending Description
smart_date_recur_requirements Implements hook_requirements().
smart_date_recur_update_8301 Increase the length of "freq" to 8 characters.