- 🇺🇸United States karlshea Minneapolis 🇺🇸
hook_blazy_alter()
no longer works, which is surprising since this wasn't a major version upgrade. - 🇺🇸United States karlshea Minneapolis 🇺🇸
Or I guess it does, but everything totally moved around:
Previous
function custom_blazy_alter(array &$build, array $settings = []) { if ( $settings['entity_type'] === 'commerce_product' && $settings['field_name'] === 'field_media' && $settings['view_mode'] === 'default' && $build['#delta'] === 0 ) { /** @var \Drupal\blazy\BlazySettings $blazies */ $blazies = $build['#build']['settings']['blazies']; $product_hero_style = ImageStyle::load('product_hero'); $blazies->set('image.style', $product_hero_style); } }
At some point now it needs to be
function custom_blazy_alter(array &$build, array $settings = []) { /** @var \Drupal\blazy\BlazySettings $blazies */ $blazies = $settings['blazies']; $field = $blazies->get('field'); if ( $field['entity_type'] === 'commerce_product' && $field['view_mode'] === 'full' && $field['name'] === 'field_media' && $build['#delta'] === 0 ) { $product_hero_style = ImageStyle::load('product_hero'); $blazies->set('image.style', $product_hero_style); } }
- 🇮🇩Indonesia gausarts
Hm, might be borked somewhere, not sure :)
Things moved very fast during dev storms last time.
However there is a new hook_blazy_item_alter:
https://git.drupalcode.org/project/blazy/-/blob/8.x-2.21/blazy.api.php?r...I found it more versatile for halfway alter, that is when you have enough info to target item alteration. Seen and proven at zooming.module for sample IIRC.
Hope that is closer and more effective than hook_blazy_alter since you don't have to manually load image stylenper item as it is already loaded at container once.
Let me know if any more info needed?
- 🇺🇸United States karlshea Minneapolis 🇺🇸
hook_blazy_item_alter
does not work, the image style is not changed. - 🇺🇸United States karlshea Minneapolis 🇺🇸
Here's what I tried:
function custom_blazy_item_alter(array &$settings, array &$attributes, array &$item_attributes) { /** @var \Drupal\blazy\BlazySettings $blazies */ $blazies = &$settings['blazies']; $field = $blazies->get('field'); // Switch the first product media item to "product_hero". if ( $field['entity_type'] === 'commerce_product' && $field['view_mode'] === 'full' && $field['name'] === 'field_media' && $blazies->get('delta') === 0 ) { $product_hero_style = ImageStyle::load('product_hero'); $blazies->set('image.style', $product_hero_style); } }
- 🇮🇩Indonesia gausarts
Perhaps the image URL needs settings too:
if ($blazies->get('delta') == 1) { $style = blazy()->load('thumbnail', 'image_style'); $uri = $blazies->get('image.uri'); $url = \Drupal\blazy\Blazy::url($uri, $style); // The $settings might not kick in, but in case needed downstream, set it. $settings['image_style'] = 'thumbnail'; $blazies->set('image.style', $style) ->set('image.url', $url); }
- 🇺🇸United States karlshea Minneapolis 🇺🇸
That worked, thank you!
Setting
image.style
doesn't appear to change anything, it'simage.uri
that does it. - Status changed to Fixed
over 1 year ago 11:16pm 15 November 2023 - 🇮🇩Indonesia gausarts
Things changed since 2.5. That would do for now :)
Thanks for the follow up.
Automatically closed - issue fixed for 2 weeks with no activity.