View source
<?php
namespace Drupal\commerce_product\Plugin\Field\FieldWidget;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Event\ProductVariationAjaxChangeEvent;
use Drupal\commerce_product\Event\ProductEvents;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ProductVariationWidgetBase extends WidgetBase implements ContainerFactoryPluginInterface {
protected $variationStorage;
protected $entityRepository;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityRepository = $entity_repository;
$this->variationStorage = $entity_type_manager
->getStorage('commerce_product_variation');
}
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('entity.repository'));
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_type = $field_definition
->getTargetEntityTypeId();
$field_name = $field_definition
->getName();
return $entity_type == 'commerce_order_item' && $field_name == 'purchased_entity';
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as $key => $value) {
$values[$key] = [
'target_id' => $value['variation'],
];
}
return $values;
}
public static function ajaxRefresh(array $form, FormStateInterface $form_state) {
$ajax_renderer = \Drupal::service('main_content_renderer.ajax');
$request = \Drupal::request();
$route_match = \Drupal::service('current_route_match');
$response = $ajax_renderer
->renderResponse($form, $request, $route_match);
$variation = ProductVariation::load($form_state
->get('selected_variation'));
$product = $form_state
->get('product');
if ($variation
->hasTranslation($product
->language()
->getId())) {
$variation = $variation
->getTranslation($product
->language()
->getId());
}
$variation_field_renderer = \Drupal::service('commerce_product.variation_field_renderer');
$view_mode = $form_state
->get('view_mode');
$variation_field_renderer
->replaceRenderedFields($response, $variation, $view_mode);
$event = new ProductVariationAjaxChangeEvent($variation, $response, $view_mode);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher
->dispatch(ProductEvents::PRODUCT_VARIATION_AJAX_CHANGE, $event);
return $response;
}
protected function getDefaultVariation(ProductInterface $product, array $variations) {
$langcode = $product
->language()
->getId();
$selected_variation = $this->variationStorage
->loadFromContext($product);
$selected_variation = $this->entityRepository
->getTranslationFromContext($selected_variation, $langcode);
if (!in_array($selected_variation, $variations)) {
$selected_variation = reset($variations);
}
return $selected_variation;
}
protected function loadEnabledVariations(ProductInterface $product) {
$langcode = $product
->language()
->getId();
$variations = $this->variationStorage
->loadEnabled($product);
foreach ($variations as $key => $variation) {
$variations[$key] = $this->entityRepository
->getTranslationFromContext($variation, $langcode);
}
return $variations;
}
}