commerce_promotion.install in Commerce Core 8.2
Install, update and uninstall functions for the commerce_promotion module.
File
modules/promotion/commerce_promotion.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the commerce_promotion module.
*/
use Drupal\commerce\CommerceContentEntityStorageSchema;
use Drupal\commerce_promotion\Entity\Promotion;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\Entity\Role;
/**
* Implements hook_schema().
*/
function commerce_promotion_schema() {
$schema['commerce_promotion_usage'] = [
'description' => 'Stores promotion usage.',
'fields' => [
'usage_id' => [
'description' => 'Primary Key: Usage ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'promotion_id' => [
'description' => 'The {commerce_promotion}.promotion_id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'coupon_id' => [
'description' => 'The {commerce_promotion_coupon}.id.',
'type' => 'int',
'unsigned' => TRUE,
'default' => 0,
],
'order_id' => [
'description' => 'The {commerce_order}.order_id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'mail' => [
'description' => 'The customer email.',
'type' => 'varchar',
'length' => 254,
'not null' => FALSE,
],
],
'primary key' => [
'usage_id',
],
'indexes' => [
'promotion_id' => [
'promotion_id',
],
'coupon_id' => [
'coupon_id',
],
],
'foreign keys' => [
'promotion_id' => [
'commerce_promotion' => 'promotion_id',
],
'coupon_id' => [
'commerce_promotion_coupon' => 'id',
],
'order_id' => [
'commerce_order' => 'order_id',
],
],
];
return $schema;
}
/**
* Install the `commerce_promotion_usage` table schema.
*/
function commerce_promotion_update_8201() {
drupal_install_schema('commerce_promotion');
}
/**
* Remove the current_usage field from promotions, add the weight field.
*/
function commerce_promotion_update_8202() {
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$storage_definition = BaseFieldDefinition::create('integer')
->setName('current_usage')
->setTargetEntityTypeId('commerce_promotion')
->setLabel(t('Current usage'))
->setDescription(t('The number of times the promotion was used.'))
->setDefaultValue(0);
$entity_definition_update
->uninstallFieldStorageDefinition($storage_definition);
$storage_definition = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('The weight of this promotion in relation to others.'))
->setDefaultValue(0)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'integer',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'number',
'weight' => 4,
]);
$entity_definition_update
->installFieldStorageDefinition('weight', 'commerce_promotion', 'commerce_promotion', $storage_definition);
}
/**
* Add the usage_limit field to coupons.
*/
function commerce_promotion_update_8203() {
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$storage_definition = BaseFieldDefinition::create('integer')
->setLabel(t('Usage limit'))
->setDescription(t('The maximum number of times the coupon can be used. 0 for unlimited.'))
->setDefaultValue(0)
->setDisplayOptions('form', [
'type' => 'commerce_usage_limit',
'weight' => 4,
]);
$entity_definition_update
->installFieldStorageDefinition('usage_limit', 'commerce_promotion_coupon', 'commerce_promotion', $storage_definition);
}
/**
* Add the display_name field to promotions.
*/
function commerce_promotion_update_8204() {
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Display name'))
->setDescription(t('If provided, shown on the order instead of "@translated".', [
'@translated' => t('Discount'),
]))
->setTranslatable(TRUE)
->setSettings([
'display_description' => TRUE,
'default_value' => '',
'max_length' => 255,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$entity_definition_update
->installFieldStorageDefinition('display_name', 'commerce_promotion', 'commerce_promotion', $storage_definition);
}
/**
* Add the usage_limit_customer field to promotions and coupons.
*/
function commerce_promotion_update_8205() {
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$storage_definition = BaseFieldDefinition::create('integer')
->setLabel(t('Customer usage limit'))
->setDescription(t('The maximum number of times the promotion can be used by a customer. 0 for unlimited.'))
->setDefaultValue(0)
->setDisplayOptions('form', [
'type' => 'commerce_usage_limit',
'weight' => 4,
]);
$entity_definition_update
->installFieldStorageDefinition('usage_limit_customer', 'commerce_promotion', 'commerce_promotion', $storage_definition);
$entity_definition_update
->installFieldStorageDefinition('usage_limit_customer', 'commerce_promotion_coupon', 'commerce_promotion', $storage_definition);
}
/**
* Ensure new field indexes on the coupon entity.
*/
function commerce_promotion_update_8206() {
$entity_type_manager = \Drupal::entityTypeManager();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Get the current coupon entity type definition, ensure the storage schema
// class is set.
$entity_type = $entity_type_manager
->getDefinition('commerce_promotion_coupon')
->setHandlerClass('storage_schema', CommerceContentEntityStorageSchema::class);
// Regenerate entity type indexes.
$definition_update_manager
->updateEntityType($entity_type);
}
/**
* Update the code field definition.
*/
function commerce_promotion_update_8207() {
$definition_update_manager = \Drupal::service('entity.definition_update_manager');
$definition_update_manager
->updateFieldStorageDefinition($definition_update_manager
->getFieldStorageDefinition('code', 'commerce_promotion_coupon'));
}
/**
* Add created/changed timestamp fields to promotions & coupons.
*/
function commerce_promotion_update_8208() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$created = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setTranslatable(TRUE)
->setDescription(t('The time when the promotion was created.'));
$definition_update_manager
->installFieldStorageDefinition('created', 'commerce_promotion', 'commerce_promotion', $created);
$changed = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setTranslatable(TRUE)
->setDescription(t('The time when the promotion was last edited.'))
->setDisplayConfigurable('view', TRUE);
$definition_update_manager
->installFieldStorageDefinition('changed', 'commerce_promotion', 'commerce_promotion', $changed);
$created = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the coupon was created.'));
$definition_update_manager
->installFieldStorageDefinition('created', 'commerce_promotion_coupon', 'commerce_promotion', $created);
$changed = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the coupon was last edited.'))
->setDisplayConfigurable('view', TRUE);
$definition_update_manager
->installFieldStorageDefinition('changed', 'commerce_promotion_coupon', 'commerce_promotion', $changed);
}
/**
* Add a 'uid' field to promotions.
*/
function commerce_promotion_update_8209() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Set the owner of the promotions to be first user which which has
// the 'administrator' role. This way we avoid hard coding user ID 1 for sites
// that prefer to not give it any special meaning.
$admin_roles = \Drupal::entityTypeManager()
->getStorage('user_role')
->getQuery()
->condition('is_admin', TRUE)
->accessCheck(FALSE)
->execute();
if (!empty($admin_roles)) {
$query = \Drupal::entityTypeManager()
->getStorage('user')
->getQuery()
->condition('roles', $admin_roles, 'IN')
->condition('status', 1)
->sort('uid', 'ASC')
->accessCheck(FALSE)
->range(0, 1);
$result = $query
->execute();
}
// Defaults to user ID 1 if we could not find any other administrator users.
$owner_id = !empty($result) ? reset($result) : 1;
$entity_type = $definition_update_manager
->getEntityType('commerce_promotion');
$keys = $entity_type
->getKeys();
$keys['owner'] = 'uid';
$entity_type
->set('entity_keys', $keys);
$definition_update_manager
->updateEntityType($entity_type);
$storage_definition = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Owner'))
->setDescription(t('The promotion owner.'))
->setSetting('target_type', 'user')
->setTranslatable(TRUE)
->setInitialValue($owner_id)
->setDefaultValueCallback(Promotion::class . '::getDefaultEntityOwner');
$definition_update_manager
->installFieldStorageDefinition('uid', 'commerce_promotion', 'commerce_promotion', $storage_definition);
/** @var \Drupal\user\RoleInterface $role */
foreach (Role::loadMultiple() as $role) {
if (!$role
->hasPermission('update commerce_promotion')) {
continue;
}
$role
->grantPermission("update any commerce_promotion");
$role
->save();
}
}
Functions
Name | Description |
---|---|
commerce_promotion_schema | Implements hook_schema(). |
commerce_promotion_update_8201 | Install the `commerce_promotion_usage` table schema. |
commerce_promotion_update_8202 | Remove the current_usage field from promotions, add the weight field. |
commerce_promotion_update_8203 | Add the usage_limit field to coupons. |
commerce_promotion_update_8204 | Add the display_name field to promotions. |
commerce_promotion_update_8205 | Add the usage_limit_customer field to promotions and coupons. |
commerce_promotion_update_8206 | Ensure new field indexes on the coupon entity. |
commerce_promotion_update_8207 | Update the code field definition. |
commerce_promotion_update_8208 | Add created/changed timestamp fields to promotions & coupons. |
commerce_promotion_update_8209 | Add a 'uid' field to promotions. |