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; + } + }