From 7fff2643622833e72ed986597e88c030282b09fe Mon Sep 17 00:00:00 2001 From: nod_ <nod_@598310.no-reply.drupal.org> Date: Fri, 31 Jan 2025 10:30:52 +0100 Subject: [PATCH] Issue #3497409 by quietone, smustgrave: Fix DrupalPractice.Objects.GlobalFunction in core/lib --- .../Drupal/Core/Command/InstallCommand.php | 7 ++-- .../Config/Entity/ConfigEntityListBuilder.php | 4 +-- .../Drupal/Core/Database/Install/Tasks.php | 35 ++++++++++--------- core/lib/Drupal/Core/Field/WidgetBase.php | 2 +- .../Drupal/Core/FileTransfer/FileTransfer.php | 18 ++++++---- core/lib/Drupal/Core/Recipe/RecipeCommand.php | 14 ++++---- .../Core/StreamWrapper/PrivateStream.php | 7 ++-- .../Core/StreamWrapper/PublicStream.php | 7 ++-- .../Core/StreamWrapper/TemporaryStream.php | 7 ++-- core/lib/Drupal/Core/Updater/Updater.php | 7 ++-- core/phpcs.xml.dist | 1 + 11 files changed, 67 insertions(+), 42 deletions(-) diff --git a/core/lib/Drupal/Core/Command/InstallCommand.php b/core/lib/Drupal/Core/Command/InstallCommand.php index 57d8916b7f8a..dab596423447 100644 --- a/core/lib/Drupal/Core/Command/InstallCommand.php +++ b/core/lib/Drupal/Core/Command/InstallCommand.php @@ -9,6 +9,7 @@ use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\Extension\InfoParserDynamic; use Drupal\Core\Site\Settings; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -24,6 +25,8 @@ */ class InstallCommand extends Command { + use StringTranslationTrait; + /** * The class loader. * @@ -233,7 +236,7 @@ protected function install($class_loader, SymfonyStyle $io, $profile, $langcode, $started = TRUE; // We've already done 1. $progress_bar->setFormat("%current%/%max% [%bar%]\n%message%\n"); - $progress_bar->setMessage(t('Installing @drupal', ['@drupal' => drupal_install_profile_distribution_name()])); + $progress_bar->setMessage($this->t('Installing @drupal', ['@drupal' => drupal_install_profile_distribution_name()])); $tasks = install_tasks($install_state); $progress_bar->start(count($tasks) + 1); } @@ -244,7 +247,7 @@ protected function install($class_loader, SymfonyStyle $io, $profile, $langcode, } $progress_bar->advance(); }); - $success_message = t('Congratulations, you installed @drupal!', [ + $success_message = $this->t('Congratulations, you installed @drupal!', [ '@drupal' => drupal_install_profile_distribution_name(), '@name' => 'admin', '@pass' => $password, diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityListBuilder.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityListBuilder.php index 33c98a2857c8..1c56bc080028 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityListBuilder.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityListBuilder.php @@ -42,14 +42,14 @@ public function getDefaultOperations(EntityInterface $entity) { if ($this->entityType->hasKey('status')) { if (!$entity->status() && $entity->hasLinkTemplate('enable')) { $operations['enable'] = [ - 'title' => t('Enable'), + 'title' => $this->t('Enable'), 'weight' => -10, 'url' => $this->ensureDestination($entity->toUrl('enable')), ]; } elseif ($entity->hasLinkTemplate('disable')) { $operations['disable'] = [ - 'title' => t('Disable'), + 'title' => $this->t('Disable'), 'weight' => 40, 'url' => $this->ensureDestination($entity->toUrl('disable')), ]; diff --git a/core/lib/Drupal/Core/Database/Install/Tasks.php b/core/lib/Drupal/Core/Database/Install/Tasks.php index 924ab8d058f2..ce74667f9ebb 100644 --- a/core/lib/Drupal/Core/Database/Install/Tasks.php +++ b/core/lib/Drupal/Core/Database/Install/Tasks.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Database\Install; use Drupal\Core\Database\Database; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslatableMarkup; /** @@ -14,6 +15,8 @@ */ abstract class Tasks { + use StringTranslationTrait; + /** * The name of the PDO driver this database type requires. * @@ -159,7 +162,7 @@ public function runTasks() { } } else { - $this->fail(t("Failed to run all tasks against the database server. The task %task wasn't found.", ['%task' => $task['function']])); + $this->fail($this->t("Failed to run all tasks against the database server. The task %task wasn't found.", ['%task' => $task['function']])); } } } @@ -194,7 +197,7 @@ protected function connect() { $this->pass('Drupal can CONNECT to the database ok.'); } catch (\Exception $e) { - $this->fail(t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname and port number?</li></ul>', ['%error' => $e->getMessage()])); + $this->fail($this->t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname and port number?</li></ul>', ['%error' => $e->getMessage()])); return FALSE; } return TRUE; @@ -207,11 +210,11 @@ protected function runTestQuery($query, $pass, $fail, $fatal = FALSE) { try { Database::getConnection()->query($query); // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString - $this->pass(t($pass)); + $this->pass($this->t($pass)); } catch (\Exception $e) { // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString - $this->fail(t($fail, ['%query' => $query, '%error' => $e->getMessage(), '%name' => $this->name()])); + $this->fail($this->t($fail, ['%query' => $query, '%error' => $e->getMessage(), '%name' => $this->name()])); return !$fatal; } } @@ -237,7 +240,7 @@ protected function checkEngineVersion() { // them or not. // @see https://www.php.net/manual/en/function.version-compare.php if ($this->minimumVersion() && version_compare(Database::getConnection()->version(), $this->minimumVersion() . '-AnyName', '<')) { - $this->fail(t("The database server version %version is less than the minimum required version %minimum_version.", ['%version' => Database::getConnection()->version(), '%minimum_version' => $this->minimumVersion()])); + $this->fail($this->t("The database server version %version is less than the minimum required version %minimum_version.", ['%version' => Database::getConnection()->version(), '%minimum_version' => $this->minimumVersion()])); } } @@ -260,7 +263,7 @@ public function getFormOptions(array $database) { $form['database'] = [ '#type' => 'textfield', - '#title' => t('Database name'), + '#title' => $this->t('Database name'), '#default_value' => empty($database['database']) ? '' : $database['database'], '#size' => 45, '#required' => TRUE, @@ -273,7 +276,7 @@ public function getFormOptions(array $database) { $form['username'] = [ '#type' => 'textfield', - '#title' => t('Database username'), + '#title' => $this->t('Database username'), '#default_value' => empty($database['username']) ? '' : $database['username'], '#size' => 45, '#required' => TRUE, @@ -286,7 +289,7 @@ public function getFormOptions(array $database) { $form['password'] = [ '#type' => 'password', - '#title' => t('Database password'), + '#title' => $this->t('Database password'), '#default_value' => empty($database['password']) ? '' : $database['password'], '#required' => FALSE, '#size' => 45, @@ -294,7 +297,7 @@ public function getFormOptions(array $database) { $form['advanced_options'] = [ '#type' => 'details', - '#title' => t('Advanced options'), + '#title' => $this->t('Advanced options'), '#weight' => 10, ]; @@ -303,16 +306,16 @@ public function getFormOptions(array $database) { $db_prefix = ($profile == 'standard') ? 'drupal_' : $profile . '_'; $form['advanced_options']['prefix'] = [ '#type' => 'textfield', - '#title' => t('Table name prefix'), + '#title' => $this->t('Table name prefix'), '#default_value' => empty($database['prefix']) ? '' : $database['prefix'], '#size' => 45, - '#description' => t('If more than one application will be sharing this database, a unique table name prefix – such as %prefix – will prevent collisions.', ['%prefix' => $db_prefix]), + '#description' => $this->t('If more than one application will be sharing this database, a unique table name prefix – such as %prefix – will prevent collisions.', ['%prefix' => $db_prefix]), '#weight' => 10, ]; $form['advanced_options']['host'] = [ '#type' => 'textfield', - '#title' => t('Host'), + '#title' => $this->t('Host'), '#default_value' => empty($database['host']) ? 'localhost' : $database['host'], '#size' => 45, // Host names can be 255 characters long. @@ -322,7 +325,7 @@ public function getFormOptions(array $database) { $form['advanced_options']['port'] = [ '#type' => 'number', - '#title' => t('Port number'), + '#title' => $this->t('Port number'), '#default_value' => empty($database['port']) ? '' : $database['port'], '#min' => 0, '#max' => 65535, @@ -348,7 +351,7 @@ public function validateDatabaseSettings(array $database) { // Verify the table prefix. if (!empty($database['prefix']) && is_string($database['prefix']) && !preg_match('/^[A-Za-z0-9_.]+$/', $database['prefix'])) { - $errors[$database['driver'] . '][prefix'] = t('The database table prefix you have entered, %prefix, is invalid. The table prefix can only contain alphanumeric characters, periods, or underscores.', ['%prefix' => $database['prefix']]); + $errors[$database['driver'] . '][prefix'] = $this->t('The database table prefix you have entered, %prefix, is invalid. The table prefix can only contain alphanumeric characters, periods, or underscores.', ['%prefix' => $database['prefix']]); } return $errors; @@ -400,10 +403,10 @@ protected function getConnection() { */ protected function checkJsonSupport() { if ($this->getConnection()->hasJson()) { - $this->pass(t('Database connection supports the JSON type.')); + $this->pass($this->t('Database connection supports the JSON type.')); } else { - $this->fail(t('<a href="https://www.drupal.org/docs/system-requirements">Database connection does not support JSON.</a>')); + $this->fail($this->t('<a href="https://www.drupal.org/docs/system-requirements">Database connection does not support JSON.</a>')); } } diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index 3b2ff90fb960..c48eb0f83914 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -281,7 +281,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f $elements['add_more'] = [ '#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_add_more', - '#value' => t('Add another item'), + '#value' => $this->t('Add another item'), '#attributes' => ['class' => ['field-add-more-submit']], '#limit_validation_errors' => [], '#submit' => [[static::class, 'addMoreSubmit']], diff --git a/core/lib/Drupal/Core/FileTransfer/FileTransfer.php b/core/lib/Drupal/Core/FileTransfer/FileTransfer.php index ac8e8d87ff0e..0a9ee284c472 100644 --- a/core/lib/Drupal/Core/FileTransfer/FileTransfer.php +++ b/core/lib/Drupal/Core/FileTransfer/FileTransfer.php @@ -2,6 +2,8 @@ namespace Drupal\Core\FileTransfer; +use Drupal\Core\StringTranslation\StringTranslationTrait; + /** * Defines the base FileTransfer class. * @@ -21,6 +23,8 @@ #[\AllowDynamicProperties] abstract class FileTransfer { + use StringTranslationTrait; + /** * The username for this file transfer. * @@ -446,26 +450,26 @@ public function setChroot() { public function getSettingsForm() { $form['username'] = [ '#type' => 'textfield', - '#title' => t('Username'), + '#title' => $this->t('Username'), ]; $form['password'] = [ '#type' => 'password', - '#title' => t('Password'), - '#description' => t('Your password is not saved in the database and is only used to establish a connection.'), + '#title' => $this->t('Password'), + '#description' => $this->t('Your password is not saved in the database and is only used to establish a connection.'), ]; $form['advanced'] = [ '#type' => 'details', - '#title' => t('Advanced settings'), + '#title' => $this->t('Advanced settings'), ]; $form['advanced']['hostname'] = [ '#type' => 'textfield', - '#title' => t('Host'), + '#title' => $this->t('Host'), '#default_value' => 'localhost', - '#description' => t('The connection will be created between your web server and the machine hosting the web server files. In the vast majority of cases, this will be the same machine, and "localhost" is correct.'), + '#description' => $this->t('The connection will be created between your web server and the machine hosting the web server files. In the vast majority of cases, this will be the same machine, and "localhost" is correct.'), ]; $form['advanced']['port'] = [ '#type' => 'textfield', - '#title' => t('Port'), + '#title' => $this->t('Port'), '#default_value' => NULL, ]; return $form; diff --git a/core/lib/Drupal/Core/Recipe/RecipeCommand.php b/core/lib/Drupal/Core/Recipe/RecipeCommand.php index 860fc15c238e..be186c484015 100644 --- a/core/lib/Drupal/Core/Recipe/RecipeCommand.php +++ b/core/lib/Drupal/Core/Recipe/RecipeCommand.php @@ -10,6 +10,7 @@ use Drupal\Core\Config\ConfigImporter; use Drupal\Core\Config\ConfigImporterException; use Drupal\Core\Config\StorageComparer; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Psr\Log\LoggerAwareInterface; use Psr\Log\LogLevel; use Symfony\Component\Console\Command\Command; @@ -28,6 +29,7 @@ final class RecipeCommand extends Command { use BootableCommandTrait; + use StringTranslationTrait; /** * Constructs a new RecipeCommand command. @@ -88,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $steps = RecipeRunner::toBatchOperations($recipe); $progress_bar = $io->createProgressBar(); $progress_bar->setFormat("%current%/%max% [%bar%]\n%message%\n"); - $progress_bar->setMessage($this->toPlainString(t('Applying recipe'))); + $progress_bar->setMessage($this->toPlainString($this->t('Applying recipe'))); $progress_bar->start(count($steps)); /** @var array{message?: \Stringable|string, results: array{module?: string[], theme?: string[], content?: string[], recipe?: string[]}} $context */ @@ -103,27 +105,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if ($io->isVerbose()) { if (!empty($context['results']['module'])) { - $io->section($this->toPlainString(t('Modules installed'))); + $io->section($this->toPlainString($this->t('Modules installed'))); $modules = array_map(fn ($module) => \Drupal::service('extension.list.module')->getName($module), $context['results']['module']); sort($modules, SORT_NATURAL); $io->listing($modules); } if (!empty($context['results']['theme'])) { - $io->section($this->toPlainString(t('Themes installed'))); + $io->section($this->toPlainString($this->t('Themes installed'))); $themes = array_map(fn ($theme) => \Drupal::service('extension.list.theme')->getName($theme), $context['results']['theme']); sort($themes, SORT_NATURAL); $io->listing($themes); } if (!empty($context['results']['content'])) { - $io->section($this->toPlainString(t('Content created for recipes'))); + $io->section($this->toPlainString($this->t('Content created for recipes'))); $io->listing($context['results']['content']); } if (!empty($context['results']['recipe'])) { - $io->section($this->toPlainString(t('Recipes applied'))); + $io->section($this->toPlainString($this->t('Recipes applied'))); $io->listing($context['results']['recipe']); } } - $io->success($this->toPlainString(t('%recipe applied successfully', ['%recipe' => $recipe->name]))); + $io->success($this->toPlainString($this->t('%recipe applied successfully', ['%recipe' => $recipe->name]))); return 0; } catch (\Throwable $e) { diff --git a/core/lib/Drupal/Core/StreamWrapper/PrivateStream.php b/core/lib/Drupal/Core/StreamWrapper/PrivateStream.php index 9cf7bc1b2df5..a6fd8bee4f41 100644 --- a/core/lib/Drupal/Core/StreamWrapper/PrivateStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/PrivateStream.php @@ -3,6 +3,7 @@ namespace Drupal\Core\StreamWrapper; use Drupal\Core\Site\Settings; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; /** @@ -13,6 +14,8 @@ */ class PrivateStream extends LocalStream { + use StringTranslationTrait; + /** * {@inheritdoc} */ @@ -24,14 +27,14 @@ public static function getType() { * {@inheritdoc} */ public function getName() { - return t('Private files'); + return $this->t('Private files'); } /** * {@inheritdoc} */ public function getDescription() { - return t('Private local files served by Drupal.'); + return $this->t('Private local files served by Drupal.'); } /** diff --git a/core/lib/Drupal/Core/StreamWrapper/PublicStream.php b/core/lib/Drupal/Core/StreamWrapper/PublicStream.php index f0ed4d5fd9d3..d1fe3fca1dc7 100644 --- a/core/lib/Drupal/Core/StreamWrapper/PublicStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/PublicStream.php @@ -5,6 +5,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\DrupalKernel; use Drupal\Core\Site\Settings; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\HttpFoundation\Request; /** @@ -15,6 +16,8 @@ */ class PublicStream extends LocalStream { + use StringTranslationTrait; + /** * {@inheritdoc} */ @@ -26,14 +29,14 @@ public static function getType() { * {@inheritdoc} */ public function getName() { - return t('Public files'); + return $this->t('Public files'); } /** * {@inheritdoc} */ public function getDescription() { - return t('Public local files served by the webserver.'); + return $this->t('Public local files served by the webserver.'); } /** diff --git a/core/lib/Drupal/Core/StreamWrapper/TemporaryStream.php b/core/lib/Drupal/Core/StreamWrapper/TemporaryStream.php index bce46e03c585..0bbf52bdf68c 100644 --- a/core/lib/Drupal/Core/StreamWrapper/TemporaryStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/TemporaryStream.php @@ -2,6 +2,7 @@ namespace Drupal\Core\StreamWrapper; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; /** @@ -12,6 +13,8 @@ */ class TemporaryStream extends LocalStream { + use StringTranslationTrait; + /** * {@inheritdoc} */ @@ -23,14 +26,14 @@ public static function getType() { * {@inheritdoc} */ public function getName() { - return t('Temporary files'); + return $this->t('Temporary files'); } /** * {@inheritdoc} */ public function getDescription() { - return t('Temporary local files for upload and previews.'); + return $this->t('Temporary local files for upload and previews.'); } /** diff --git a/core/lib/Drupal/Core/Updater/Updater.php b/core/lib/Drupal/Core/Updater/Updater.php index d802ef5a0bbb..aaacf151dcfa 100644 --- a/core/lib/Drupal/Core/Updater/Updater.php +++ b/core/lib/Drupal/Core/Updater/Updater.php @@ -4,12 +4,15 @@ use Drupal\Core\FileTransfer\FileTransferException; use Drupal\Core\FileTransfer\FileTransfer; +use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Defines the base class for Updaters used in Drupal. */ abstract class Updater { + use StringTranslationTrait; + /** * Directory to install from. * @@ -350,8 +353,8 @@ public function prepareInstallDirectory(&$filetransfer, $directory) { } catch (FileTransferException $e) { // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString - $message = t($e->getMessage(), $e->arguments); - $throw_message = t('Unable to create %directory due to the following: %reason', ['%directory' => $directory, '%reason' => $message]); + $message = $this->t($e->getMessage(), $e->arguments); + $throw_message = $this->t('Unable to create %directory due to the following: %reason', ['%directory' => $directory, '%reason' => $message]); throw new UpdaterException($throw_message); } } diff --git a/core/phpcs.xml.dist b/core/phpcs.xml.dist index af51590c698b..2f69df575db1 100644 --- a/core/phpcs.xml.dist +++ b/core/phpcs.xml.dist @@ -189,6 +189,7 @@ <include-pattern>*/modules/system/tests/modules/*</include-pattern> <include-pattern>*/Kernel*/*</include-pattern> <include-pattern>*/tests/*/Functional*/*</include-pattern> + <include-pattern>./core/lib/*</include-pattern> </rule> <!-- Generic sniffs --> -- GitLab