Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 672843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:31:47+00:00 2026-05-14T00:31:47+00:00

I’m trying to take the results of a view – using the function views_get_view_result()

  • 0

I’m trying to take the results of a view – using the function views_get_view_result() – and sort the array in a way I couldn’t do from within the Views interface. So far so good. I’ve got a $rows variable with all of the stuff I need.

Now… How do I put it back? 🙂 Before I needed this sort, I used views_embed_view(), but I can’t do that anymore.

Grateful for any help on this, feels like I’m so close to cracking it!

$important_var = important_function();
$result = views_get_view_result($view, $display, $args);
$result = sorting_function($result, $important_var);

//TODO: Put the result back into the view
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:31:47+00:00Added an answer on May 14, 2026 at 12:31 am

    The views module provides some hooks for ‘external’ manipulations, just like Drupal core.

    You can implement hook_views_pre_render(&$view) within a custom module and manipulate the result array available in $view->result:

    /**
     * Implementation of hook_views_pre_render()
     *
     * @param view $view
     */
    function YourModuleName_views_pre_render(&$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->name && 'YourDisplayName' == $view->current_display) {
        // EXAMPLE: Just reverse result order
        // TODO: Replace with your desired (re)ordering logic
        $view->result = array_reverse($view->result);
      }
    }
    

    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.

    EDIT: Alternatively, you could process the view ‘manually’, by copying the behavior of the views_get_view_result() function, but instead of returning the result, you manipulate it and continue to render the view:

    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->set_arguments($args);
        }
        if (is_string($display_id)) {
          $view->set_display($display_id);
        }
        else {
          $view->init_display();
        }
        $view->pre_execute();
        $view->execute();
        // 'views_get_view_result()' would just return $view->result here,
        // but we need to go on, reordering the result:
        $important_var = important_function();
        $view->result = sorting_function($result, $important_var);
        // Now we continue the view processing and generate the rendered output
        // NOTE: $view->render will call $view->execute again,
        // but the execute method will detect that it ran already and not redo it.
        $output = $view->render();
        // Clean up after processing
        $view->post_execute();
      }
    
      return $output;
    }
    

    Note: 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.