View source
<?php
namespace Drupal\commerce_product\Plugin\Field\FieldWidget;
use Drupal\commerce\InlineFormManager;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
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;
class SingleVariationWidget extends WidgetBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $inlineFormManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
$this->inlineFormManager = $inline_form_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('entity_type.manager'), $container
->get('plugin.manager.commerce_inline_form'));
}
public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
if ($this
->isDefaultValueWidget($form_state)) {
return $form;
}
return parent::form($items, $form, $form_state, $get_delta);
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$variation = !$items
->isEmpty() ? $items
->first()->entity : NULL;
if (!$variation) {
$variation = $this->entityTypeManager
->getStorage('commerce_product_variation')
->create([
'type' => reset($this
->getFieldSetting('handler_settings')['target_bundles']),
'langcode' => $items
->getEntity()
->language()
->getId(),
]);
}
$inline_form = $this->inlineFormManager
->createInstance('content_entity', [], $variation);
$element = [
'#type' => 'details',
'#open' => TRUE,
'#required' => FALSE,
'#field_title' => $this
->t('Product information'),
'#after_build' => [
[
get_class($this),
'removeTranslatabilityClue',
],
],
] + $element;
$element['entity'] = [
'#parents' => array_merge($element['#field_parents'], [
$items
->getName(),
'entity',
]),
'#inline_form' => $inline_form,
];
$element['entity'] = $inline_form
->buildInlineForm($element['entity'], $form_state);
return $element;
}
public static function removeTranslatabilityClue(array $element, FormStateInterface $form_state) {
$element['#title'] = $element['#field_title'];
return $element;
}
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
if ($this
->isDefaultValueWidget($form_state)) {
$items
->filterEmptyItems();
return;
}
$parents = [
$this->fieldDefinition
->getName(),
'widget',
];
if ($form['#type'] != 'inline_entity_form') {
$parents = array_merge($form['#parents'], $parents);
}
$element = NestedArray::getValue($form, $parents);
$inline_form = $element['entity']['#inline_form'];
$values = $items
->getValue();
$values[0] = [
'entity' => $inline_form
->getEntity(),
];
$items
->setValue($values);
$items
->filterEmptyItems();
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_type = $field_definition
->getTargetEntityTypeId();
$field_name = $field_definition
->getName();
return $entity_type == 'commerce_product' && $field_name == 'variations';
}
}