0

I am trying to get number of rows in a view by executing this function in my custom module in Drupal 9.3.x:

function module_custom_view_count () {

    $view = Views::getView('site_images_page_1');
    $view->setItemsPerPage(0);
    $arguments = array(1,176);
    $view->setArguments($arguments);
    $view->setDisplay('page_1');
    $view->execute();
    $total_rows = count($view->result);
    
    if ($total_rows) {
    \Drupal::logger('module_custom')->notice($total_rows);
    }
}

And I am getting this error:

TypeError: Argument 1 passed to Drupal\views\ViewExecutable::setRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given, called in /core/modules/views/src/ViewExecutableFactory.php on line 72 in Drupal\views\ViewExecutable->setRequest() (line 1804 of /home/ulhyooam/domains/guide/core/modules/views/src/ViewExecutable.php)

The problem is in Views::getView(), because it goes away as soon as I remove this. Removing other lines won't make any difference. Any ideas?

3
  • 1
    For some reason, there is no request on the stack when you get the View api.drupal.org/api/drupal/… Jul 30 at 4:34
  • It seems that module_custom_view_count() is called in the wrong moment. What function is calling module_custom_view_count()? More exactly, may you show the line that is calling module_custom_view_count()?
    – apaderno
    Jul 30 at 10:22
  • @apaderno I am calling this function right below it in the same file using this line: module_custom_view_count();
    – Sami
    Jul 31 at 20:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.