You are here

class ProductLazyBuilders in Commerce Core 8.2

Provides #lazy_builder callbacks.

Hierarchy

Expanded class hierarchy of ProductLazyBuilders

1 string reference to 'ProductLazyBuilders'
commerce_product.services.yml in modules/product/commerce_product.services.yml
modules/product/commerce_product.services.yml
1 service uses ProductLazyBuilders
commerce_product.lazy_builders in modules/product/commerce_product.services.yml
Drupal\commerce_product\ProductLazyBuilders

File

modules/product/src/ProductLazyBuilders.php, line 14

Namespace

Drupal\commerce_product
View source
class ProductLazyBuilders 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 ProductLazyBuilders 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;
  }

  /**
   * Builds the add to cart form.
   *
   * @param string $product_id
   *   The product ID.
   * @param string $view_mode
   *   The view mode used to render the product.
   * @param bool $combine
   *   TRUE to combine order items containing the same product variation.
   * @param string $langcode
   *   The langcode for the language that should be used in form.
   *
   * @return array
   *   A renderable array containing the cart form.
   */
  public function addToCartForm($product_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\Entity\ProductInterface $product */
    $product = $this->entityTypeManager
      ->getStorage('commerce_product')
      ->load($product_id);

    // Load Product for current language.
    $product = $this->entityRepository
      ->getTranslationFromContext($product, $langcode);
    $default_variation = $product
      ->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');
    $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_' . $product_id);
    $form_state = (new FormState())
      ->setFormState([
      'product' => $product,
      'view_mode' => $view_mode,
      'settings' => [
        'combine' => $combine,
      ],
    ]);
    return $this->formBuilder
      ->buildForm($form_object, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'addToCartForm',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProductLazyBuilders::$entityRepository protected property The entity repository.
ProductLazyBuilders::$entityTypeManager protected property The entity type manager.
ProductLazyBuilders::$formBuilder protected property The form builder.
ProductLazyBuilders::addToCartForm public function Builds the add to cart form.
ProductLazyBuilders::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
ProductLazyBuilders::__construct public function Constructs a new ProductLazyBuilders object.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.