/**
* Extend poll choices more than 255 characters.
*/
function poll_update_8006() {
$connection = Database::getConnection();
$entity_definition_update_manager = Drupal::entityDefinitionUpdateManager();
$entity_type_id = 'poll_choice';
// Check if we have updates for entity type.
if ($entity_definition_update_manager->needsUpdates()) {
$changes = $entity_definition_update_manager->getChangeSummary();
if (array_key_exists($entity_type_id, $changes)) {
// Set tables to update.
$tables_to_update = [
$entity_type_id . '_field_data',
];
$tables_data = [];
// Copy content from entity tables.
foreach ($tables_to_update as $table_to_update) {
$tables_data[$table_to_update] = $connection->select($table_to_update)
->fields($table_to_update)
->execute()->fetchAll();
$connection->truncate($table_to_update)->execute();
}
// Apply updates.
$entity_field_manager = \Drupal::service('entity_field.manager');
$entity_field_manager->clearCachedFieldDefinitions();
$storage_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
if ($storage_definitions['choice']) {
$entity_definition_update_manager->updateFieldStorageDefinition($storage_definitions['choice']);
}
// Insert content into updated entity tables.
foreach ($tables_data as $table_name => $table_data) {
foreach ($table_data as $result) {
$data = (array) $result;
// Save the information in the table.
$connection->insert($table_name)
->fields($data)
->execute();
}
}
}
}
}