D9: UntrustedCallbackException

Created on 14 April 2021, over 4 years ago
Updated 18 February 2023, over 2 years ago

The variations are not displayed

Drupal\Core\Security\UntrustedCallbackException: Render #lazy_builder callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was

Drupal\dc_ajax_add_cart\ProductLazyBuilders::ajaxAddToCartForm.

See https://www.drupal.org/node/2966725 β†’ in Drupal\Core\Render\Renderer->doTrustedCallback() (Zeile 96 in /var/www/vhosts/mydomain.com/httpdocs/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php)

πŸ› Bug report
Status

RTBC

Version

1.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany umac_de

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom alexku

    Same issue as in comment #9.

    $product_id is empty on Manage layout page of a product type (admin/commerce/config/product-types/default/edit/display/full)

    Adding a check if $product_id is empty resolves the issue

    public function ajaxAddToCartForm($product_id, $view_mode, $combine) {
        /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
        $order_item_storage = $this->entityTypeManager->getStorage('commerce_order_item');
    
        if (empty($product_id)) {
          return [];
        }
        /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
        $product = $this->entityTypeManager->getStorage('commerce_product')->load($product_id);
        $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', 'dc_ajax_add_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);
      }
    
  • πŸ‡ΊπŸ‡ΈUnited States bob.hinrichs

    Thanks for this info, is a patch or update available that fixes #12 also?

  • Status changed to Needs work over 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States bob.hinrichs

    I am sorry to change status, but in order for this to work, I believe the adjustments in #12 would be necessary in order to avoid the errors reported?

  • πŸ‡ͺπŸ‡ΈSpain alvarodemendoza

    Updated patch with comments in #12

  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    I think this has been incorporated into the MR at πŸ“Œ Drupal 10 update Needs review

    Might be a duplicate a this point

  • I suggest merging the MR in πŸ“Œ Drupal 10 update Needs review as it incorporates this issues' changes as well as the D10 compatibility changes from πŸ“Œ Drupal 10 update Needs review .

  • Issue was unassigned.
  • Status changed to Closed: duplicate 4 days ago
  • πŸ‡ΊπŸ‡¦Ukraine marchuk.vitaliy Rivne, UA

    Fixed in this issue https://www.drupal.org/project/dc_ajax_add_cart/issues/3541983 πŸ“Œ Drupal 10 and Commerce 2.4/3.x compatibility fixes Active and will be available in version 3.0.0.

Production build 0.71.5 2024