Got this message in one of my views

Notice: Undefined variable: group_name in resp_img_field_attach_view_alter() (line 261 of /var/www/vhosts/pcmarket.net.au/httpdocs/sites/all/modules/resp_img/resp_img.module).

Comments

attiks’s picture

It's a known problem, we're going to backport the things from picture (D8) to either resp_img or picture

to be continued

jaffaralia’s picture

Status: Active » Needs review

Hi,
I was using this module in my project. After i changed the code notice message got disappear. Here is my code.

function resp_img_field_attach_view_alter(&$output, $context) {
  foreach (element_children($output) as $field_name) {
    $element = &$output[$field_name];
    $vars = array();
    if (isset($element['#formatter'])) {
      if ($element['#formatter'] == 'image') {
        $vars['image_style'] = 'image_style';
        $vars['#formatter'] = 'picture';
        $vars['#theme'] = 'picture_formatter';
      }
      elseif ($element['#formatter'] == 'colorbox') {
        $vars['image_style'] = 'colorbox_node_style';
        $vars['#formatter'] = 'picture';
        $vars['#theme'] = 'colorbox_picture_formatter';
      }

      if (!empty($vars)) {
        $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
        if (isset($instance['display'][$context['view_mode']])) {
          $settings = $instance['display'][$context['view_mode']]['settings'];
        }
        else {
          $settings = $instance['display']['default']['settings'];
        }
        $breakpoint_styles = array();
        $first_image_style = '';
        if (strpos($settings[$vars['image_style']], RESP_IMG_STYLE_PREFIX) !== FALSE) {
          $group_name = substr($settings[$vars['image_style']], strlen(RESP_IMG_STYLE_PREFIX));
          if ($mappings = resp_img_mapping_load('mappings.' . $group_name)) {
            foreach ($mappings as $breakpoint_name => $multipliers) {
              if (!empty($multipliers) && is_array($multipliers)) {
                foreach ($multipliers as $multiplier => $image_style) {
                  if (!empty($image_style)) {
                    if (empty($first_image_style)) {
                      $first_image_style = $image_style;
                    }
                    if (!isset($breakpoint_styles[$breakpoint_name])) {
                      $breakpoint_styles[$breakpoint_name] = array();
                    }
                    $breakpoint_styles[$breakpoint_name][$multiplier] = $image_style;
                  }
                }
              }
            }
          }
          if (!empty($breakpoint_styles)) {
            // Change the formatter so it uses ours.
            $element['#formatter'] = $vars['#formatter'];
  
            // Change the formatter on all items as well.
            $num_fields = count($element['#items']);
            for ($delta = 0; $delta < $num_fields; $delta++) {
              $element[$delta]['#theme'] = $vars['#theme'];
              // Change the image style to the first in use.
              $element[$delta]['#image_style'] = $first_image_style;
              $element[$delta]['#breakpoints'] = $breakpoint_styles;
            }
          }
          else {
            watchdog('Responsive images', t('You have to map at least 1 style for %group.', array('%group' => $group_name)));
          }
        }
      }
    }
  }
}
attiks’s picture

Can you provide a patch so i can see what's different?

jaffaralia’s picture

Apply attached patch to remove notice message.

What the patch does?

Notice error occurred when there was no responsive image style used for any image. Moving the code with in the below if condition resolves the same.
if (strpos($settings[$vars['image_style']], RESP_IMG_STYLE_PREFIX) !== FALSE)

Also moved the watchdog message as the else for this if condition

jaffaralia’s picture

Status: Needs review » Closed (fixed)

It's fixed in latest dev version.