diff --git a/quant.module b/quant.module index b1c0fec7..0bf3af35 100644 --- a/quant.module +++ b/quant.module @@ -358,19 +358,11 @@ function quant_form_system_site_information_settings_alter(&$form, FormStateInte * Update the special pages when the form is saved. */ function quant_special_pages() { - $system = \Drupal::config('system.site'); - $system_pages = [ - $system->get('page.front'), - $system->get('page.404'), - $system->get('page.403'), - '/', - '/_quant404', - '/_quant403', - ]; - foreach ($system_pages as $route) { + foreach (Utility::getSpecialPages() as $route) { $item = new RouteItem(['route' => $route]); $item->send(); + \Drupal::logger('quant')->notice("Sending route: @route", ['@route' => $route]); } } diff --git a/src/EventSubscriber/CollectionSubscriber.php b/src/EventSubscriber/CollectionSubscriber.php index c597d722..a9710cc5 100644 --- a/src/EventSubscriber/CollectionSubscriber.php +++ b/src/EventSubscriber/CollectionSubscriber.php @@ -8,6 +8,7 @@ use Drupal\quant\Event\CollectRoutesEvent; use Drupal\quant\Event\CollectTaxonomyTermsEvent; use Drupal\quant\Event\QuantCollectionEvents; +use Drupal\quant\Utility; use Drupal\user\Entity\User; use Drupal\views\Views; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -239,21 +240,8 @@ public function collectRoutes(CollectRoutesEvent $event) { // Handle unpublished content based on settings. $disable_drafts = $this->configFactory->get('quant.settings')->get('disable_content_drafts'); - // Collect the site configured routes. - $system = $this->configFactory->get('system.site'); - $system_pages = ['page.front', 'page.404', 'page.403']; - - foreach ($system_pages as $config) { - $system_path = $system->get($config); - if (!empty($system_path)) { - $event->queueItem(['route' => $system_path]); - } - } - // Add special Quant pages. - $quant_pages = ['/', '/_quant404', '/_quant403']; - - foreach ($quant_pages as $page) { + foreach (Utility::getSpecialPages() as $page) { $event->queueItem(['route' => $page]); } diff --git a/src/Seed.php b/src/Seed.php index 92eecc91..63641253 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -286,13 +286,14 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { $url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options)->toString(); - // Special case for home-page, rewrite URL as /. + // If this is the front/home page, rewrite URL as /. + // @todo Handle translated front/home page. $site_config = \Drupal::config('system.site'); $front = $site_config->get('page.front'); if ((strpos($front, '/node/') === 0) && $nid == substr($front, 6)) { if ($entity->isPublished() && $entity->isDefaultRevision()) { - // Trigger redirect event from alias to home. + // Trigger redirect event from alias to /. \Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent($url, "/", 301), QuantRedirectEvent::UPDATE); } $url = "/"; @@ -319,16 +320,27 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { } } - // Special case pages (403/404/Home) - $specialPages = [ - '/' => $site_config->get('page.front'), - '/_quant404' => $site_config->get('page.404'), + // Handle status pages. Must happen after response has been checked. + $statusPages = [ '/_quant403' => $site_config->get('page.403'), + '/_quant404' => $site_config->get('page.404'), ]; - foreach ($specialPages as $k => $v) { - if ((strpos($v, '/node/') === 0) && $entity->get('nid')->value == substr($v, 6)) { - $url = $k; + // If this node is a status page, rewrite URL to use special internal route + // so they show up properly when getting a 403 or 404 status code. + foreach ($statusPages as $key => $value) { + if ((strpos($value, '/node/') === 0) && $entity->get('nid')->value == substr($value, 6)) { + // Only set for the default language. + // @todo Handle translated status pages. + if (empty($langcode) || $langcode == \Drupal::languageManager()->getDefaultLanguage()->getId()) { + $url = $key; + \Drupal::logger('quant')->notice("Setting status page: @key => @value", + [ + '@key' => $key, + '@value' => $value, + ] + ); + } } } diff --git a/src/Utility.php b/src/Utility.php index 20e2efff..3993f8d9 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -117,6 +117,34 @@ public static function inList($item, array $list) { return $found; } + /** + * Get special pages. + * + * @return array + * An array of special pages to process. + */ + public static function getSpecialPages() { + $system = \Drupal::config('system.site'); + $pages = [ + $system->get('page.front'), + $system->get('page.404'), + $system->get('page.403'), + '/', + '/_quant404', + '/_quant403', + ]; + + $validator = \Drupal::service('path.validator'); + foreach ($pages as $index => $page) { + // Remove any pages that don't exist. + if (empty($page) || !$validator->getUrlIfValid($page)) { + unset($pages[$index]); + } + } + + return $pages; + } + /** * Get Quant page info for the given URLs. *