XquantityStockItem.php in Commerce Extended Quantity 8
File
modules/xquantity_stock/src/Plugin/Field/FieldType/XquantityStockItem.php
View source
<?php
namespace Drupal\xquantity_stock\Plugin\Field\FieldType;
use Drupal\xnumber\Plugin\Field\FieldType\XdecimalItem;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\xnumber\Utility\Xnumber as Numeric;
use Drupal\Core\Form\FormStateInterface;
class XquantityStockItem extends XdecimalItem {
public static function defaultFieldSettings() {
return [
'threshold' => '1800',
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this
->getQuantityWidgetSettings();
if (!empty($settings['step'])) {
$element['step']['#step'] = $settings['step'];
$element['min']['#step'] = $settings['step'];
$element['max']['#step'] = $settings['step'];
}
$element['step']['#min'] = $settings['step'];
$min = $settings['min'];
$min = (!is_numeric($min) || $min < 0) && $settings['unsigned'] ? '0' : $min;
$element['min']['#min'] = $min;
$element['max']['#min'] = $min;
$element['threshold'] = [
'#type' => 'number',
'#step' => '1',
'#field_suffix' => $this
->t('seconds', [], [
'context' => 'xquantity stock',
]),
'#title' => $this
->t('Threshold', [], [
'context' => 'xquantity stock',
]),
'#description' => $this
->t('Stock rotation threshold. Read more: <a href=":href" target="_blank">admin/help/xquantity_stock#stock-rotation</a>', [
':href' => '/admin/help/xquantity_stock#stock-rotation',
]),
'#default_value' => $settings['threshold'],
];
return $element;
}
public function getQuantityWidgetSettings() {
$settings = [];
$type_id = $this
->getEntity()
->getOrderItemTypeId();
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display');
$form_display_mode = $form_display
->load("commerce_order_item.{$type_id}.add_to_cart");
$quantity = $form_display_mode ? $form_display_mode
->getComponent('quantity') : NULL;
if (!$quantity) {
$form_display_mode = $form_display
->load("commerce_order_item.{$type_id}.default");
$quantity = $form_display_mode ? $form_display_mode
->getComponent('quantity') : NULL;
}
if (isset($quantity['settings']['step'])) {
$settings = $form_display_mode
->getRenderer('quantity')
->getFormDisplayModeSettings();
}
return $settings + $this
->getSettings();
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$settings = $field_definition
->getSettings();
$precision = $settings['precision'] ?: 10;
$scale = $settings['scale'] ?: 2;
$max = '10';
$min = $settings['min'] > 0 ? $settings['min'] : ($settings['unsigned'] ? pow(0.1, $scale) : '-10');
$decimal_digits = Numeric::getDecimalDigits($max);
$decimal_digits = max(Numeric::getDecimalDigits($min), $decimal_digits);
$scale = rand($decimal_digits, $scale);
$random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
$values['value'] = Numeric::truncateDecimal($random_decimal, $scale);
return $values;
}
}