0

I am building a site that uses Drupal Commerce 2 and I am attempting to build out the product details template. Our product has a variation (field_accessories) which is an entity reference to another product and I am trying to access the title, the images (first one actually), and the link to the product's detail page.

I got the title showing with

{{ product['field_accessories'][0]['#commerce_product_variation'].getTitle() }}

and I can get the sku and price with:

{{ product['field_accessories'][0]['#commerce_product_variation'].getPrice() }}
{{ product['field_accessories'][0]['#commerce_product_variation'].getSku() }}

But I need the images and url link. Based on the available methods from using kint on the product['field_accessories'][0]['#commerce_product_variation'] I tried:

{{ product['field_accessories'][0]['#commerce_product_variation'].get('field_images') }}

and

{{ product['field_accessories'][0]['#commerce_product_variation'].getField('field_images') }}

and

{{ product['field_accessories'][0]['#commerce_product_variation'].getField('images') }}

but nothing is working and all come back with null.

When I do a product['field_accessories'][1]['#commerce_product_variation'].getFields() I see the following:

field from getFields

Any ideas on how to access the field_images and url to that product's page? Thanks and let me know!

1
  • Ok I manager to get to the Drupal\image\Plugin\Field\FieldType\ImageItem using product.field_accessories[0]['#commerce_product_variation'].get('field_images')[0] but I can't find the URL path for the image. Commented Mar 23, 2020 at 15:11

1 Answer 1

1

Here is the answer that worked for me:

product.field_accessories[0]['#commerce_product_variation'].get('field_images')[0].entity.fileuri)

And I looped over the product.field_accessories to get just the first image for each product

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.