Commit 50b2be9d authored by gbyte.co's avatar gbyte.co

Issue #2875286 by ziomizar: Exclude translation variants for non-entity pages

parent 45c6e7d0
......@@ -6,6 +6,7 @@ skip_untranslated: false
batch_process_limit: 1500
base_url: ''
custom_links_include_images: false
excluded_languages: []
enabled_entity_types:
- 'node'
- 'taxonomy_term'
......
......@@ -30,6 +30,11 @@ simple_sitemap.settings:
type: sequence
sequence:
type: string
excluded_languages:
label: 'Excluded languages'
type: sequence
sequence:
type: string
simple_sitemap.bundle_settings.*.*:
label: 'Entity bundle settings'
......
......@@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\system\MenuInterface;
use Drupal\language\ConfigurableLanguageInterface;
/**
*Implements hook_help.
......@@ -160,6 +161,24 @@ function simple_sitemap_cron() {
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*
* When a language is removed from the system remove it also from settings.
*/
function simple_sitemap_configurable_language_delete(ConfigurableLanguageInterface $language) {
/**
* @var \Drupal\simple_sitemap\Simplesitemap $generator
*/
$generator = \Drupal::service('simple_sitemap.generator');
$excluded_languages = $generator->getSetting('excluded_languages');
if (isset($excluded_languages[$language->id()])) {
unset($excluded_languages[$language->id()]);
$generator->saveSetting('excluded_languages', $excluded_languages);
}
}
/**
* Implements hook_entity_delete().
*
......
......@@ -193,10 +193,12 @@ class UrlGeneratorBase {
// Including only translated variants of content entity.
if ($entity instanceof ContentEntityBase && $this->batchInfo['skip_untranslated']) {
foreach ($translation_languages as $language) {
$translation = $entity->getTranslation($language->getId());
if ($translation->access('view', $this->anonUser)) {
$url_object->setOption('language', $language);
$alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object->toString());
if (!isset($this->batchInfo['excluded_languages'][$language->getId()]) || $language->isDefault()) {
$translation = $entity->getTranslation($language->getId());
if ($translation->access('view', $this->anonUser)) {
$url_object->setOption('language', $language);
$alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object->toString());
}
}
}
}
......@@ -204,8 +206,10 @@ class UrlGeneratorBase {
// Not a content entity or including all untranslated variants.
elseif ($url_object->access($this->anonUser)) {
foreach ($translation_languages as $language) {
$url_object->setOption('language', $language);
$alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object->toString());
if (!isset($this->batchInfo['excluded_languages'][$language->getId()]) || $language->isDefault()) {
$url_object->setOption('language', $language);
$alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object->toString());
}
}
}
}
......
......@@ -7,6 +7,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\Core\Path\PathValidator;
use Drupal\Core\Language\LanguageManagerInterface;
/**
* Class SimplesitemapFormBase
......@@ -34,23 +35,31 @@ abstract class SimplesitemapFormBase extends ConfigFormBase {
*/
protected $pathValidator;
/**
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* SimplesitemapFormBase constructor.
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\simple_sitemap\Form\FormHelper $form_helper
* @param \Drupal\simple_sitemap\EntityHelper $entity_helper
* @param \Drupal\Core\Path\PathValidator $path_validator
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
*/
public function __construct(
Simplesitemap $generator,
FormHelper $form_helper,
EntityHelper $entity_helper,
PathValidator $path_validator
PathValidator $path_validator,
LanguageManagerInterface $language_manager
) {
$this->generator = $generator;
$this->formHelper = $form_helper;
$this->entityHelper = $entity_helper;
$this->pathValidator = $path_validator;
$this->languageManager = $language_manager;
}
/**
......@@ -61,7 +70,8 @@ abstract class SimplesitemapFormBase extends ConfigFormBase {
$container->get('simple_sitemap.generator'),
$container->get('simple_sitemap.form_helper'),
$container->get('simple_sitemap.entity_helper'),
$container->get('path.validator')
$container->get('path.validator'),
$container->get('language_manager')
);
}
......
......@@ -11,19 +11,6 @@ use Drupal\Component\Utility\UrlHelper;
*/
class SimplesitemapSettingsForm extends SimplesitemapFormBase {
/**
* @var array
*/
protected $formSettings = [
'max_links',
'cron_generate',
'cron_generate_interval',
'remove_duplicates',
'skip_untranslated',
'batch_process_limit',
'base_url',
];
/**
* {@inheritdoc}
*/
......@@ -41,7 +28,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
$form['simple_sitemap_settings']['regenerate'] = [
'#type' => 'fieldset',
'#title' => $this->t('Regenerate sitemap'),
'#markup' => '<p>' . $this->t('This will regenerate the XML sitemap for all languages.') . '</p>',
'#markup' => '<p>' . $this->t('This will regenerate the XML sitemap immediately.') . '</p>',
];
$form['simple_sitemap_settings']['regenerate']['regenerate_submit'] = [
......@@ -111,13 +98,6 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
];
$form['simple_sitemap_settings']['advanced']['skip_untranslated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip non-existent translations'),
'#description' => $this->t('If unchecked, entity links are generated for every language installed on the site, regardless whether the entity has been translated to a language or not.<br/>If checked, entity links are generated exclusively for languages the entity has been translated to. This setting has no effect on non-entity paths like homepage.'),
'#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
];
$form['simple_sitemap_settings']['advanced']['max_links'] = [
'#type' => 'number',
'#title' => $this->t('Maximum links in a sitemap'),
......@@ -135,6 +115,34 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#required' => TRUE,
];
$form['simple_sitemap_settings']['advanced']['languages'] = [
'#type' => 'details',
'#title' => $this->t('Language settings'),
'#open' => FALSE,
];
$language_options = [];
foreach ($this->languageManager->getLanguages() as $language) {
if (!$language->isDefault()) {
$language_options[$language->getId()] = $language->getName();
}
}
$form['simple_sitemap_settings']['advanced']['languages']['skip_untranslated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip non-existent translations'),
'#description' => $this->t('If checked, entity links are generated exclusively for languages the entity has been translated to as long as the language is not excluded below.<br/>Otherwise entity links are generated for every language installed on the site apart from languages excluded below.<br/>This setting has no effect on non-entity paths like homepage.'),
'#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
];
$form['simple_sitemap_settings']['advanced']['languages']['excluded_languages'] = [
'#title' => $this->t('Exclude languages'),
'#type' => 'checkboxes',
'#options' => $language_options,
'#description' => $this->t('There will be no links generated for languages checked here.'),
'#default_value' => $this->generator->getSetting('excluded_languages', []),
];
$this->formHelper->displayRegenerateNow($form['simple_sitemap_settings']);
return parent::buildForm($form, $form_state);
......@@ -155,9 +163,17 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($this->formSettings as $setting_name) {
foreach (['max_links',
'cron_generate',
'cron_generate_interval',
'remove_duplicates',
'skip_untranslated',
'batch_process_limit',
'base_url',] as $setting_name) {
$this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
}
$this->generator->saveSetting('excluded_languages', array_filter($form_state->getValue('excluded_languages')));
parent::submitForm($form, $form_state);
// Regenerate sitemaps according to user setting.
......
......@@ -152,6 +152,7 @@ class SitemapGenerator {
'remove_duplicates' => $this->generator->getSetting('remove_duplicates', TRUE),
'entity_types' => $this->generator->getBundleSettings(),
'base_url' => $this->generator->getSetting('base_url', ''),
'excluded_languages' => $this->generator->getSetting('excluded_languages', []),
]);
// Add custom link generating operation.
......
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