1

When you have a view page that takes a contextual filter, you can easily set the no result behavior to a 404. But I am trying to do this when I have a view pane, in a page manager page like in the screenshot attachedenter image description here

If the view has no result (contextual fitter does not validate) I would like the page to return a 404.

2

You can implement hook_views_pre_render in your module to do this task

/**
 * Implements hook_views_pre_render().
 */
 function mymodule_views_pre_render(&$view) {
   if ($view->name == 'myview' && $view->current_display == 'mypane') {
     if (empty($view->result)) {
       drupal_not_found();
     }
  }
}
0

@Andrew's answer adopted to Drupal 8:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Implements hook_views_pre_render().
 */
function mymodule_views_pre_render(&$view) {
    if (
            array_key_exists('title', $view->build_info)
            && $view->build_info['title'] == 'My View'
            && $view->current_display == 'mypane'
    ) {
        if (empty($view->result)) {
            throw new NotFoundHttpException();
        }
    }
}

Your Answer

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

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