Commit 21038490 authored by bojanz's avatar bojanz

Reimplement the default price widget.

parent f30c0f57
......@@ -31,7 +31,7 @@ content:
placeholder: ''
third_party_settings: { }
unit_price:
type: price_simple
type: price_default
weight: 2
settings: { }
third_party_settings: { }
......
drupal.commerce_price.simple-widget:
admin:
version: VERSION
css:
theme:
css/commerce_price.css: {}
css/commerce_price.admin.css: {}
/**
* @file
* Admin styling.
*/
.field-widget-price-default .form-type-textfield, .field-widget-price-default .form-type-select {
display: inline;
}
/**
* @file
* Styling for Commerce Price.
*/
.field-widget-price-simple .form-type-textfield, .field-widget-price-simple .form-type-select {
display: inline;
}
......@@ -18,7 +18,7 @@ use Drupal\Core\TypedData\DataDefinition;
* id = "price",
* label = @Translation("Price"),
* description = @Translation("Stores a decimal amount and a three letter currency code."),
* default_widget = "price_simple",
* default_widget = "price_default",
* default_formatter = "price_default",
* )
*/
......
<?php
/**
* @file
* Contains \Drupal\commerce_price\Plugin\Field\FieldWidget\PriceDefaultWidget.
*/
namespace Drupal\commerce_price\Plugin\Field\FieldWidget;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'price_default' widget.
*
* @FieldWidget(
* id = "price_default",
* label = @Translation("Price"),
* field_types = {
* "price"
* }
* )
*/
class PriceDefaultWidget extends WidgetBase implements ContainerFactoryPluginInterface {
/**
* The currency storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $currencyStorage;
/**
* Constructs a new PriceDefaultWidget object.
*
* @param string $pluginId
* The plugin_id for the widget.
* @param mixed $pluginDefinition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition
* The definition of the field to which the widget is associated.
* @param array $settings
* The widget settings.
* @param array $thirdPartySettings
* Any third party settings.
* @param \Drupal\Core\Entity\EntityStorageInterface $currencyStorage
* The currency storage.
*/
public function __construct($pluginId, $pluginDefinition, FieldDefinitionInterface $fieldDefinition, array $settings, array $thirdPartySettings, EntityStorageInterface $currencyStorage) {
parent::__construct($pluginId, $pluginDefinition, $fieldDefinition, $settings, $thirdPartySettings);
$this->currencyStorage = $currencyStorage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
/** @var \Drupal\Core\Entity\EntityManagerInterface $entityManager */
$entityManager = $container->get('entity.manager');
return new static(
$pluginId,
$pluginDefinition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$entityManager->getStorage('commerce_currency')
);
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// Load both enabled and disabled currencies, prices with disabled
// currencies can be skipped down the line.
$currencies = $this->currencyStorage->loadMultiple();
$currencyCodes = array_keys($currencies);
// Stop rendering if there are no currencies available.
if (empty($currencyCodes)) {
return $element;
}
$element['#attached']['library'][] = 'commerce_price/admin';
$element['amount'] = [
'#type' => 'textfield',
'#title' => $element['#title'],
'#default_value' => $this->prepareAmount($items[$delta]->amount),
'#required' => $element['#required'],
'#size' => 10,
'#maxlength' => 255,
];
if (count($currencyCodes) == 1) {
$currencyCode = reset($currencyCodes);
$element['amount']['#field_suffix'] = $currencyCode;
$element['currency_code'] = [
'#type' => 'value',
'#value' => $currencyCode,
];
}
else {
$element['currency_code'] = [
'#type' => 'select',
'#title' => $this->t('Currency'),
'#default_value' => $items[$delta]->currency_code,
'#options' => array_combine($currencyCodes, $currencyCodes),
'#title_display' => 'invisible',
'#field_suffix' => '',
];
}
return $element;
}
/**
* Prepares the amount for display.
*
* @param int $amount
* The amount
*
* @return int
* The processed amount, ready for display.
*/
protected function prepareAmount($amount) {
$amount = $amount ?: 0;
if (!empty($amount)) {
// Trim redundant zeroes.
$amount = rtrim($amount, 0);
// If we've trimmed all the way to the decimal separator, remove it.
$lastChar = substr($amount, -1, 1);
if (!is_numeric($lastChar)) {
$amount = substr($amount, 0, strlen($amount) - 1);
}
}
return $amount;
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_price\Plugin\Field\FieldWidget\PriceSimpleWidget.
*/
namespace Drupal\commerce_price\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the commerce price widget.
*
* @FieldWidget(
* id = "price_simple",
* label = @Translation("Price"),
* field_types = {
* "price"
* }
* )
*/
class PriceSimpleWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// Get the enabled currencies.
$enabledCurrencies = entity_load_multiple_by_properties('commerce_currency', ['status' => 1]);
$currency_codes = array_keys($enabledCurrencies);
$default_amount = NULL;
if (isset($items[$delta]->amount)) {
// Trim all trailing 0. Since prices doesn't use significant figures they
// are redundant. Maybe we should keep the zeroes that normally would be
// displayed (fx 123.00 for EUR). For now this should be enough.
$default_amount = rtrim($items[$delta]->amount, 0);
}
$element['amount'] = [
'#type' => 'textfield',
'#title' => $element['#title'],
'#default_value' => $default_amount,
'#required' => $element['#required'],
'#size' => 10,
'#maxlength' => 255,
'#attached' => [
'library' => [
'commerce_price/drupal.commerce_price.simple-widget',
],
],
];
$element['currency_code'] = [
'#type' => 'select',
'#title' => $this->t('Currency code'),
'#default_value' => isset($items[$delta]->currency_code) ? $items[$delta]->currency_code : NULL,
'#required' => $element['#required'],
'#options' => array_combine($currency_codes, $currency_codes),
'#title_display' => 'invisible',
];
return $element;
}
}
......@@ -17,7 +17,7 @@ content:
placeholder: ''
third_party_settings: { }
commerce_price:
type: price_simple
type: price_default
weight: 4
settings: { }
third_party_settings: { }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment