diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8662aa9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/core/modules/language/src/Plugin/Block/LanguageBlock.php b/core/modules/language/src/Plugin/Block/LanguageBlock.php index 3995ece..1e7c26b 100644 --- a/core/modules/language/src/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/src/Plugin/Block/LanguageBlock.php @@ -10,6 +10,8 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; use Drupal\Core\Path\PathMatcherInterface; +use Drupal\Core\Form\FormStateInterface; +use Drupal\language\LanguageNegotiatorInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -36,6 +38,12 @@ class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface protected $languageManager; /** + * The language negotiator. + * @var \Drupal\language\LanguageNegotiatorInterface + */ + protected $negotiator; + + /** * The path matcher. * * @var \Drupal\Core\Path\PathMatcherInterface @@ -55,11 +63,14 @@ class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface * The language manager. * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher * The path matcher. + * @param \Drupal\language\LanguageNegotiatorInterface $negotiator + * The language negotiator. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher, LanguageNegotiatorInterface $negotiator) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->languageManager = $language_manager; $this->pathMatcher = $path_matcher; + $this->negotiator = $negotiator; } @@ -72,7 +83,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('language_manager'), - $container->get('path.matcher') + $container->get('path.matcher'), + $container->get('language_negotiator') ); } @@ -118,4 +130,23 @@ public function getCacheMaxAge() { return 0; } + /** + * {@inheritdoc} + */ + public function blockForm($form, FormStateInterface $form_state) { + $form = parent::blockForm($form, $form_state); + $languages = $this->languageManager->getLanguages(); + // Warn the user that if only one language is enabled the block won't appear. + if (count($languages) == 1) { + drupal_set_message($this->t('Only one language is enabled. Therefore the language switcher block will not be shown. To add a new language on the site, go to @language-link.', array('@language-link' => \Drupal::l(t('Language list'), Url::fromRoute('entity.configurable_language.collection')))), 'warning'); + } + // Warn the user that if there are no links to show because language-url + // method detection isn't enabled, the block won't appear. + elseif (!$this->negotiator->isNegotiationMethodEnabled('language-url')) { + drupal_set_message($this->t('No language detection method is enabled that would provide URL based detection. Therefore the language switcher block will not be shown.'), 'warning'); + } + + return $form; + } + } diff --git a/core/modules/language/src/Tests/LanguageSwitchingTest.php b/core/modules/language/src/Tests/LanguageSwitchingTest.php index c9ff8d0..828e943 100644 --- a/core/modules/language/src/Tests/LanguageSwitchingTest.php +++ b/core/modules/language/src/Tests/LanguageSwitchingTest.php @@ -283,6 +283,37 @@ function testLanguageBodyClass() { } + function testLanguageSwitcher() { + // Go to block layout. + $this->drupalGet('admin/structure/block'); + + // Check the link to add the block is there. + $language_switcher = $this->xpath('//*[@id="edit-category-system"]/div/ul/li[@class="language-blocklanguage-interface"]/a'); + $this->assertTrue($language_switcher, 'LanguageĀ  switcher is available.'); + + // Check the link has a tooltip. + $this->assertTrue($language_switcher[0]['title'], 'The tooltip is available.'); + + // Click to add the Language switcher. + $this->clickLink(t('Language switcher')); + + // Post the block form. + $edit = array('region' => 'sidebar_first',); + $this->drupalPostForm(NULL, $edit, t('Save block')); + + // Check the block has not been added. + $block = $this->xpath('//*[@id="blocks"]/tbody/tr[@class="draggable color-warning js-block-placed odd"]'); + $this->assertFalse($block, 'The block is not added.'); + + // Check the message is being rendered. + // TODO: The system can show more than one message, check it in a loop. + $warning_messages = $this->xpath('//div[@class="region region-content"]/div[@class="messages messages--status"]/ul/li'); + foreach($warning_messages as $warning) { + $warning_bool = ($warning->__toString() == 'The block Language Switcher cannot be added.')? true : false; + } + $this->assertTrue($warning_bool, 'Message to warning the user the block cannot be added.'); + } + /** * For authenticated users, the "active" class is set by JavaScript. *