ListPriceWidget.php in Commerce Core 8.2
File
modules/price/src/Plugin/Field/FieldWidget/ListPriceWidget.php
View source
<?php
namespace Drupal\commerce_price\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class ListPriceWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$checkbox_parents = array_merge($form['#parents'], [
$this->fieldDefinition
->getName(),
0,
'has_value',
]);
$checkbox_path = array_shift($checkbox_parents);
$checkbox_path .= '[' . implode('][', $checkbox_parents) . ']';
$element['has_value'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Provide a list price'),
];
$element['value'] = [
'#type' => 'commerce_price',
'#title' => $element['#title'],
'#available_currencies' => array_filter($this
->getFieldSetting('available_currencies')),
'#states' => [
'visible' => [
':input[name="' . $checkbox_path . '"]' => [
'checked' => TRUE,
],
],
],
];
if (!$items[$delta]
->isEmpty()) {
$element['has_value']['#default_value'] = TRUE;
$element['value']['#default_value'] = $items[$delta]
->toPrice()
->toArray();
}
if ($element['#required']) {
$element['has_value']['#access'] = FALSE;
$element['has_value']['#default_value'] = TRUE;
$element['value']['#required'] = TRUE;
}
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$item) {
$item = !empty($item['has_value']) ? $item['value'] : NULL;
}
return $values;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getName() == 'list_price';
}
}