You are here

class HandlerFor403AccessDenied in moderated content bulk publish 1.0.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/HandlerFor403AccessDenied.php \Drupal\moderated_content_bulk_publish\EventSubscriber\HandlerFor403AccessDenied
  2. 2.0.x src/EventSubscriber/HandlerFor403AccessDenied.php \Drupal\moderated_content_bulk_publish\EventSubscriber\HandlerFor403AccessDenied

Hierarchy

Expanded class hierarchy of HandlerFor403AccessDenied

1 string reference to 'HandlerFor403AccessDenied'
moderated_content_bulk_publish.services.yml in ./moderated_content_bulk_publish.services.yml
moderated_content_bulk_publish.services.yml
1 service uses HandlerFor403AccessDenied
moderated_content_bulk_publish.exception403.subscriber in ./moderated_content_bulk_publish.services.yml
Drupal\moderated_content_bulk_publish\EventSubscriber\HandlerFor403AccessDenied

File

src/EventSubscriber/HandlerFor403AccessDenied.php, line 11

Namespace

Drupal\moderated_content_bulk_publish\EventSubscriber
View source
class HandlerFor403AccessDenied extends HttpExceptionSubscriberBase {
  protected $currentUser;
  public function __construct(AccountInterface $current_user) {
    $this->currentUser = $current_user;
  }
  protected function getHandledFormats() {
    return [
      'html',
    ];
  }
  public function on403(GetResponseForExceptionEvent $event) {
    $request = $event
      ->getRequest();
    $is_anonymous = $this->currentUser
      ->isAnonymous();
    $route_name = $request->attributes
      ->get('_route');
    $is_not_login = $route_name != 'user.login';
    $current_path = \Drupal::service('path.current')
      ->getPath();
    if (stripos($current_path, 'latest') > 1 && stripos($current_path, 'ode') > 0) {
      $langId = \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId();
      $new_path = $current_path;

      // Fait de la magique ici.
      // Latest revision doesn't exist in this language, redirect to node page.
      $new_path = str_replace('/latest', '', $current_path);
      \Drupal::logger('moderated_content_bulk_publish')
        ->notice(utf8_encode('HandlerFor403AccessDenied: Redirecting from ' . $current_path . ' to ' . $new_path));
      $base_url = \Drupal::request()
        ->getSchemeAndHttpHost();
      $returnResponse = new \Drupal\Core\Routing\TrustedRedirectResponse($base_url . '/' . $langId . $new_path);

      // TODO: figure out how to do this the Drupal 8 way for internal path but didn't because of language afterthought in Drupal 8.
      $event
        ->setResponse($returnResponse);
      return;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HandlerFor403AccessDenied::$currentUser protected property
HandlerFor403AccessDenied::getHandledFormats protected function Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase::getHandledFormats
HandlerFor403AccessDenied::on403 public function
HandlerFor403AccessDenied::__construct public function
HttpExceptionSubscriberBase::getPriority protected static function Specifies the priority of all listeners in this class. 5
HttpExceptionSubscriberBase::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
HttpExceptionSubscriberBase::onException public function Handles errors for this subscriber. 1