commerce_product_bundles-8.x-1.0/src/ProductBundleLazyBuilders.php
src/ProductBundleLazyBuilders.php
<?php namespace Drupal\commerce_product_bundles; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormState; use Drupal\Core\Security\TrustedCallbackInterface; /** * Class ProductBundleLazyBuilders * * @package Drupal\commerce_product_bundles * * Code ws taken form and modified: * @see \Drupal\commerce_product\ProductLazyBuilders */ class ProductBundleLazyBuilders implements TrustedCallbackInterface { /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The form builder. * * @var \Drupal\Core\Form\FormBuilderInterface */ protected $formBuilder; /** * The entity repository. * * @var \Drupal\Core\Entity\EntityRepositoryInterface */ protected $entityRepository; /** * Constructs a new CartLazyBuilders object. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param \Drupal\Core\Form\FormBuilderInterface $form_builder * The form builder. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository * The entity repository. */ public function __construct(EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, EntityRepositoryInterface $entity_repository) { $this->entityTypeManager = $entity_type_manager; $this->formBuilder = $form_builder; $this->entityRepository = $entity_repository; } /** * {@inheritdoc} */ public static function trustedCallbacks() { return ['addToCartBundleForm']; } /** * Builds 'Bundle add to cart' form. * * @param $product_bundle_id * @param $view_mode * @param $combine * @param $langcode * * @return array * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * @throws \Drupal\Core\Form\EnforcedResponseException * @throws \Drupal\Core\Form\FormAjaxException */ public function addToCartBundleForm($product_bundle_id, $view_mode, $combine, $langcode) { /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */ $order_item_storage = $this->entityTypeManager->getStorage('commerce_order_item'); /** @var \Drupal\commerce_product_bundles\Entity\ProductBundleInterface $product_bundle */ $product_bundle = $this->entityTypeManager->getStorage('commerce_product_bundles')->load($product_bundle_id); // Load Product for current language. $product_bundle = $this->entityRepository->getTranslationFromContext($product_bundle, $langcode); // Get default bundle variations. $default_variation = $product_bundle->getDefaultVariation(); if (!$default_variation) { return []; } $order_item = $order_item_storage->createFromPurchasableEntity($default_variation); /** @var \Drupal\commerce_cart\Form\AddToCartFormInterface $form_object */ $form_object = $this->entityTypeManager->getFormObject('commerce_order_item', 'add_to_cart_bundle'); $form_object->setEntity($order_item); // The default form ID is based on the variation ID, but in this case the // product ID is more reliable (the default variation might change between // requests due to an availability change, for example). $form_object->setFormId($form_object->getBaseFormId() . '_commerce_product_bundles_' . $product_bundle_id); $form_state = (new FormState())->setFormState([ 'product_bundle' => $product_bundle, 'view_mode' => $view_mode, 'settings' => [ 'combine' => $combine, ], ]); return $this->formBuilder->buildForm($form_object, $form_state); } }