From 792936f471da443d5e2d30c1552df211aa3e0992 Mon Sep 17 00:00:00 2001 From: Finne Fortuin Date: Mon, 11 Sep 2017 17:49:29 +0200 Subject: [PATCH 1/3] 2885421 promotion description text format --- .../commerce_promotion.post_update.php | 53 +++++++++++++++++++ modules/promotion/src/Entity/Promotion.php | 4 +- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/modules/promotion/commerce_promotion.post_update.php b/modules/promotion/commerce_promotion.post_update.php index 7042d7d6bc..25b3005b70 100644 --- a/modules/promotion/commerce_promotion.post_update.php +++ b/modules/promotion/commerce_promotion.post_update.php @@ -233,3 +233,56 @@ 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(); + + // 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, From f03fec3fb9525da30884a7afd73fbae27137f7c9 Mon Sep 17 00:00:00 2001 From: Finne Fortuin Date: Tue, 12 Sep 2017 11:07:18 +0200 Subject: [PATCH 2/3] change post update function so coupon test won't fail --- .../commerce_promotion.post_update.php | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/modules/promotion/commerce_promotion.post_update.php b/modules/promotion/commerce_promotion.post_update.php index 25b3005b70..f0868f734e 100644 --- a/modules/promotion/commerce_promotion.post_update.php +++ b/modules/promotion/commerce_promotion.post_update.php @@ -241,18 +241,16 @@ 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(); - - // 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(); + /** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */ + $promotion_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_promotion'); + /** @var \Drupal\commerce_promotion\Entity\PromotionInterface[] $promotions */ + $promotions = $promotion_storage->loadMultiple(); + $descriptions = []; + foreach ($promotions as $promotion) { + $descriptions[$promotion->id()] = $promotion->getDescription(); + $promotion->setDescription(null); + $promotion->save(); + } // Update definitions and schema. $entity_definition_manager = \Drupal::entityDefinitionUpdateManager(); @@ -277,6 +275,7 @@ function commerce_promotion_post_update_9() { $entity_definition_manager->installFieldStorageDefinition('description', 'commerce_promotion', 'commerce_promotion', $storage_definition); // Restore entity data in the new schema. + $database = \Drupal::database(); foreach ($descriptions as $promotion_id => $description) { $database->update('commerce_promotion_field_data') ->fields(['description__value' => $description]) @@ -285,4 +284,3 @@ function commerce_promotion_post_update_9() { } } - From 0be46e1b489ee6d4178caa3dafdf91cf3a8878d2 Mon Sep 17 00:00:00 2001 From: Finne Fortuin Date: Tue, 12 Sep 2017 11:59:59 +0200 Subject: [PATCH 3/3] change post update function so coupon test won't fail 2 --- .../commerce_promotion.post_update.php | 77 ++++++++++--------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/modules/promotion/commerce_promotion.post_update.php b/modules/promotion/commerce_promotion.post_update.php index f0868f734e..699d127060 100644 --- a/modules/promotion/commerce_promotion.post_update.php +++ b/modules/promotion/commerce_promotion.post_update.php @@ -241,46 +241,49 @@ 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. - /** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */ - $promotion_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_promotion'); - /** @var \Drupal\commerce_promotion\Entity\PromotionInterface[] $promotions */ - $promotions = $promotion_storage->loadMultiple(); - $descriptions = []; - foreach ($promotions as $promotion) { - $descriptions[$promotion->id()] = $promotion->getDescription(); - $promotion->setDescription(null); - $promotion->save(); - } + $database = \Drupal::database(); - // 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); + // 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(); - $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. - $database = \Drupal::database(); - foreach ($descriptions as $promotion_id => $description) { + // Remove data from the storage. $database->update('commerce_promotion_field_data') - ->fields(['description__value' => $description]) - ->condition('promotion_id', $promotion_id) + ->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(); + } + } }