Altering image style for one image out of multiple

Created on 27 May 2022, about 2 years ago
Updated 15 November 2023, 7 months ago

In Blazy 2.5 I was able to alter the image style for the first image when rendering multiple media items, but that's no longer working in Blazy 2.11.

This is what I was doing before:

function mytheme_preprocess_field__commerce_product__field_media(&$variables) {
  // Switch the first product media item to "product_hero".
  if (!empty($variables['items'][0]['content'])) {
    $variables["items"][0]["content"]["#build"]["settings"]["image_style"] = 'product_hero';
  }
}

What is the right way to do this in 2.11? Stepping through the code it looks like Blazy already has everything built out in a blazies key.

💬 Support request
Status

Fixed

Version

2.11

Component

Miscellaneous

Created by

🇺🇸United States KarlShea Minneapolis, 🇺🇸

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸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's image.uri that does it.

  • Status changed to Fixed 7 months ago
  • 🇮🇩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.

Production build 0.69.0 2024