diff --git a/README.md b/README.md index 08bf637..998f649 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Language Suggestion module for Drupal 8 -Drupal 8 module built for multilingual websites with a friendly suggestion box about other versions of the site with browser based language detection. +Drupal 8 module built for multilingual websites with a friendly suggestion +box about other versions of the site with browser based language detection. Language suggestion mapping page: @@ -16,11 +17,21 @@ Suggestion box that appears for visitors ### Use case -Sometimes when implementing multilingual sites there might be a case when rather than redirecting to a version of the site that you assume visitor is from you might want suggest and ask them if they would like to continue to a language that we think he understands. - -This module does exactly that. It shows a little box with some text and a link to take to a sugested version of the site. The module also provides a way to **dismiss** the box which just snoozes it for a configurable time. It can also remember selection and **automatically redirect** to previously selected language. This option can be overriden by a user when manually selecting a language from the language switcher/dropdown. - -Configuration page is located at (`Administration` > `Configuration` > `Regional and language`). Path `/admin/config/regional/language-suggestion` +Sometimes when implementing multilingual sites there might be +a case when rather than redirecting to a version of the site +that you assume visitor is from you might want suggest and ask +them if they would like to continue to a language that we think he understands. + +This module does exactly that. It shows a little box with some +text and a link to take to a sugested version of the site. +The module also provides a way to **dismiss** the box which +just snoozes it for a configurable time. It can also remember +selection and **automatically redirect** to previously selected language. +This option can be overriden by a user when manually selecting +a language from the language switcher/dropdown. + +Configuration page is located at (`Administration` > `Configuration` > +`Regional and language`). Path `/admin/config/regional/language-suggestion` Module developed by [Minnur Yunusov](https://www.minnur.com) at [Chapter Three](https://www.chapterthree.com) diff --git a/assets/language_suggestion.css b/assets/language_suggestion.css index 8776c48..10d26ef 100644 --- a/assets/language_suggestion.css +++ b/assets/language_suggestion.css @@ -10,8 +10,8 @@ border: 1px solid #c3c3c3; padding: 15px; -webkit-box-shadow: 6px 8px 9px -10px black; - -moz-box-shadow: 6px 8px 9px -10px black; - box-shadow: 6px 8px 9px -10px black; + -moz-box-shadow: 6px 8px 9px -10px black; + box-shadow: 6px 8px 9px -10px black; } .ls-wrapper .ls-message, diff --git a/language_suggestion.module b/language_suggestion.module index 3b1b2cb..0a37b54 100644 --- a/language_suggestion.module +++ b/language_suggestion.module @@ -1,5 +1,10 @@ config_factory = $config_factory->get('language_suggestion.settings'); + /** + * Messenger service. + * + * @var Drupal\Core\Logger\LoggerChannelFactory + */ + protected $loggerFactory; + + /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configfactory; + + /** + * {@inheritdoc} + */ + public function __construct(ConfigFactoryInterface $configfactory, EntityTypeManagerInterface $entity, LoggerChannelFactory $logger_factory) { + $this->configfactory = $configfactory->get('language_suggestion.settings'); + $this->fileStorage = $entity->getStorage('file'); + $this->loggerFactory = $logger_factory; } + /** + * {@inheritdoc} + */ public static function create(ContainerInterface $container) { return new static( - $container->get('config.factory') + $container->get('config.factory'), + $container->get('file'), + $container->get('logger.factory') ); } /** * Get HTTP parameter value. */ - public function getHTTPHeaderValue(Request $request) { + public function gethttpheadervalue(Request $request) { $langcode = ''; $data = []; - $parameter = $this->config_factory->get('http_header_parameter'); - if ($this->config_factory->get('language_detection') == 'http_header') { + $parameter = $this->configfactory->get('http_header_parameter'); + if ($this->configfactory->get('language_detection') == 'http_header') { $langcode = $request->headers->get($parameter); } $data['#cache'] = [ @@ -42,7 +75,7 @@ class LanguageSuggestionController extends ControllerBase { 'tags' => ['language_suggestion_http_header'], 'contexts' => [ 'url.query_args', - 'ip' + 'ip', ], ]; $response = new CacheableJsonResponse($langcode); @@ -54,12 +87,11 @@ class LanguageSuggestionController extends ControllerBase { * Get GEOIP country code from database. */ public function getCountryCode(Request $request) { - $langcode = ''; $data = ''; $language_detection = $this->config_factory->get('language_detection'); if ($language_detection == 'geoip_db') { $db_file_id = $this->config_factory->get('geoip_db_file'); - if ($file = File::load($db_file_id)) { + if ($file = $this->fileStorage->load($db_file_id)) { try { $reader = new Reader($file->getFileUri()); $record = $reader->country($request->getClientIp()); @@ -70,17 +102,17 @@ class LanguageSuggestionController extends ControllerBase { } } } - else if ($language_detection == '_server') { + elseif ($language_detection == '_server') { if ($_server_param = $this->config_factory->get('_server_parameter')) { - header('cache-control: no-store', false); - header('Vary: x-geo-country-code', false); + header('cache-control: no-store', FALSE); + header('Vary: x-geo-country-code', FALSE); $data = !empty($_SERVER[$_server_param]) ? $_SERVER[$_server_param] : ''; } } if ($this->config_factory->get('debug')) { - \Drupal::logger('language_suggestion')->notice('Detected country:' . $data); + $this->loggerFactory('language_suggestion')->notice('Detected country:' . $data); } - return new JsonResponse([ 'country' => $data]); + return new JsonResponse(['country' => $data]); } } diff --git a/src/Form/LanguageSuggestionMappingForm.php b/src/Form/LanguageSuggestionMappingForm.php index 7180c69..99895de 100644 --- a/src/Form/LanguageSuggestionMappingForm.php +++ b/src/Form/LanguageSuggestionMappingForm.php @@ -14,7 +14,7 @@ use Drupal\Core\Cache\Cache; * Class Language Suggestion mapping form. */ class LanguageSuggestionMappingForm extends ConfigFormBase { - + /** * The language manager. * @@ -23,11 +23,13 @@ class LanguageSuggestionMappingForm extends ConfigFormBase { protected $languageManager; /** - * Constructs a \Drupal\language_suggestion\Form\LanguageSuggestionMappingForm object. + * The config service. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The factory for configuration objects. - * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager + * + * The language interface. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. */ public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) { diff --git a/src/Form/LanguageSuggestionSettingsForm.php b/src/Form/LanguageSuggestionSettingsForm.php index 09c6e56..a3d4a65 100644 --- a/src/Form/LanguageSuggestionSettingsForm.php +++ b/src/Form/LanguageSuggestionSettingsForm.php @@ -5,7 +5,7 @@ namespace Drupal\language_suggestion\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Cache\Cache; -use Drupal\file\Entity\File; +use Drupal\Core\Entity\EntityTypeManagerInterface; /** * Class Language Suggestion settings form. @@ -19,6 +19,30 @@ class LanguageSuggestionSettingsForm extends ConfigFormBase { return ['language_suggestion.settings']; } + /** + * The storage handler class for files. + * + * @var \Drupal\file\FileStorage + */ + protected $fileStorage; + + /** + * {@inheritdoc} + */ + public function __construct(EntityTypeManagerInterface $entity) { + $this->fileStorage = $entity->getStorage('file'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('file'), + + ); + } + /** * {@inheritdoc} */ @@ -180,7 +204,7 @@ class LanguageSuggestionSettingsForm extends ConfigFormBase { $db_file = $form_state->getValue('geoip_db_file'); if (!empty($db_file[0])) { - $file = File::load($db_file[0]); + $file = $this->fileStorage($db_file[0]); $file->setPermanent(); $file->save(); } diff --git a/src/Form/NegotiationHTTPHeaderForm.php b/src/Form/NegotiationHTTPHeaderForm.php index d3511e6..36589cf 100644 --- a/src/Form/NegotiationHTTPHeaderForm.php +++ b/src/Form/NegotiationHTTPHeaderForm.php @@ -5,7 +5,6 @@ namespace Drupal\language_suggestion\Form; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; use Drupal\language\ConfigurableLanguageManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -70,8 +69,10 @@ class NegotiationHTTPHeaderForm extends ConfigFormBase { $existing_languages[$langcode] = $language->getName(); } - // If we have no languages available, present the list of predefined languages - // only. If we do have already added languages, set up two option groups with + // If we have no languages available, + // present the list of predefined languages + // only. If we do have already added languages, + // set up two option groups with // the list of existing and then predefined languages. if (empty($existing_languages)) { $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured(); diff --git a/tests/src/Functional/LanguageSuggestionTest.php b/tests/src/Functional/LanguageSuggestionTest.php index 91b49e4..e9047c5 100644 --- a/tests/src/Functional/LanguageSuggestionTest.php +++ b/tests/src/Functional/LanguageSuggestionTest.php @@ -28,7 +28,9 @@ final class LanguageSuggestionTest extends BrowserTestBase { * Test callback. */ public function testSetup(): void { - $admin_user = $this->drupalCreateUser(['access administration pages', 'administer site configuration']); + $admin_user = $this->drupalCreateUser(['access administration pages', + 'administer site configuration', + ]); $this->drupalLogin($admin_user); $this->drupalGet(Url::fromRoute('language_suggestion.settings')); $this->assertSession()->pageTextContains('Show language suggestion.');