In issue #3107818 the isMultilingualSitemap() method was added to SitemapGeneratorBase.php with a strict requirement of LanguageNegotiationUrl::METHOD_ID being enabled. See: https://git.drupalcode.org/project/simple_sitemap/-/commit/75017cd102811...

We use an alternate method provided by another contributed module, but it is a child class of "LanguageNegotiationUrl." The result is all alternate URLs are removed before the final sitemap is generated.

Proposed solution is to use the language negotiation service to load the primary negotiation method for the language type 'content' (LanguageInterface::TYPE_CONTENT) and then see if that is an instance of the "LanguageNegotiationUrl" class. Patch to follow shortly.

This documentation was very helpful in developing the approach: https://api.drupal.org/api/drupal/core%21modules%21language%21src%21Lang...

I think it's worth calling out here that there may be other negotiation methods that could use the URL without inheriting from the LanguageNegotiationUrl class that this solution would miss. An alternative would be to simply provide a configuration value that would allow a user to simply select whether or not they want a multilingual sitemap, which could be more inclusive.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ben.hamelin created an issue. See original summary.

ben.hamelin’s picture

Status: Active » Needs review
FileSize
1.31 KB

Status: Needs review » Needs work

The last submitted patch, 2: 3154570-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ben.hamelin’s picture

FileSize
2.09 KB

Add an anonymous user login to SimplesitemapTestBase.php setup() method so there is a valid user available when the language negotiation service calls are made. Should resolve previous failures but also want to be sure this approach is ok for the existing testing suite.

ben.hamelin’s picture

ben.hamelin’s picture

Status: Needs work » Needs review

I tried some approaches to resolve the failing tests but not having luck. Would love some feedback on this and some guidance on resolving these test issues. I thought adding the drupalLogin() calls would work but I am missing something.

gbyte’s picture

Category: Bug report » Task
Status: Needs review » Needs work

This does not work for me as I have "Customize Content language detection to differ from Interface text language detection settings" unchecked under admin/config/regional/language/detection.

I don't really grasp what LanguageInterface::TYPE_URL does. If it does what I think it does, this should be a working solution:

  public static function isMultilingualSitemap() {

    /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
    $language_negotiator = \Drupal::service('language_negotiator');

    $url_negotiation_method_enabled = FALSE;
    foreach ($language_negotiator->getNegotiationMethods(LanguageInterface::TYPE_URL) as $method) {
      if ($language_negotiator->isNegotiationMethodEnabled($method['id'])) {
        $url_negotiation_method_enabled = TRUE;
        break;
      }
    }

    $has_multiple_indexable_languages = count(
        array_diff_key(\Drupal::languageManager()->getLanguages(),
          \Drupal::service('simple_sitemap.generator')->getSetting('excluded_languages', []))
      ) > 1;

    return $url_negotiation_method_enabled && $has_multiple_indexable_languages;
  }

Otherwise this might be necessary:

  public static function isMultilingualSitemap() {

    /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
    $language_negotiator = \Drupal::service('language_negotiator');

    $url_negotiation_method_enabled = FALSE;
    foreach ([LanguageInterface::TYPE_CONTENT, LanguageInterface::TYPE_INTERFACE] as $type) {
      foreach ($language_negotiator->getNegotiationMethods($type) as $method) {
        if ($language_negotiator->getNegotiationMethodInstance($method['id']) instanceof LanguageNegotiationUrl
          && $language_negotiator->isNegotiationMethodEnabled($method['id'])) {
          $url_negotiation_method_enabled = TRUE;
          break 2;
        }
      }
    }

    $has_multiple_indexable_languages = count(
        array_diff_key(\Drupal::languageManager()->getLanguages(),
          \Drupal::service('simple_sitemap.generator')->getSetting('excluded_languages', []))
      ) > 1;

    return $url_negotiation_method_enabled && $has_multiple_indexable_languages;
  }

Both are working for me. Sorry for not creating patches, but I am quite busy right now. Could you please review/test these two approaches and provide feedback?

gbyte’s picture

Any progress on this?

ben.hamelin’s picture

Sorry, taking a while to get back to this. Hoping to have a test patch ready later this week. Thank you!

abhisekmazumdar’s picture

Priority: Normal » Major
FileSize
313.15 KB

Marking this as major. One of the live site we are working on doesn't has URL detection enabled. So its not creating for the links with the lang-code.

gbyte’s picture

@abhisekmazumdar Please correct me if I am wrong, but a multilingual sitemap is only practical/functional if it can take you to a specific translation/language. If URL detection is fully disabled, the links in the sitemap will be same for each language. What is the point of having repeating links for each language?

gbyte’s picture

Priority: Major » Normal
ben.hamelin’s picture

@gbyte - Here is a patch based on your comment from #7. Tried this locally, it worked fine for me.

gbyte’s picture

Status: Needs work » Needs review
Issue tags: +3

  • gbyte committed fd53dbd on 8.x-3.x
    Issue #3154570 by ben.hamelin, gbyte: Multilingual sites using alternate...
gbyte’s picture

Assigned: ben.hamelin » Unassigned
Category: Task » Feature request
Status: Needs review » Fixed
Issue tags: -3

Thanks for testing.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

PieterDC’s picture

As far as I understand, this issue hasn't been solved because it still requires the default URL language detection to be enabled.
I don't consider reopening this issue, because I consider #3228584: Multilingual sitemap test fails for some language negotiation plugins to be a follow-up issue.