You are here

function votingapi_update_8303 in Voting API 8.3

Alter votingapi_result table manually before definition update.

File

./votingapi.install, line 18
Install, update and uninstall functions for the Voting API module.

Code

function votingapi_update_8303(&$sandbox) {

  // First we need to change the column of the table manually, because if there
  // is content already, entity definition update manager will not allow to
  // change the length of function field.
  $field_storage_definitions = \Drupal::service('entity_field.manager')
    ->getFieldStorageDefinitions('vote_result');
  if (!empty($field_storage_definitions['function'])) {

    /** @var \Drupal\Core\Field\BaseFieldDefinition $function_field_definition */
    $function_field_definition = $field_storage_definitions['function'];
    $schema = $function_field_definition
      ->getSchema();
    \Drupal::database()
      ->schema()
      ->changeField('votingapi_result', 'function', 'function', $schema['columns']['value']);
    if (!empty($function_field_definition)) {
      $new_schema = [
        'votingapi_result' => [
          'fields' => [
            'function' => $schema['columns']['value'] + [
              'not null' => FALSE,
            ],
          ],
        ],
      ];
      \Drupal::keyValue('entity.storage_schema.sql')
        ->set('vote_result.field_schema_data.function', $new_schema);
    }

    /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $repo */
    $repo = \Drupal::service('entity.last_installed_schema.repository');
    $repo
      ->setLastInstalledFieldStorageDefinition($function_field_definition);
  }
}