Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The views module <a href="http://views2.logrus.com/doc/html/group__views__hooks.html" rel="noreferrer">provides some hooks</a> for 'external' manipulations, just like Drupal core.</p> <p>You can implement <a href="http://views2.logrus.com/doc/html/group__views__hooks.html#gb88d5c49ec4ad30dba2be34327d5b0cc" rel="noreferrer"><code>hook_views_pre_render(&amp;$view)</code></a> within a custom module and manipulate the result array available in <code>$view-&gt;result</code>:</p> <pre><code>/** * Implementation of hook_views_pre_render() * * @param view $view */ function YourModuleName_views_pre_render(&amp;$view) { // Check if this is the view and display you want to manipulate // NOTE: Adjust/Remove the display check, if you want to manipulate some/all displays of the view if ('YourViewName' == $view-&gt;name &amp;&amp; 'YourDisplayName' == $view-&gt;current_display) { // EXAMPLE: Just reverse result order // TODO: Replace with your desired (re)ordering logic $view-&gt;result = array_reverse($view-&gt;result); } } </code></pre> <p>The hook is invoked in the middle of the view generation process, after all result data has been assembled, but before the actual output gets rendered, so changes to the result array will be reflected in the views final output.</p> <p><strong>EDIT:</strong> Alternatively, you could process the view 'manually', by copying the behavior of the <code>views_get_view_result()</code> function, but instead of returning the result, you manipulate it and continue to render the view:</p> <pre><code>function yourModule_get_custom_sorted_view($display_id = NULL) { // As the custom sorting probably only works for a specific view, // we 'demote' the former $name function parameter of 'views_get_view_result()' // and set it within the function: $name = 'yourViewName'; // Prepare a default output in case the view definition can not be found // TODO: Decide what to return in that case (using empty string for now) $output = ''; // Then we create the result just as 'views_get_view_result()' would do it: $args = func_get_args(); if (count($args)) { array_shift($args); // remove $display_id } $view = views_get_view($name); if (is_object($view)) { if (is_array($args)) { $view-&gt;set_arguments($args); } if (is_string($display_id)) { $view-&gt;set_display($display_id); } else { $view-&gt;init_display(); } $view-&gt;pre_execute(); $view-&gt;execute(); // 'views_get_view_result()' would just return $view-&gt;result here, // but we need to go on, reordering the result: $important_var = important_function(); $view-&gt;result = sorting_function($result, $important_var); // Now we continue the view processing and generate the rendered output // NOTE: $view-&gt;render will call $view-&gt;execute again, // but the execute method will detect that it ran already and not redo it. $output = $view-&gt;render(); // Clean up after processing $view-&gt;post_execute(); } return $output; } </code></pre> <p><strong>Note:</strong> This is a lot of code duplication and thus error prone - I do not recommend this and would rather go with the hook implementation above, trying to find a way to get access to your '$important_var' from within that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload