2

I dont know if im over thinking this but im not making much progress with my function. I am trying to get the values from views rows in my modules for example if I was to use $row->field_name inside of a view.tpl file then it would show the output of that row, except I am trying to do it in a module. Ultimately I am trying to render the image style of the 1st image that loads from the view. This is what I have so far.

function custom_module_views_pre_render(&$vars){
    if($vars->name == 'front_page'){
        $vars->result[0]->field_field_image[0]['rendered']['#image_style'] = 'front-page-scroll';           
    }
}

Update 1: I've attached a image of what I am trying to do seeing how when I use preview(); execute(); or render(); it makes the function stop working, actually it makes the whole site stop working. If I cant get to the field view result count which counts the the number a current node is then I can say if($views->view_result_counter == 1) then render just that node. The function would look something like this.

function custom_module_views_pre_render(&$vars){
    $views = views_get_current_view('front_page', 'page');
    if($views->name == 'front_page'){   
         if($views->views_result_counter == 1){
            $vars->result[0]->field_field_image[0]['rendered']['#image_style'] = 'front-page-scroll';
         }
    }
}

Here is a image of what I am trying to do, Now I currently have the 1st image rendering as I want it to but it keeps rendering the image of each page instead of it rendering one time only.

enter image description here

Now as for my view is there any reason why execute or render wouldnt work? this is what my view looks like. enter image description here

any suggestions?

  • I see from your comment that you are using the Infinite scroll module, and from the image of your view, the Masonry module. Have you seen this issue? I am also having trouble understanding exactly what you are doing. In your image are you showing the start of the next page at the bottom? Is your module just changing the display of the first item in a page? Is each image a page? Are you trying to change the display of the first page? Could you expand your requirement? – Triskelion Mar 21 '13 at 0:15
  • I havent seen that post but I dont think it applies to this problem, basically what I am trying to do is render the very 1st image of the 1st page and no other images, right now when I do it, it renders the 1st image on every page instead of just the 1st one. – Jeremy Love Mar 21 '13 at 3:10
1
+50

I think you are mixing the hook_views_pre_render with a themeing function. In the hook, there is no $vars, you get access directly to the $view - the entire view, not only a row. So you will be able to do something along the lines of:

function YOURMOD_views_pre_render($view) {
  if ($view->name == 'YOURVIEW' 
    && $view->current_display == 'YOUR_DISPLAY'
    && $view->query->pager->current_page == 0) {
    $row = $view->result[0];
    $row->field_YOUR_image[0]['rendered']['file']['#style_name'] = 'DIFFERENT_style';
  }
}

This way you just change the image style of the image and you can let drupal do the rest for you :).

  • Your function is pretty much the same as the one I currently have. It renders the 1st image of every page instead of just rendering the image on the 1st page which is what I am trying to achieve here. – Jeremy Love Mar 22 '13 at 16:53
  • sorry :) then you could check for current_page i guess (edited my answer above, not sure if its 1 or 0). I just wanted to stress out you dont have to views_get_current_view - you have the view already :) – mojzis Mar 22 '13 at 18:38
  • one more edit, this time it should really work :) use $view->query->pager->current_page instead. – mojzis Mar 22 '13 at 18:45
1

as this site is not about php programming I will not go very deep into this. but what you are probably looking for is http://api.drupal.org/api/views/views.module/function/views_get_view_result/7

to render the view use $view->render();

further reference: http://drupal.org/node/342132

  • Question about PHP are off topic, but answers with working code or roughed out code are always welcome and encouraged. – mpdonadio Mar 16 '13 at 2:00
  • Thank you, Alhough it is php, I was looking more so for the right hook function to go about using. – Jeremy Love Mar 16 '13 at 3:27
1

You might be over thinking this. If you have working code, but the problem is that the code keeps on working, then the issue is to stop it from working again after it has done its job once.

The way to make a function aware if its previous iterations is to use a static variable. Try wrapping your working code with a static variable and test it like:

  // Only process one image.
  static $first_image_done = FALSE;
  if ( !$first_image_done ) {
    ...working code ...
    $first_image_done = TRUE;
  }

That way the function knows it has already processed the first image, and will skip the process on further iterations.

EDIT after time in chat:

The satic variable approach is not working because infinite scroll still works as a pager, and separate queries are being made for each page of output.

The current page position is controlled through the $pager (object) property of the $views object. The test for the first page can be accomplished as follows:

  $pager = $vars->pager;
  // Check for 'first page' condition
  if ( !isset($pager->current_page) || $pager->current_page == 0) {
     $vars->result[0]->field_field_image[0]['rendered']['#image_style'] = 'front-page-scroll'; 
  }

This should limit the image resizing to the first page of the view.

  • I wish i was over thinking it but this simply isnt the case, I tried using static to make the function only execute once and it executes every time a new page is loaded, this time I tried your version as well and it didn't work. Any other suggestions? – Jeremy Love Mar 21 '13 at 17:26
  • If the static variable is not working, then the program is being bootstrapped between pages. The view must still keep track of its pagination. Try placing watchdog('custom module' '<pre>' . print_r($vars) . '</pre>') within your code. Load two pages and look at your log to see how the view tracks its location. Then you know which property to test. – Triskelion Mar 21 '13 at 18:26
  • Thanks for replying, Ive actually tried this a few times and in the data its not printing out the views results that are being displayed on screen like the image field and the result counter, its prints out the data of the nodes and the settings of the views, but I cant specify which node to render if the latest node is always being changed. Any other suggestions? – Jeremy Love Mar 21 '13 at 19:39
  • If you check out the API your function would be better written as function custom_module_views_pre_render(&$view) and invoking views_get_current_view should not be unnecessary. I also assumed that the image you present above is what you are getting, perhaps mistakenly. I think we are still working at cross purpose. Perhaps we can move this to chat and try to drill down in the issue. – Triskelion Mar 21 '13 at 23:06
  • sure, i am in the drupal chat chat.stackexchange.com/rooms/327/drupal-answers – Jeremy Love Mar 21 '13 at 23:09
0

I'm not exactly entirely following why you're working in your module.

Have you tried using 2 displays in your View? 1 regular display with a limit of 1 row, and an attachment display, displaying all results, but with an offset of 1, so that it skips the 1st row. You can then change the Image preset on the regular View, but keep using thumbnail preset in the attachment View.

Or - I can't try it out right now - isn't there a counter somewhere in the $vars variable that contains what page you're on? If so, you can only intercept & change the row when you're on the 1st page

  • ive, tried those and your idea about the counter is a good idea however it wont work since the pagination is controlled by using infinite scroll views module – Jeremy Love Mar 20 '13 at 22:14

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.