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.
CAPTCHA
                  .ooo     .o88o.  oooooo   oooooo     oooo    .oooooo.                
.88' 888 `" `888. `888. .8' d8P' `Y8b
oooo ooo d88' o888oo `888. .8888. .8' 888 888 .oooo.o
`88b..8P' d888P"Ybo. 888 `888 .8'`888. .8' 888 888 d88( "8
Y888' Y88[ ]88 888 `888.8' `888.8' 888 888 `"Y88b.
.o8"'88b `Y88 88P 888 `888' `888' `88b d88b o. )88b
o88' 888o `88bod8' o888o `8' `8' `Y8bood8P'Ybd' 8""888P'


Enter the code depicted in ASCII art style.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.