Commit 4136ffa7 authored by gbyte.co's avatar gbyte.co

Issue #2781627 by Ryan Weal, hitfactory: Not possible to override base_url in production

parent 69566b6b
......@@ -7,3 +7,4 @@ enabled_entity_types:
- 'node'
- 'taxonomy_term'
- 'menu_link_content'
base_url: ''
......@@ -21,6 +21,9 @@ simple_sitemap.settings:
type: sequence
sequence:
type: string
base_url:
label: 'Base URL'
type: string
simple_sitemap.bundle_settings.*.*:
label: 'Entity bundle settings'
......
......@@ -279,7 +279,7 @@ class BatchUrlGenerator {
if (!is_null($entity) && isset($translation_languages['und'])) {
if ($url_object->access($this->anonUser)) {
$url_object->setOption('language', $this->languages[$this->defaultLanguageId]);
$alternate_urls[$this->defaultLanguageId] = $url_object->toString();
$alternate_urls[$this->defaultLanguageId] = $this->replaceBaseUrlWithCustom($url_object->toString());
}
}
else {
......@@ -289,7 +289,7 @@ class BatchUrlGenerator {
$translation = $entity->getTranslation($language->getId());
if ($translation->access('view', $this->anonUser)) {
$url_object->setOption('language', $language);
$alternate_urls[$language->getId()] = $url_object->toString();
$alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object->toString());
}
}
}
......@@ -298,7 +298,7 @@ class BatchUrlGenerator {
elseif ($url_object->access($this->anonUser)) {
foreach ($translation_languages as $language) {
$url_object->setOption('language', $language);
$alternate_urls[$language->getId()] = $url_object->toString();
$alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object->toString());
}
}
}
......@@ -393,6 +393,12 @@ class BatchUrlGenerator {
: NULL;
}
private function replaceBaseUrlWithCustom($url) {
return !empty($this->batchInfo['base_url'])
? str_replace($GLOBALS['base_url'], $this->batchInfo['base_url'], $url)
: $url;
}
/**
* Callback function called by the batch API when all operations are finished.
*
......@@ -407,6 +413,7 @@ class BatchUrlGenerator {
Cache::invalidateTags(['simple_sitemap']);
$this->logger->m(self::REGENERATION_FINISHED_MESSAGE,
['@url' => $GLOBALS['base_url'] . '/sitemap.xml'])
// ['@url' => $this->sitemapGenerator->getCustomBaseUrl() . '/sitemap.xml']) //todo: Use actual base URL for message.
->display('status')
->log('info');
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
/**
* Class SimplesitemapSettingsForm.
......@@ -17,6 +18,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'remove_duplicates',
'skip_untranslated',
'batch_process_limit',
'base_url',
];
/**
......@@ -65,6 +67,14 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#open' => TRUE,
];
$form['simple_sitemap_settings']['advanced']['base_url'] = [
'#type' => 'textfield',
'#title' => $this->t('Default base URL'),
'#default_value' => $this->generator->getSetting('base_url', ''),
'#size' => 30,
'#description' => $this->t('On some hosting providers it is impossible to pass parameters to cron to tell Drupal which URL to bootstrap with. In this case the base URL of sitemap links can be set here.'),
];
$form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
'#type' => 'checkbox',
'#title' => $this->t('Exclude duplicate links'),
......@@ -104,7 +114,13 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {}
public function validateForm(array &$form, FormStateInterface $form_state) {
$base_url = $form_state->getValue('base_url');
$form_state->setValue('base_url', rtrim($base_url, '/'));
if ($base_url != '' && !UrlHelper::isValid($base_url, TRUE)) {
$form_state->setErrorByName('base_url', t('The base URL is invalid.'));
}
}
/**
* {@inheritdoc}
......
......@@ -60,7 +60,8 @@ class SitemapGenerator {
}
/**
*
* @param $from
* @return $this
*/
public function setGenerateFrom($from) {
$this->generateFrom = $from;
......@@ -79,6 +80,7 @@ class SitemapGenerator {
'skip_untranslated' => $this->generator->getSetting('skip_untranslated', FALSE),
'remove_duplicates' => $this->generator->getSetting('remove_duplicates', TRUE),
'entity_types' => $this->generator->getBundleSettings(),
'base_url' => $this->generator->getSetting('base_url', ''),
]);
// Add custom link generating operation.
$this->batch->addOperation('generateCustomUrls', $this->getCustomUrlsData());
......@@ -179,8 +181,7 @@ class SitemapGenerator {
foreach ($chunks as $chunk_id => $chunk_data) {
$writer->startElement('sitemap');
$writer->writeElement('loc', $GLOBALS['base_url'] . '/sitemaps/'
. $chunk_id . '/' . 'sitemap.xml');
$writer->writeElement('loc', $this->getCustomBaseUrl() . '/sitemaps/' . $chunk_id . '/' . 'sitemap.xml');
$writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
$writer->endElement();
}
......@@ -189,6 +190,11 @@ class SitemapGenerator {
return $writer->outputMemory();
}
public function getCustomBaseUrl() {
$customBaseUrl = $this->generator->getSetting('base_url', '');
return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
}
/**
* Generates and returns a sitemap chunk.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment