You are here

function resp_img_field_attach_view_alter in Responsive images and styles 8

Same name and namespace in other branches
  1. 7.2 resp_img.module \resp_img_field_attach_view_alter()

Implements hook_preprocess_field().

File

./resp_img.module, line 55

Code

function resp_img_field_attach_view_alter(&$output, $context) {
  foreach (element_children($output) as $field_name) {
    $element =& $output[$field_name];
    if ($element['#field_type'] == 'image') {
      $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();
      if (isset($settings['resp_img_responsive']) && $settings['resp_img_responsive']) {
        foreach ($settings['resp_img'] as $breakpoint_name => $multipliers) {
          if (is_array($multipliers)) {
            foreach ($multipliers as $multiplier => $image_style) {
              if (!empty($settings['resp_img'][$breakpoint_name][$multiplier])) {
                if (!isset($breakpoint_styles[$breakpoint_name])) {
                  $breakpoint_styles[$breakpoint_name] = array();
                }
                $breakpoint_styles[$breakpoint_name][$multiplier] = $image_style;
              }
            }
          }
        }
      }

      // Change the formatter so it uses ours.
      $element['#formatter'] = 'picture';

      // Change the formatter on all items as well.
      $num_fields = count($element['#items']);
      if (!empty($breakpoint_styles)) {
        for ($delta = 0; $delta < $num_fields; $delta++) {
          $element[$delta]['#theme'] = 'picture_formatter';
          $element[$delta]['#breakpoints'] = $breakpoint_styles;
        }
      }
      else {
        for ($delta = 0; $delta < $num_fields; $delta++) {
          $element[$delta]['#theme'] = 'picture_formatter';
          $element[$delta]['#breakpoints'] = 'all';
        }
      }
    }
  }
}