Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2885421 promotion description text format #789

Open
wants to merge 3 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions modules/promotion/commerce_promotion.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
4 changes: 2 additions & 2 deletions modules/promotion/src/Entity/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down