forked from a64f7bb4-7358-4778-9fbe-3b882c34cc1d/v1
145 lines
5.3 KiB
PHP
145 lines
5.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Contains install and update functions for Shipping.
|
|
*/
|
|
|
|
use Drupal\commerce_shipping\Event\ShipmentEvent;
|
|
use Drupal\Core\Field\BaseFieldDefinition;
|
|
|
|
/**
|
|
* Remove the 'adjustments' field from shipments.
|
|
*/
|
|
function commerce_shipping_update_8200() {
|
|
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
|
|
|
|
$storage_definition = BaseFieldDefinition::create('commerce_adjustment')
|
|
->setLabel(t('Adjustments'))
|
|
->setTargetEntityTypeId('commerce_shipment')
|
|
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
|
|
->setDisplayConfigurable('form', FALSE)
|
|
->setDisplayConfigurable('view', TRUE);
|
|
$entity_definition_update->uninstallFieldStorageDefinition($storage_definition);
|
|
}
|
|
|
|
/**
|
|
* Add the condition fields to shipping methods.
|
|
*/
|
|
function commerce_shipping_update_8201() {
|
|
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
|
|
|
|
$storage_definition = BaseFieldDefinition::create('commerce_plugin_item:commerce_condition')
|
|
->setLabel(t('Conditions'))
|
|
->setTargetEntityTypeId('commerce_shipping_method')
|
|
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
|
|
->setRequired(FALSE)
|
|
->setDisplayOptions('form', [
|
|
'type' => 'commerce_conditions',
|
|
'weight' => 3,
|
|
'settings' => [
|
|
'entity_types' => ['commerce_order', 'commerce_shipment'],
|
|
],
|
|
]);
|
|
$entity_definition_update->installFieldStorageDefinition('conditions', 'commerce_shipping_method', 'commerce_shipping', $storage_definition);
|
|
|
|
$storage_definition = BaseFieldDefinition::create('list_string')
|
|
->setLabel(t('Condition operator'))
|
|
->setDescription(t('The condition operator.'))
|
|
->setRequired(TRUE)
|
|
->setSetting('allowed_values', [
|
|
'AND' => t('All conditions must pass'),
|
|
'OR' => t('Only one condition must pass'),
|
|
])
|
|
->setDisplayOptions('form', [
|
|
'type' => 'options_buttons',
|
|
'weight' => 4,
|
|
])
|
|
->setDisplayConfigurable('form', TRUE)
|
|
->setDefaultValue('AND');
|
|
$entity_definition_update->installFieldStorageDefinition('condition_operator', 'commerce_shipping_method', 'commerce_shipping', $storage_definition);
|
|
}
|
|
|
|
/**
|
|
* Add the 'adjustments' field to shipments.
|
|
*/
|
|
function commerce_shipping_update_8202() {
|
|
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
|
|
|
|
$storage_definition = BaseFieldDefinition::create('commerce_adjustment')
|
|
->setLabel(t('Adjustments'))
|
|
->setTargetEntityTypeId('commerce_shipment')
|
|
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
|
|
->setDisplayConfigurable('form', FALSE)
|
|
->setDisplayConfigurable('view', FALSE);
|
|
$entity_definition_update->installFieldStorageDefinition('adjustments', 'commerce_shipment', 'commerce_shipping', $storage_definition);
|
|
}
|
|
|
|
/**
|
|
* Add the 'original_amount' field to shipments.
|
|
*/
|
|
function commerce_shipping_update_8203() {
|
|
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
|
|
|
|
$storage_definition = BaseFieldDefinition::create('commerce_price')
|
|
->setLabel(t('Original amount'))
|
|
->setDescription(t('The original amount.'))
|
|
->setDisplayConfigurable('form', FALSE)
|
|
->setDisplayConfigurable('view', TRUE)
|
|
->setInitialValueFromField('amount');
|
|
$entity_definition_update->installFieldStorageDefinition('original_amount', 'commerce_shipment', 'commerce_shipping', $storage_definition);
|
|
}
|
|
|
|
/**
|
|
* Add created and changed fields to shipping methods.
|
|
*/
|
|
function commerce_shipping_update_8204() {
|
|
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
|
|
|
$storage_definitions['created'] = BaseFieldDefinition::create('created')
|
|
->setLabel(t('Created'))
|
|
->setDescription(t('The time when the shipping method was created.'))
|
|
->setTranslatable(TRUE)
|
|
->setDisplayConfigurable('form', FALSE)
|
|
->setDisplayConfigurable('view', FALSE);
|
|
|
|
$storage_definitions['changed'] = BaseFieldDefinition::create('changed')
|
|
->setLabel(t('Changed'))
|
|
->setDescription(t('The time when the shipping method was last edited.'))
|
|
->setTranslatable(TRUE);
|
|
|
|
foreach ($storage_definitions as $name => $definition) {
|
|
$definition_update_manager->installFieldStorageDefinition($name, 'commerce_shipping_method', 'commerce_shipping', $definition);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add the event handler to the shipment entity type.
|
|
*/
|
|
function commerce_shipping_update_8205() {
|
|
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
|
|
$entity_type = $entity_definition_update->getEntityType('commerce_shipment');
|
|
$entity_type->setHandlerClass('event', ShipmentEvent::class);
|
|
$entity_definition_update->updateEntityType($entity_type);
|
|
}
|
|
|
|
/**
|
|
* Mark the shipping method "stores" field as optional.
|
|
*/
|
|
function commerce_shipping_update_8206() {
|
|
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
|
|
}
|
|
|
|
/**
|
|
* Uninstall the created / changed fields added by content_translation.
|
|
*/
|
|
function commerce_shipping_update_8207() {
|
|
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
|
|
|
foreach (['content_translation_created', 'content_translation_changed'] as $field_name) {
|
|
if ($field_storage_definition = $definition_update_manager->getFieldStorageDefinition($field_name, 'commerce_shipping_method')) {
|
|
$definition_update_manager->uninstallFieldStorageDefinition($field_storage_definition);
|
|
}
|
|
}
|
|
}
|