class CoreStockEvents in Commerce Stock 8
Core Stock Events.
Plugin annotation
@StockEvents(
id = "core_stock_events",
description = @Translation("Core stock Events."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\commerce_stock\Plugin\StockEvents\CoreStockEventsBase implements StockEventsInterface
- class \Drupal\commerce_stock\Plugin\StockEvents\CoreStockEvents
- class \Drupal\commerce_stock\Plugin\StockEvents\CoreStockEventsBase implements StockEventsInterface
Expanded class hierarchy of CoreStockEvents
1 file declares its use of CoreStockEvents
- OrderEventSubscriber.php in src/
EventSubscriber/ OrderEventSubscriber.php
File
- src/
Plugin/ StockEvents/ CoreStockEvents.php, line 19
Namespace
Drupal\commerce_stock\Plugin\StockEventsView source
class CoreStockEvents extends CoreStockEventsBase {
/**
* {@inheritdoc}
*/
public function stockEvent(Context $context, PurchasableEntityInterface $entity, $stockEvent, $quantity, StockLocationInterface $location, $transaction_type, array $metadata) {
$config = \Drupal::configFactory()
->get('commerce_stock.core_stock_events');
// Check if event should be handled.
$order_placed_events = [
StockEventsInterface::ORDER_PLACE_EVENT,
];
$order_update_events = [
StockEventsInterface::ORDER_UPDATE_EVENT,
StockEventsInterface::ORDER_ITEM_UPDATE_EVENT,
];
$order_delete_events = [
StockEventsInterface::ORDER_CANCEL_EVENT,
StockEventsInterface::ORDER_DELET_EVENT,
StockEventsInterface::ORDER_ITEM_DELETE_EVENT,
];
// Cancel if event type is not enabled.
if (in_array($stockEvent, $order_placed_events) && !$config
->get('core_stock_events_order_complete')) {
return FALSE;
}
elseif (in_array($stockEvent, $order_update_events) && !$config
->get('core_stock_events_order_updates')) {
return FALSE;
}
elseif (in_array($stockEvent, $order_delete_events) && !$config
->get('core_stock_events_order_cancel')) {
return FALSE;
}
// Get the stock service.
$stockService = \Drupal::service('commerce_stock.service_manager')
->getService($entity);
// Use the stock updater to create the transaction.
$transaction_id = $stockService
->getStockUpdater()
->createTransaction($entity, $location
->getId(), '', $quantity, NULL, $currency_code = NULL, $transaction_type, $metadata);
// Return the transaction ID.
return $transaction_id;
}
/**
* {@inheritdoc}
*/
public function configFormOptions() {
$config = \Drupal::configFactory()
->get('commerce_stock.core_stock_events');
$form_options['core_stock_events_order_complete'] = [
'#type' => 'checkbox',
'#title' => t('Reserve stock on order complete'),
'#default_value' => $config
->get('core_stock_events_order_complete'),
];
$form_options['core_stock_events_order_cancel'] = [
'#type' => 'checkbox',
'#title' => t('Automatically return stock on cancel'),
'#default_value' => $config
->get('core_stock_events_order_cancel'),
];
$form_options['core_stock_events_order_updates'] = [
'#type' => 'checkbox',
'#title' => t('Adjust stock on order updates (after the order was completed)'),
'#default_value' => $config
->get('core_stock_events_order_updates'),
];
return $form_options;
}
/**
* {@inheritdoc}
*/
public function saveConfigFormOptions(array $form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$config = \Drupal::configFactory()
->getEditable('commerce_stock.core_stock_events');
$config
->set('core_stock_events_order_complete', $values['core_stock_events_order_complete']);
$config
->set('core_stock_events_order_cancel', $values['core_stock_events_order_cancel']);
$config
->set('core_stock_events_order_updates', $values['core_stock_events_order_updates']);
$config
->save();
}
/**
* To ensure backwards compatibility we introduced StockEventTypes plugins
* without changing the StockEventsInterface. This functions maps the
* interface constants to StockEventTypes.
*
* @param int $event_type_id
* The StockEventsInterface interface constant.
*
* @return string
* The StockEventType id or FALSE if not exists.
*/
public static function mapStockEventTypes($event_type_id) {
$map = self::getEventTypeMap();
$result = array_key_exists($event_type_id, $map) ? $map[$event_type_id] : FALSE;
return $result;
}
/**
* To ensure backwards compatibility we introduced StockEventTypes plugins
* without changing the StockEventsInterface. This functions maps the
* interface constants to StockEventTypes.
*
* @param string $stock_event_type_id
* The StockEventType id.
*
* @return int
* The StockEventsInterface interface constant or FALSE if it not exists.
*/
public static function mapStockEventIds($stock_event_type_id) {
$map = array_flip(self::getEventTypeMap());
$result = array_key_exists($stock_event_type_id, $map) ? $map[$stock_event_type_id] : FALSE;
return $result;
}
/**
* Get the map of StockEvenTypes.
*/
private static function getEventTypeMap() {
return $map = [
StockEventsInterface::ORDER_PLACE_EVENT => 'commerce_stock_order_place',
StockEventsInterface::ORDER_UPDATE_EVENT => 'commerce_stock_order_update',
StockEventsInterface::ORDER_CANCEL_EVENT => 'commerce_stock_order_cancel',
StockEventsInterface::ORDER_DELET_EVENT => 'commerce_stock_order_delete',
StockEventsInterface::ORDER_ITEM_DELETE_EVENT => 'commerce_stock_order_item_delete',
StockEventsInterface::ORDER_ITEM_UPDATE_EVENT => 'commerce_stock_order_item_update',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CoreStockEvents:: |
public | function |
Return form elements holding the configuration options. Overrides StockEventsInterface:: |
|
CoreStockEvents:: |
private static | function | Get the map of StockEvenTypes. | |
CoreStockEvents:: |
public static | function | To ensure backwards compatibility we introduced StockEventTypes plugins without changing the StockEventsInterface. This functions maps the interface constants to StockEventTypes. | |
CoreStockEvents:: |
public static | function | To ensure backwards compatibility we introduced StockEventTypes plugins without changing the StockEventsInterface. This functions maps the interface constants to StockEventTypes. | |
CoreStockEvents:: |
public | function |
Save the configuration options. Overrides StockEventsInterface:: |
|
CoreStockEvents:: |
public | function |
A stock event with transaction details. Overrides StockEventsInterface:: |
|
CoreStockEventsBase:: |
protected | function | Helper method for building the transaction metadata. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StockEventsInterface:: |
constant | |||
StockEventsInterface:: |
constant | |||
StockEventsInterface:: |
constant | |||
StockEventsInterface:: |
constant | |||
StockEventsInterface:: |
constant | |||
StockEventsInterface:: |
constant |