diff -u b/simplenews.install b/simplenews.install --- b/simplenews.install +++ b/simplenews.install @@ -145,10 +145,43 @@ - /** - * Remove simplenews_issue field type. + * Update the column. */ function simplenews_update_8001() { - $config = \Drupal::config('simplenews.settings'); - $config->clear('field.value.simplenews_issue.subscribers'); - $config->save(); - // @todo Erase from database. + update_entity_field('node', 'simplenews_issue', function ($schema) { + unset($schema['node__simplenews_issue']['fields']['unsigned']); + unset($schema['node__simplenews_issue']['fields']['size']); + \Drupal::database()->schema()->changeField('node__simplenews_issue', 'simplenews_issue_subscribers', '', $schema['simplenews']['fields']); + }); +} + +/** + * Allows to update an entity field table. + * + * @param string $entity_type_id + * The entity type. + * @param string $field_name + * The field name. + * @param callable $callback + * A callback function that will be called with the original schema and is + * responsible for updating it and also executing any necessary schema + * changes. + */ +function update_entity_field($entity_type_id, $field_name, callable $callback) { + // Get the current schema. + $key_value_store_schema = \Drupal::keyValue('entity.storage_schema.sql'); + $schema = $key_value_store_schema->get($entity_type_id . '.field_schema_data.' . $field_name); + + // Executable the callback to update the schema and manually apply changes. + $callback($schema); + + // Update the last installed field definition and field schema. + /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value_store */ + \Drupal::entityManager()->clearCachedFieldDefinitions(); + $key_value_store_definition = \Drupal::keyValue('entity.definitions.installed'); + $storage_definitions = $key_value_store_definition->get($entity_type_id . '.field_storage_definitions'); + $storage_definitions[$field_name] = $storage_definition = \Drupal::entityManager() + ->getFieldStorageDefinitions($entity_type_id)[$field_name]; + $key_value_store_definition->set($entity_type_id . '.field_storage_definitions', $storage_definitions); + // Update the stored schema. + $key_value_store_schema->set($entity_type_id . '.field_schema_data.' . $field_name, $schema); } +