View source
<?php
namespace Drupal\commerce_product\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
class ProductVariationTitleWidget extends ProductVariationWidgetBase implements ContainerFactoryPluginInterface {
public static function defaultSettings() {
return [
'label_display' => TRUE,
'label_text' => 'Please select',
'hide_single' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['label_display'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display label'),
'#default_value' => $this
->getSetting('label_display'),
];
$element['label_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label text'),
'#default_value' => $this
->getSetting('label_text'),
'#description' => $this
->t('The label will be available to screen readers even if it is not displayed.'),
'#required' => TRUE,
];
$element['hide_single'] = [
'#type' => 'checkbox',
'#title' => $this
->t("Hide if there's only one product variation"),
'#default_value' => $this
->getSetting('hide_single'),
];
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this
->t('Label: "@text" (@visible)', [
'@text' => $this
->getSetting('label_text'),
'@visible' => $this
->getSetting('label_display') ? $this
->t('visible') : $this
->t('hidden'),
]);
if ($this
->getSetting('hide_single')) {
$summary[] = $this
->t("Hidden if there's only one product variation.");
}
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$product = $form_state
->get('product');
$variations = $this
->loadEnabledVariations($product);
if (count($variations) === 0) {
$form_state
->set('hide_form', TRUE);
$element['variation'] = [
'#type' => 'value',
'#value' => 0,
];
return $element;
}
elseif (count($variations) === 1 && $this
->getSetting('hide_single')) {
$selected_variation = reset($variations);
$element['variation'] = [
'#type' => 'value',
'#value' => $selected_variation
->id(),
];
return $element;
}
$wrapper_id = Html::getUniqueId('commerce-product-add-to-cart-form');
$form += [
'#wrapper_id' => $wrapper_id,
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
];
$selected_variation = NULL;
if ($form_state
->isRebuilding()) {
$parents = array_merge($element['#field_parents'], [
$items
->getName(),
$delta,
]);
$user_input = (array) NestedArray::getValue($form_state
->getUserInput(), $parents);
$selected_variation = $this
->selectVariationFromUserInput($variations, $user_input);
}
if (!$selected_variation) {
$selected_variation = $this
->getDefaultVariation($product, $variations);
}
$form_state
->set('selected_variation', $selected_variation
->id());
$variation_options = [];
foreach ($variations as $option) {
$variation_options[$option
->id()] = $option
->label();
}
$element['variation'] = [
'#type' => 'select',
'#title' => $this
->t($this
->getSetting('label_text')),
'#options' => $variation_options,
'#required' => TRUE,
'#default_value' => $selected_variation
->id(),
'#ajax' => [
'callback' => [
get_class($this),
'ajaxRefresh',
],
'wrapper' => $form['#wrapper_id'],
'disable-refocus' => TRUE,
],
];
if ($this
->getSetting('label_display') == FALSE) {
$element['variation']['#title_display'] = 'invisible';
}
return $element;
}
protected function selectVariationFromUserInput(array $variations, array $user_input) {
$current_variation = NULL;
if (!empty($user_input['variation']) && isset($variations[$user_input['variation']])) {
$current_variation = $variations[$user_input['variation']];
}
return $current_variation;
}
}