Score:0

How can I programmatically check if a view's result is not empty?

gs flag

Programmatically, How to check if view result is not empty with in custom function? I want to check the views result is it empty or not, and then display the result. How can i do?

use Drupal\views\Views;
use Drupal\views\ViewExecutable;

$view = Views::getView($view_name = "My View");
$view->setTitle('Sample Title');
$view->setArguments($args = ['all']);
$view->display_handler->setOption('empty', $empty = 'No content');
$view->preExecute();

// Need to check if empty result, before render

$markup = $view->render();
No Sssweat avatar
ua flag
use `$view->execute();` and `if (!empty($view->result)) { ...`
Thirsty Six avatar
gs flag
Yes, it's working fine.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.