Creating a view access plugin that returns false in a REST view returns a 200 HTTP code instead of 401

Created on 13 November 2016, almost 9 years ago
Updated 30 July 2025, 2 months ago

Creating a view access plugin that returns false in a REST view returns a 200 HTTP code and empty content instead of the expected 401 HTTP return code. Here is a sample class.

/**
 * Access plugin.
 *
 * @ingroup views_access_plugins
 *
 * @ViewsAccess(
 *   id = "rest_access",
 *   title = @Translation("Rest Access"),
 * )
 */
class RestAccess extends AccessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account) {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function alterRouteDefinition(Route $route) {
    // Allow by default, and have $this->access() function resolve access
    $route->setRequirement('_access', 'TRUE');
  }

}
πŸ’¬ Support request
Status

Fixed

Version

8.2 ⚰️

Component

views.module

Created by

πŸ‡²πŸ‡½Mexico jackbravo

Live updates comments and jobs are added and updated live.
  • VDC

    Related to the Views in Drupal Core initiative.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024