0

I want get very close Views results, but image style should differ. It should be done by using different URLs or Views arguments. How I can reach that?

UPD: final solution:

/**
 * Return actual image_style of image depending on URL params.
 */
function _MYMODULE_image_style() {
  $allowed_image_styles = array('image_style_1', 'image_style_2');
  $params = drupal_get_query_parameters();
  if (isset($params['image_style']) && in_array($params['image_style'], $allowed_image_styles)) {
    $image_style = $params['image_style'];
  }
  return isset($image_style) ? $image_style : NULL;
}

/**
 * Implementation of hook_views_pre_execute().
 */
function MYMODULE_views_pre_execute(&$view) {
  if ($view->name == 'view_1' && $view->current_display == 'services_1') {
    $image_style = _mymodule_image_style();
    if ($image_style) {
      $field_name = 'field_vendor_logo';
      $view->display_handler->handlers['field'][$field_name]->options['settings']['image_style'] = $image_style; // <<< THIS IS the main part of solution
    }
  }
}
  • Create your default Views display, then clone it and change the path and the image style accordingly. – Paul Mar 19 '15 at 13:39
  • Yes, you are right, but is there any other solution? Because in this case I should duplicate each of my Views (I have several). – graceman9 Mar 19 '15 at 14:11
  • I guess there is no other way by just using Views UI. Of course, you can try to write a custom module using the views preprocess hooks. – Paul Mar 19 '15 at 14:19
2

Create different ImageStyles, If the image field of the content type is field_image then you can use this code:

        $variables['theme_hook_suggestions'][]='THE_THEME_HOOK_SUGGESTION';
        $wrapper = entity_metadata_wrapper('node',$variables['node']);
        $photo_field = $wrapper->field_image->value();
        $url = arg(1); // you can distinguish the address by arg(0),arg(1), ... 
        if ($url == "URL1") {
                $variables['image'] = theme('image_style',
                     array(
                        'style_name' => 'STYLE1',
                        'path' => $photo_field['uri'],
                    )
                );
        } else if ($url == "URL2"){
                $variables['image'] = theme('image_style',
                     array(
                        'style_name' => 'STYLE2',
                        'path' => $photo_field['uri'],
                    )
                );
        }

Make sure to put such code hook_preprocess_node, and also this code is run when the correspondent content type is loaded. then in the THEME_HOOK page of the content type use $image variable to display the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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