Skip to content

Commit

Permalink
Drupal coding standards and disabled frontpage redirecting on sites w…
Browse files Browse the repository at this point in the history
…hen multi-lingual
  • Loading branch information
danjdewhurst committed Feb 13, 2019
1 parent 63f9cd1 commit 42cb0fd
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 284 deletions.
64 changes: 31 additions & 33 deletions src/EventSubscriber/KernelRequestSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @file
* Contains \Drupal\starter\EventSubscriber\KernelRequestSubscriber.
*/

namespace Drupal\starter\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -14,14 +9,14 @@
use Symfony\Component\HttpFoundation\Response;

/**
* Subscribe to KernelEvents::REQUEST events and throw a 404 if content should not be accessed directly
* Subscribe to KernelEvents::REQUEST events and throw a 404 if content should not be accessed directly.
*/
class KernelRequestSubscriber implements EventSubscriberInterface {

private $config;

/*
* get module configuration
/**
* Get module configuration.
*/
public function getConfig($config) {
$this->config = $config->get('starter.settings');
Expand All @@ -30,48 +25,51 @@ public function getConfig($config) {
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('disableDirectAccess');
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = ['disableDirectAccess'];
return $events;
}

/**
* Disable all direct access of content set with the special disable_access route alias
* Also prevent homepage being accessed from anything other than the base path
* Also redirect any aliased pages to their aliased path
* Only applies to anonymous users
* Only applies to anonymous users.
*/
public function disableDirectAccess(GetResponseEvent $event) {

$anonymous = \Drupal::currentUser()->isAnonymous();
$anonymous = \Drupal::currentUser()->isAnonymous();

// target anonymous users and master request (important)
if($anonymous===true && $event->isMasterRequest()) {
$current_path = \Drupal::service('path.current')->getPath();
$alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$alias_parts = explode('/', trim($alias,'/'));
// this content should not be viewed directly
if(!empty($alias_parts) && $alias_parts[0] == $this->config->get('paths.disable_access')) {
throw new NotFoundHttpException();
}
// Target anonymous users and master request (important)
if ($anonymous === TRUE && $event->isMasterRequest()) {
$current_path = \Drupal::service('path.current')->getPath();
$alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$alias_parts = explode('/', trim($alias, '/'));
// This content should not be viewed directly.
if (!empty($alias_parts) && $alias_parts[0] == $this->config->get('paths.disable_access')) {
throw new NotFoundHttpException();
}

// redirect front page url to site root, if it's not ajax request
if(\Drupal::service('path.matcher')->isFrontPage() && $event->getRequest()->getPathInfo() !== '/' && !$event->getRequest()->isXmlHttpRequest()) {
$response = new Response();
$response->headers->set('Location', '/');
$response->setStatusCode(Response::HTTP_PERMANENTLY_REDIRECT);
$event->setResponse($response);
// Redirect front page url to site root, if it's not ajax request.
if (count(\Drupal::service('language_manager')->getLanguages()) <= 1) {
if (\Drupal::service('path.matcher')->isFrontPage() && $event->getRequest()->getPathInfo() !== '/' && !$event->getRequest()->isXmlHttpRequest()) {
$response = new Response();
$response->headers->set('Location', '/');
$response->setStatusCode(Response::HTTP_PERMANENTLY_REDIRECT);
$event->setResponse($response);
}
}
}
}

// Redirect any pages with aliases to the alias
// Redirect any pages with aliases to the alias.
$current_uri = \Drupal::request()->getRequestUri();
$alias_uri = \Drupal::service('path.alias_manager')->getAliasByPath($current_uri);
if ($current_uri !== $alias_uri) {
$response = new Response();
$response->headers->set('Location', $alias_uri);
$response->setStatusCode(Response::HTTP_PERMANENTLY_REDIRECT);
$event->setResponse($response);
$response = new Response();
$response->headers->set('Location', $alias_uri);
$response->setStatusCode(Response::HTTP_PERMANENTLY_REDIRECT);
$event->setResponse($response);
}
}

}
40 changes: 20 additions & 20 deletions src/EventSubscriber/KernelResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ class KernelResponseSubscriber implements EventSubscriberInterface {

private $config;

/*
* get module configuration
/**
* Get module configuration.
*/
public function getConfig($config) {
$this->config = $config->get('starter.settings');
}


/**
* The subscribed events.
*/
Expand All @@ -38,42 +37,42 @@ public static function getSubscribedEvents() {
*/
public function onKernelResponse(FilterResponseEvent $event) {

if($event->getRequest()->attributes->get('_route') == 'system.entity_autocomplete') {
if ($event->getRequest()->attributes->get('_route') == 'system.entity_autocomplete') {

$json_suggested_values = $event->getResponse()->getContent();
$suggested_values = json_decode($json_suggested_values);
$return_values = [];

if($event->getRequest()->attributes->get('target_type') == 'taxonomy_term' && $this->config->get('term_autocomplete_display_vocabulary')) {
if ($event->getRequest()->attributes->get('target_type') == 'taxonomy_term' && $this->config->get('term_autocomplete_display_vocabulary')) {

$vocabularies = entity_load_multiple('taxonomy_vocabulary');

foreach ($suggested_values as $value) {

$value_parts = explode('(', $value->value);
$tid = trim(end($value_parts),' )');
$value_parts = explode('(', $value->value);
$tid = trim(end($value_parts), ' )');

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);

$return_values[] = [
'value' => $value->value,
'label' => $value->label.(!empty($term) ? ' ['.$vocabularies[$term->getVocabularyId()]->label().']' : ''),
];
$return_values[] = [
'value' => $value->value,
'label' => $value->label . (!empty($term) ? ' [' . $vocabularies[$term->getVocabularyId()]->label() . ']' : ''),
];
}
}
elseif($event->getRequest()->attributes->get('target_type') == 'node' && $this->config->get('node_autocomplete_display_bundle')) {
elseif ($event->getRequest()->attributes->get('target_type') == 'node' && $this->config->get('node_autocomplete_display_bundle')) {

foreach ($suggested_values as $value) {

$value_parts = explode('(', $value->value);
$nid = trim(end($value_parts),' )');
$value_parts = explode('(', $value->value);
$nid = trim(end($value_parts), ' )');

$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);

$return_values[] = [
'value' => $value->value,
'label' => $value->label.(!empty($node) ? ' ['.$node->type->entity->label().']' : ''),
];
$return_values[] = [
'value' => $value->value,
'label' => $value->label . (!empty($node) ? ' [' . $node->type->entity->label() . ']' : ''),
];
}
}
else {
Expand All @@ -84,4 +83,5 @@ public function onKernelResponse(FilterResponseEvent $event) {
$event->setResponse($json_response);
}
}

}
42 changes: 23 additions & 19 deletions src/EventSubscriber/RouteSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
<?php

namespace Drupal\starter\EventSubscriber;

use Symfony\Component\Routing\RouteCollection;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteBuildEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
*
*/
class RouteSubscriber implements EventSubscriberInterface {

private $config;

/*
* get module configuration
/**
* Get module configuration.
*/
public function getConfig($config) {
$this->config = $config->get('starter.settings');
}

/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::ALTER] = 'alterRoutes';
return $events;
}

/**
* Alters existing routes.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
*/
* Alters existing routes.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
*/
public function alterRoutes(RouteBuildEvent $event) {

// Fetch the collection which can be altered.
$collection = $event->getRouteCollection();
// The event is fired multiple times so ensure that the user_page route
// is available.
// alter user login url
// alter user login url.
if ($route = $collection->get('user.login')) {
$login = $this->config->get('paths.login');
// make sure path starts with /
if(strpos($login,'/')!==0) {
$login = '/'.$login;
// Make sure path starts with /.
if (strpos($login, '/') !== 0) {
$login = '/' . $login;
}
$route->setPath($login);
}
// alter user logout url
// Alter user logout url.
if ($route = $collection->get('user.logout')) {
$logout = $this->config->get('paths.logout');
// make sure path starts with /
if(strpos($logout,'/')!==0) {
$logout = '/'.$logout;
// Make sure path starts with /.
if (strpos($logout, '/') !== 0) {
$logout = '/' . $logout;
}
$route->setPath($logout);
}
// prevent access to default front page view
// Prevent access to default front page view.
if ($route = $collection->get('view.frontpage.page_1')) {
$route->setRequirement('_access', 'false');
}
}

}
Loading

0 comments on commit 42cb0fd

Please sign in to comment.