diff --git a/modules/promotion/commerce_promotion.post_update.php b/modules/promotion/commerce_promotion.post_update.php index 7042d7d6bc..699d127060 100644 --- a/modules/promotion/commerce_promotion.post_update.php +++ b/modules/promotion/commerce_promotion.post_update.php @@ -233,3 +233,57 @@ function commerce_promotion_post_update_8(&$sandbox = NULL) { $sandbox['#finished'] = ($sandbox['total_count'] - $sandbox['current_count']) / $sandbox['total_count']; } } + +/** + * Change the description basefield to support formatted text. + */ +function commerce_promotion_post_update_9() { + // To update the field schema we need to have no field data in the storage, + // thus we retrieve it, delete it from storage, and write it back to the + // storage after updating the schema. + $database = \Drupal::database(); + + // Check update is needed. + if ($database->schema()->fieldExists('commerce_promotion_field_data', 'description')) { + // Retrieve existing field data. + $descriptions = $database->select('commerce_promotion_field_data', 'cp') + ->fields('cp', ['promotion_id', 'description']) + ->execute() + ->fetchAllKeyed(); + + // Remove data from the storage. + $database->update('commerce_promotion_field_data') + ->fields(['description' => NULL]) + ->execute(); + + // Update definitions and schema. + $entity_definition_manager = \Drupal::entityDefinitionUpdateManager(); + /** @var Drupal\Core\Field\BaseFieldDefinition $storage_definition */ + $storage_definition = $entity_definition_manager->getFieldStorageDefinition('description', 'commerce_promotion'); + $entity_definition_manager->uninstallFieldStorageDefinition($storage_definition); + + $storage_definition = BaseFieldDefinition::create('text_long') + ->setLabel(t('Description')) + ->setDescription(t('Additional information about the promotion to show to the customer')) + ->setTranslatable(TRUE) + ->setDefaultValue('') + ->setDisplayOptions('form', [ + 'type' => 'text_textarea', + 'weight' => 1, + 'settings' => [ + 'rows' => 3, + ], + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE); + $entity_definition_manager->installFieldStorageDefinition('description', 'commerce_promotion', 'commerce_promotion', $storage_definition); + + // Restore entity data in the new schema. + foreach ($descriptions as $promotion_id => $description) { + $database->update('commerce_promotion_field_data') + ->fields(['description__value' => $description]) + ->condition('promotion_id', $promotion_id) + ->execute(); + } + } +} diff --git a/modules/promotion/src/Entity/Promotion.php b/modules/promotion/src/Entity/Promotion.php index 52842cea0d..6876cf0425 100644 --- a/modules/promotion/src/Entity/Promotion.php +++ b/modules/promotion/src/Entity/Promotion.php @@ -545,13 +545,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDisplayConfigurable('view', TRUE) ->setDisplayConfigurable('form', TRUE); - $fields['description'] = BaseFieldDefinition::create('string_long') + $fields['description'] = BaseFieldDefinition::create('text_long') ->setLabel(t('Description')) ->setDescription(t('Additional information about the promotion to show to the customer')) ->setTranslatable(TRUE) ->setDefaultValue('') ->setDisplayOptions('form', [ - 'type' => 'string_textarea', + 'type' => 'text_textarea', 'weight' => 1, 'settings' => [ 'rows' => 3,