Skip to content

Commit

Permalink
Merge pull request #223 from quantcdn/feature/special-pages
Browse files Browse the repository at this point in the history
Updated special pages logic to avoid errors.
  • Loading branch information
kepol authored Jan 31, 2024
2 parents 9d82f5f + c489e45 commit e508a30
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 39 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
47 changes: 38 additions & 9 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,37 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) {
$language = \Drupal::languageManager()->getLanguage($langcode);
$options['language'] = $language;
}
$defaultLangcode = \Drupal::languageManager()->getDefaultLanguage()->getId();

$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 /.
$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 = "/";

// Handle default language prefix.
if ($langcode == $defaultLangcode) {
// Tack on the prefix if it's set.
$negotiation = \Drupal::config('language.negotiation')->get('url');
$url .= $negotiation['prefixes'][$langcode] ?? '';
if ($url != "/") {
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/", $url, 301), QuantRedirectEvent::UPDATE);
\Drupal::logger('quant_seed')->notice("Adding home page redirect: / => @url", ['@url' => $url]);
}
}
// Handle translated front/home page.
elseif ($prefix = Utility::getPathPrefix($langcode)) {
$url = $prefix;
\Drupal::logger('quant_seed')->notice("Adding translated home page: @url", ['@url' => $url]);
}
}

$response = self::markupFromRoute($url, ['quant-revision' => $rid]);
Expand All @@ -319,16 +337,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 == $defaultLangcode) {
$url = $key;
\Drupal::logger('quant')->notice("Setting status page: @key => @value",
[
'@key' => $key,
'@value' => $value,
]
);
}
}
}

Expand Down
61 changes: 55 additions & 6 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,42 @@ public static function getUrl(string $url = NULL, string $langcode = NULL) : str
$url = '/' . $url;
}

// Handle multilingual paths.
$prefix = self::getPathPrefix($langcode);

// Only add the language prefix if it's not there.
if (!str_starts_with($url, $prefix)) {
$url = $prefix . $url;
}

return $url;
}

/**
* Get path prefix based on site settings.
*
* @param string $langcode
* The language code.
*
* @return string
* The path prefix based on multilingual settings. Defaults to '/'.
*/
public static function getPathPrefix(string $langcode = NULL) : string {

// Always start with a slash.
$prefix = '/';

// Handle multilingual paths.
if (self::usesLanguagePathPrefixes()) {
// Use the current language if none is provided.
if (!$langcode) {
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
}
// @todo Handle when prefix is different than the langcode.
$prefix = '/' . $langcode;

// Only add the language prefix if it's not there.
if (!str_starts_with($url, $prefix)) {
$url = $prefix . $url;
}
}

return $url;
return $prefix;
}

/**
Expand Down Expand Up @@ -117,6 +138,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 e508a30

Please sign in to comment.