diff --git a/src/Controllers/GoToDefaultLanguage.php b/src/Controllers/GoToDefaultLanguage.php index dc2a73a..5296c47 100644 --- a/src/Controllers/GoToDefaultLanguage.php +++ b/src/Controllers/GoToDefaultLanguage.php @@ -21,4 +21,5 @@ class GoToDefaultLanguage extends ControllerBase { $language = \Drupal::languageManager()->getDefaultLanguage(); return $this->redirect('stringoverrides.translations_form', ['language' => $language->getId()]); } -} \ No newline at end of file + +} diff --git a/src/Form/StringoverridesAdminForm.php b/src/Form/StringoverridesAdminForm.php index 7e335ef..0a22dd1 100644 --- a/src/Form/StringoverridesAdminForm.php +++ b/src/Form/StringoverridesAdminForm.php @@ -20,7 +20,10 @@ class StringoverridesAdminForm extends FormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $language = 'default') { + public function buildForm(array $form, FormStateInterface $form_state, $language = NULL) { + if (!$language) { + $language = \Drupal::languageManager()->getDefaultLanguage(); + } $strings = $this->getCurrentTranslations($language); $form['lang'] = [ @@ -30,7 +33,12 @@ class StringoverridesAdminForm extends FormBase { $form['translations_table'] = [ '#type' => 'table', - '#header' => ['Enabled', 'Original', 'Replacement', 'Context'], + '#header' => [ + $this->t('Enabled'), + $this->t('Original'), + $this->t('Replacement'), + $this->t('Context'), + ], '#title' => $this->t('Translations'), '#attributes' => ['id' => 'stringoverrides-wrapper'], ]; @@ -80,7 +88,7 @@ class StringoverridesAdminForm extends FormBase { * Translations. */ public function getCurrentTranslations($language) { - $config_factory = \Drupal::service('config.factory'); + $config_factory = $this->configFactory(); $words_enabled = $config_factory ->getEditable('stringoverrides.string_override.' . $language) ->get('contexts'); @@ -96,7 +104,7 @@ class StringoverridesAdminForm extends FormBase { $strings = []; foreach ($words as $enabled => $custom_strings) { foreach ($custom_strings as $context) { - foreach ($context['translations'] as $source => $translation) { + foreach ($context['translations'] as $translation) { $strings[] = [ 'enabled' => $enabled, 'context' => $context['context'], @@ -142,27 +150,27 @@ class StringoverridesAdminForm extends FormBase { '#type' => 'checkbox', '#maxlength' => 255, '#default_value' => $string['enabled'], - '#attributes' => array( - 'title' => t('Flag whether this override should be active.'), - ), + '#attributes' => [ + 'title' => $this->t('Flag whether this override should be active.'), + ], ]; $row['source'] = [ '#type' => 'textarea', '#default_value' => $string['source'], '#rows' => 1, - '#attributes' => array( - 'title' => t('The original source text to be replaced.'), - ), + '#attributes' => [ + 'title' => $this->t('The original source text to be replaced.'), + ], ]; $row['translation'] = [ '#type' => 'textarea', '#default_value' => $string['translation'], '#rows' => 1, - '#attributes' => array( - 'title' => t('The text to replace the original source text.'), - ), + '#attributes' => [ + 'title' => $this->t('The text to replace the original source text.'), + ], // Hide the translation when the source is empty. '#states' => [ 'invisible' => [ @@ -176,7 +184,7 @@ class StringoverridesAdminForm extends FormBase { '#maxlength' => 255, '#default_value' => $string['context'], '#attributes' => [ - 'title' => t('Strings sometimes can have context applied to them. Most cases, this is not the case.'), + 'title' => $this->t('Strings sometimes can have context applied to them. Most cases, this is not the case.'), ], '#size' => 5, // Hide the context when the source is empty. @@ -228,11 +236,14 @@ class StringoverridesAdminForm extends FormBase { $words = [TRUE => [], FALSE => []]; // Drupal config key can't have some characters, we need index for each // contexts string and array to keep track . - $config_enabled = \Drupal::service('config.factory')->getEditable('stringoverrides.string_override.' . $language); - $config_disabled = \Drupal::service('config.factory')->getEditable('stringoverrides.string_override.' . $language . '_disabled'); + $config_factory = $this->configFactory(); + $config_enabled = $config_factory + ->getEditable('stringoverrides.string_override.' . $language); + $config_disabled = $config_factory + ->getEditable('stringoverrides.string_override.' . $language . '_disabled'); $form_data = $form_state->getValue('translations_table'); - foreach ($form_data as $i => $string) { + foreach ($form_data as $string) { if (!empty($string['source'])) { $context = $string['context']; list($source, $translation) = str_replace("\r", '', [$string['source'], $string['translation']]); @@ -257,12 +268,12 @@ class StringoverridesAdminForm extends FormBase { case 'edit-submit': $config_disabled->set('contexts', $words[FALSE]); $config_disabled->save(); - drupal_set_message(t('Your changes have been saved.')); + $this->messenger()->addStatus($this->t('Your changes have been saved.')); break; case 'edit-remove': $config_disabled->delete(); - drupal_set_message(t('The disabled strings have been removed.')); + $this->messenger()->addStatus($this->t('The disabled strings have been removed.')); break; } diff --git a/src/Plugin/Derivative/DynamicLocalTasks.php b/src/Plugin/Derivative/DynamicLocalTasks.php index 8c803e6..f21ecba 100644 --- a/src/Plugin/Derivative/DynamicLocalTasks.php +++ b/src/Plugin/Derivative/DynamicLocalTasks.php @@ -20,10 +20,10 @@ class DynamicLocalTasks extends DeriverBase { 'base_route' => 'stringoverrides.translations_form', 'route_name' => 'stringoverrides.translations_form', 'route_parameters' => ['language' => $language_code], - 'weight' => 100, + 'weight' => $language->getWeight(), ] + $base_plugin_definition; } return parent::getDerivativeDefinitions($base_plugin_definition); } -} \ No newline at end of file +} diff --git a/src/StringOverridesTranslation.php b/src/StringOverridesTranslation.php index 9f192c5..5f595b3 100644 --- a/src/StringOverridesTranslation.php +++ b/src/StringOverridesTranslation.php @@ -1,10 +1,5 @@ get($cid)) { + $cache_backend = \Drupal::cache(); + if ($cache = $cache_backend->get($cid)) { return $cache->data; } else { @@ -36,7 +32,7 @@ class StringOverridesTranslation extends StaticTranslation { } } } - \Drupal::cache()->set($cid, $translations); + $cache_backend->set($cid, $translations); return $translations; } } diff --git a/stringoverrides.info.yml b/stringoverrides.info.yml index 3aee6b4..ca1bd79 100644 --- a/stringoverrides.info.yml +++ b/stringoverrides.info.yml @@ -3,3 +3,4 @@ type: module description: 'Provides a quick and easy way of replacing text.' configure: stringoverrides.translations_form.default core: 8.x +core_version_requirement: ^8 || ^9 diff --git a/stringoverrides.links.menu.yml b/stringoverrides.links.menu.yml index 0ffef77..496c2f2 100644 --- a/stringoverrides.links.menu.yml +++ b/stringoverrides.links.menu.yml @@ -3,4 +3,3 @@ stringoverrides.translations_form: route_name: stringoverrides.translations_form.default description: 'Provides a quick and easy way of replacing text.' parent: system.admin_config_regional - diff --git a/stringoverrides.links.task.yml b/stringoverrides.links.task.yml index d795de5..45d2f51 100644 --- a/stringoverrides.links.task.yml +++ b/stringoverrides.links.task.yml @@ -1,3 +1,2 @@ string_override.default: deriver: 'Drupal\stringoverrides\Plugin\Derivative\DynamicLocalTasks' - weight: 100 \ No newline at end of file diff --git a/stringoverrides.permissions.yml b/stringoverrides.permissions.yml index c0b6d46..916e739 100644 --- a/stringoverrides.permissions.yml +++ b/stringoverrides.permissions.yml @@ -1,2 +1,4 @@ administer string overrides: - title: 'Administer string overrides' \ No newline at end of file + title: 'Administer string overrides' + description: 'Allows to override default translatable strings.' + restrict access: true