Drupal 9 image field migration

You can add images to Flyout cart https://www.drupal.org/project/commerce_cart_flyout to make look better! Use the same approach from previous article, adding normalizer for Variations.
Variations normalization. Add to services.yml in your module

  my_commerce.cart_normalizer.product_variation:
    class: Drupal\my_commerce\Normalizer\VariationCartNormalizer
    tags:
      - { name: normalizer, priority: 2 }

In my case Product entity has an image, so we take it from there.

    $images = $entity->getProduct()->field_images;
    if (!empty($images) && isset($images->entity)) {
      $original_image = $images->entity->getFileUri();
      $style = \Drupal::entityTypeManager()
        ->getStorage('image_style')
        ->load('thumbnail');
      $destination = $style->buildUri($original_image);
      if (!file_exists($destination)) {
        $style->createDerivative($original_image, $destination);
      }
      $attributes['image'] = $style->buildUrl($original_image);
    }

https://gist.github.com/onesixromcom/ba8ce1858af4a85d31283800ea87d4d2

Now you can use in commerce-cart-flyout-offcanvas-contents-items.html.twig template your image

<% _.each(cart.order_items, function(orderItem, key) { %>
    <img src="<%- orderItem.purchased_entity.image %>" width="70px" height="70px" />
<% }) %>

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
 oooooooooo.                .o8                     ooooo             .   
`888' `Y8b "888 `888' .o8
888 888 .oooooooo 888oooo. oooo oooo 888 .o888oo
888oooo888' 888' `88b d88' `88b `888 `888 888 888
888 `88b 888 888 888 888 888 888 888 888
888 .88P `88bod8P' 888 888 888 888 888 o 888 .
o888bood8P' `8oooooo. `Y8bod8P' `V88V"V8P' o888ooooood8 "888"
d" YD
"Y88888P'
Enter the code depicted in ASCII art style.