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.