1

In Drupal 10, I want to create a custom block plugin. Blocks instances of this new block type would be placed in a region on a View page.

How can I gain access to the View object from a programmatic block? I had thought perhaps adding a ContextDefinition in the block annotation, but I don't see that View is a possible option.

In the block I'd like to have access to the $view->result array, after the View has been executed.

Any thoughts?

2 Answers 2

2

To Get View object in custom block plugin you can follow this snippet code: (Drupal 9/10)

use Drupal\views\Views;


// Get current route object.
  $route = \Drupal::routeMatch()->getRouteObject();
  if ($route) {
    // Get view id & display id from current route.
    $view_id = $route->getDefault('view_id');
    $display_id = $route->getDefault('display_id');
    if (!empty($view_id) && !empty($display_id)) {
      // Get the view by id.
      $view = Views::getView($view_id);
      if ($view) {
        // Set display id.
        $view->setDisplay($display_id);
        // Run execute() to get result count.
        $view->execute();
        // Get view result count.
        $result_count = $view->total_rows;
      }
    }
  }

Note: Don't skip $view->execute() otherwise result_count 'll be render null despite of having value

If the code works fine the can do code optimization, like instead of calling directly APIs like below: can use dependency injection

\Drupal::routeMatch()->getRouteObject();

Reference link on dependency injection in a plugin (block)

4
  • 1
    This is a good answer. I'd like to have the View object after the query is executed, so the View doesn't have to execute twice, but I can't see how to do that. Sep 26 at 22:02
  • Well , that's nice question from site optimisation perspective as well, however as you see above code basically get current view page id, then get current view object and execute view to get executed view results. If there is API() function provided by Drupal core to get view object from current view page (for eg as it is provided for node page to get node object from current node page) then it can be possible to get view object after view executed.
    – mini
    Sep 29 at 15:59
  • If you want to know view is executed or not then you can try this code: $view->executed; where $view denotes view object
    – mini
    Sep 29 at 16:00
  • I was using the views_remote_data module, to wrap API results to a View. SO I cached the API result by url parameters, then I could load the View programmatically as many times on the same page, and at least the API call was cached, so achieved the effect I needed Oct 16 at 21:39
0

I am not sure I follow your question entirely, but when you make a views page, you should have all of the regions all of your other pages have, you just put your block in there like you would any other page -- unless you have changed the theme for that page and then you need to look to see what regions that theme is providing.

7
  • Yeah, I'm making a custom block plugin, and in the code for that, I want access to the View object, so I can programmatically sift through the results, and do some custom output in the block. I also don't want the View executed twice. Sep 22 at 14:28
  • Imagine like a facets block but for a normal view. Sep 22 at 14:31
  • You may want to add that clarification to the ticket :) Sep 22 at 14:46
  • I think its impossible but we'll see. I'll ask ChatGPT too. stackexchange vs. AI grudge match. Lets go. Sep 22 at 14:48
  • You may want to consider something like views json drupal.org/project/views_json_query it will provide you with an api to get your views results that you can consume in your block code Sep 22 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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