Skip to content

Commit

Permalink
Updated special pages logic to avoid errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jan 30, 2024
1 parent 2a3be47 commit 5f24210
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
12 changes: 2 additions & 10 deletions quant.module
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
16 changes: 2 additions & 14 deletions src/EventSubscriber/CollectionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}

Expand Down
30 changes: 21 additions & 9 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/";
Expand All @@ -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,
]
);
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 5f24210

Please sign in to comment.