You are here

class NotFound in Unpublished 404 8

Redirects 403 node page error responses to 404 page.

Hierarchy

  • class \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
    • class \Drupal\unpublished_404\EventSubscriber\NotFound

Expanded class hierarchy of NotFound

1 string reference to 'NotFound'
unpublished_404.services.yml in ./unpublished_404.services.yml
unpublished_404.services.yml
1 service uses NotFound
unpublished_404.not_found in ./unpublished_404.services.yml
Drupal\unpublished_404\EventSubscriber\NotFound

File

src/EventSubscriber/NotFound.php, line 13

Namespace

Drupal\unpublished_404\EventSubscriber
View source
class NotFound extends HttpExceptionSubscriberBase {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $account;

  /**
   * Constructs a NotFound object.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $account
   *   The current user.
   */
  public function __construct(AccountProxyInterface $account) {
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  protected static function getPriority() {
    return 1000;
  }

  /**
   * {@inheritdoc}
   */
  protected function getHandledFormats() {
    return [
      'html',
    ];
  }

  /**
   * Handles all 4xx errors for all serialization failures.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function on403(ExceptionEvent $event) {

    // Check if current user has pemission to view own unpublished content.
    if ($this->account && !$this->account
      ->hasPermission('view own unpublished content')) {
      $request = $event
        ->getRequest();
      if ($node = $request->attributes
        ->get('node')) {
        if (!$node
          ->isPublished()) {
          $event
            ->setThrowable(new NotFoundHttpException());
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpExceptionSubscriberBase::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
HttpExceptionSubscriberBase::onException public function Handles errors for this subscriber.
NotFound::$account protected property The current user.
NotFound::getHandledFormats protected function Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase::getHandledFormats
NotFound::getPriority protected static function Specifies the priority of all listeners in this class. Overrides HttpExceptionSubscriberBase::getPriority
NotFound::on403 public function Handles all 4xx errors for all serialization failures.
NotFound::__construct public function Constructs a NotFound object.