diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php new file mode 100644 index 0000000..cba8a8f --- /dev/null +++ b/core/lib/Drupal/Core/Form/FormBase.php @@ -0,0 +1,76 @@ +translationManager = $translation_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('string_translation') + ); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + // Validation is optional. + } + + /** + * Translates a string to the current language or to a given language using + * the string translation service. + * + * @param string $string + * A string containing the English string to translate. + * @param array $args + * An associative array of replacements to make after translation. Based + * on the first character of the key, the value is escaped and/or themed. + * See \Drupal\Core\Utility\String::format() for details. + * @param array $options + * An associative array of additional options, with the following elements: + * - 'langcode': The language code to translate to a language other than + * what is used to display the page. + * - 'context': The context the source string belongs to. + * + * @return string + * The translated string. + */ + protected function t($string, array $args = array(), array $options = array()) { + return $this->translationManager->translate($string, $args, $options); + } + +} diff --git a/core/modules/config/lib/Drupal/config/Form/ConfigSync.php b/core/modules/config/lib/Drupal/config/Form/ConfigSync.php index 8c5cb96..4aa5b7d 100644 --- a/core/modules/config/lib/Drupal/config/Form/ConfigSync.php +++ b/core/modules/config/lib/Drupal/config/Form/ConfigSync.php @@ -7,11 +7,9 @@ namespace Drupal\config\Form; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; - +use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Form\FormBase; use Drupal\Core\Controller\ControllerInterface; -use Drupal\Core\Form\FormInterface; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Config\StorageComparer; @@ -20,12 +18,13 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; use Drupal\Core\Routing\PathBasedGeneratorInterface; -use \Drupal\Core\Entity\EntityManager; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Construct the storage changes in a configuration synchronization form. */ -class ConfigSync implements ControllerInterface, FormInterface { +class ConfigSync extends FormBase implements ControllerInterface { /** * The database lock object. @@ -66,14 +65,6 @@ class ConfigSync implements ControllerInterface, FormInterface { protected $entity_manager; /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface - */ - protected $translationManager; - - - /** * URL generator service. * * @var \Drupal\Core\Routing\PathBasedGeneratorInterface @@ -83,8 +74,6 @@ class ConfigSync implements ControllerInterface, FormInterface { /** * Constructs the object. * - * @param \Drupal\Core\Database\Connection; $database - * The database object. * @param \Drupal\Core\Config\StorageInterface $sourceStorage * The source storage object. * @param \Drupal\Core\Config\StorageInterface $targetStorage @@ -95,21 +84,22 @@ class ConfigSync implements ControllerInterface, FormInterface { * Event dispatcher. * @param \Drupal\Core\Config\ConfigFactory $config_factory * Configuration object factory. - * @param \Drupal\Core\Entity\EntityManager + * @param \Drupal\Core\Entity\EntityManager $entity_manager * Entity manager. * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translation_manager * The translation manager. * @param \Drupal\Core\Routing\PathBasedGeneratorInterface $url_generator * The url generator service. */ - public function __construct(StorageInterface $sourceStorage, StorageInterface $targetStorage, LockBackendInterface $lock, EventDispatcherInterface $event_dispatcher, ConfigFactory $config_factory, EntityManager $entity_manger, TranslatorInterface $translation_manager, PathBasedGeneratorInterface $url_generator) { + public function __construct(StorageInterface $sourceStorage, StorageInterface $targetStorage, LockBackendInterface $lock, EventDispatcherInterface $event_dispatcher, ConfigFactory $config_factory, EntityManager $entity_manager, TranslatorInterface $translation_manager, PathBasedGeneratorInterface $url_generator) { + parent::__construct($translation_manager); + $this->sourceStorage = $sourceStorage; $this->targetStorage = $targetStorage; $this->lock = $lock; $this->eventDispatcher = $event_dispatcher; $this->configFactory = $config_factory; - $this->entity_manager = $entity_manger; - $this->translationManager = $translation_manager; + $this->entity_manager = $entity_manager; $this->urlGenerator = $url_generator; } @@ -143,14 +133,14 @@ public function buildForm(array $form, array &$form_state) { $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', - '#value' => $this->translationManager->translate('Import all'), + '#value' => $this->t('Import all'), ); $source_list = $this->sourceStorage->listAll(); $config_comparer = new StorageComparer($this->sourceStorage, $this->targetStorage); if (empty($source_list) || !$config_comparer->createChangelist()->hasChanges()) { $form['no_changes'] = array( - '#markup' => $this->translationManager->translate('There are no configuration changes.'), + '#markup' => $this->t('There are no configuration changes.'), ); $form['actions']['#access'] = FALSE; return $form; @@ -194,7 +184,7 @@ public function buildForm(array $form, array &$form_state) { foreach ($config_files as $config_file) { $links['view_diff'] = array( - 'title' => $this->translationManager->translate('View differences'), + 'title' => $this->t('View differences'), 'href' => $this->urlGenerator->getPathFromRoute('config_diff', array('config_file' => $config_file)), 'attributes' => array( 'class' => array('use-ajax'), @@ -222,12 +212,6 @@ public function buildForm(array $form, array &$form_state) { /** * {@inheritdoc} */ - public function validateForm(array &$form, array &$form_state) { - } - - /** - * {@inheritdoc} - */ public function submitForm(array &$form, array &$form_state) { $config_importer = new ConfigImporter( $form_state['storage_comparer'], @@ -237,13 +221,13 @@ public function submitForm(array &$form, array &$form_state) { $this->lock ); if ($config_importer->alreadyImporting()) { - drupal_set_message($this->translationManager->translate('Another request may be synchronizing configuration already.')); + drupal_set_message($this->t('Another request may be synchronizing configuration already.')); } else{ try { $config_importer->import(); drupal_flush_all_caches(); - drupal_set_message($this->translationManager->translate('The configuration was imported successfully.')); + drupal_set_message($this->t('The configuration was imported successfully.')); } catch (ConfigException $e) { // Return a negative result for UI purposes. We do not differentiate @@ -252,7 +236,7 @@ public function submitForm(array &$form, array &$form_state) { // multiple developers or site builders attempt to do it without // coordinating. watchdog_exception('config_import', $e); - drupal_set_message($this->translationManager->translate('The import failed due to an error. Any errors have been logged.'), 'error'); + drupal_set_message($this->t('The import failed due to an error. Any errors have been logged.'), 'error'); } } } diff --git a/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php b/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php index c699c09..8738850 100644 --- a/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php +++ b/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php @@ -9,9 +9,9 @@ use Drupal\Core\Controller\ControllerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Form\FormInterface; -use Drupal\Core\KeyValueStore\KeyValueExpirableFactory; +use Drupal\Core\Form\FormBase; use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface; +use Drupal\Core\Routing\PathBasedGeneratorInterface; use Drupal\Core\StringTranslation\TranslationManager; use Drupal\Component\Utility\Unicode; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -25,7 +25,7 @@ * requires. See drupal_parse_info_file() for info on module.info.yml * descriptors. */ -class ModulesListForm implements FormInterface, ControllerInterface { +class ModulesListForm extends FormBase implements ControllerInterface { /** * The module handler service. @@ -42,13 +42,6 @@ class ModulesListForm implements FormInterface, ControllerInterface { protected $keyValueExpirable; /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationManager - */ - protected $translationManager; - - /** * The request object. * * @var \Symfony\Component\HttpFoundation\Request @@ -77,9 +70,10 @@ public static function create(ContainerInterface $container) { * The translation manager. */ public function __construct(ModuleHandlerInterface $module_handler, KeyValueStoreExpirableInterface $key_value_expirable, TranslationManager $translation_manager) { + parent::__construct($translation_manager); + $this->moduleHandler = $module_handler; $this->keyValueExpirable = $key_value_expirable; - $this->translationManager = $translation_manager; } /** @@ -111,14 +105,14 @@ public function buildForm(array $form, array &$form_state, Request $request = NU $form['filters']['text'] = array( '#type' => 'search', - '#title' => $this->translationManager->translate('Search'), + '#title' => $this->t('Search'), '#size' => 30, - '#placeholder' => $this->translationManager->translate('Enter module name'), + '#placeholder' => $this->t('Enter module name'), '#attributes' => array( 'class' => array('table-filter-text'), 'data-table' => '#system-modules', 'autocomplete' => 'off', - 'title' => $this->translationManager->translate('Enter a part of the module name or description to filter by.'), + 'title' => $this->t('Enter a part of the module name or description to filter by.'), ), ); @@ -139,12 +133,12 @@ public function buildForm(array $form, array &$form_state, Request $request = NU foreach (element_children($form['modules']) as $package) { $form['modules'][$package] += array( '#type' => 'details', - '#title' => $this->translationManager->translate($package), + '#title' => $this->t($package), '#theme' => 'system_modules_details', '#header' => array( - array('data' => '' . $this->translationManager->translate('Enabled') . '', 'class' => array('checkbox')), - array('data' => $this->translationManager->translate('Name'), 'class' => array('name')), - array('data' => $this->translationManager->translate('Description'), 'class' => array('description', RESPONSIVE_PRIORITY_LOW)), + array('data' => '' . $this->t('Enabled') . '', 'class' => array('checkbox')), + array('data' => $this->t('Name'), 'class' => array('name')), + array('data' => $this->t('Description'), 'class' => array('description', RESPONSIVE_PRIORITY_LOW)), ), '#attributes' => array('class' => array('package-listing')), // Ensure that the "Core" package comes first. @@ -159,7 +153,7 @@ public function buildForm(array $form, array &$form_state, Request $request = NU $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', - '#value' => $this->translationManager->translate('Save configuration'), + '#value' => $this->t('Save configuration'), ); return $form; @@ -184,7 +178,7 @@ protected function buildRow(array $modules, $module, $distribution) { $row['#required_by'] = array(); $row['name']['#markup'] = $module->info['name']; - $row['description']['#markup'] = $this->translationManager->translate($module->info['description']); + $row['description']['#markup'] = $this->t($module->info['description']); $row['version']['#markup'] = $module->info['version']; // Add links for each module. @@ -197,9 +191,9 @@ protected function buildRow(array $modules, $module, $distribution) { if ($this->moduleHandler->invoke($module->name, 'help', array("admin/help#$module->name", $help))) { $row['links']['help'] = array( '#type' => 'link', - '#title' => $this->translationManager->translate('Help'), + '#title' => $this->t('Help'), '#href' => "admin/help/$module->name", - '#options' => array('attributes' => array('class' => array('module-link', 'module-link-help'), 'title' => $this->translationManager->translate('Help'))), + '#options' => array('attributes' => array('class' => array('module-link', 'module-link-help'), 'title' => $this->t('Help'))), ); } } @@ -209,9 +203,9 @@ protected function buildRow(array $modules, $module, $distribution) { if ($module->status && user_access('administer permissions') && in_array($module->name, $this->moduleHandler->getImplementations('permission'))) { $row['links']['permissions'] = array( '#type' => 'link', - '#title' => $this->translationManager->translate('Permissions'), + '#title' => $this->t('Permissions'), '#href' => 'admin/people/permissions', - '#options' => array('fragment' => 'module-' . $module->name, 'attributes' => array('class' => array('module-link', 'module-link-permissions'), 'title' => $this->translationManager->translate('Configure permissions'))), + '#options' => array('fragment' => 'module-' . $module->name, 'attributes' => array('class' => array('module-link', 'module-link-permissions'), 'title' => $this->t('Configure permissions'))), ); } @@ -221,7 +215,7 @@ protected function buildRow(array $modules, $module, $distribution) { if (($configure = menu_get_item($module->info['configure'])) && $configure['access']) { $row['links']['configure'] = array( '#type' => 'link', - '#title' => $this->translationManager->translate('Configure'), + '#title' => $this->t('Configure'), '#href' => $configure['href'], '#options' => array('attributes' => array('class' => array('module-link', 'module-link-configure'), 'title' => $configure['description'])), ); @@ -231,7 +225,7 @@ protected function buildRow(array $modules, $module, $distribution) { // Present a checkbox for installing and indicating the status of a module. $row['enable'] = array( '#type' => 'checkbox', - '#title' => $this->translationManager->translate('Enable'), + '#title' => $this->t('Enable'), '#default_value' => (bool) $module->status, ); @@ -249,7 +243,7 @@ protected function buildRow(array $modules, $module, $distribution) { // Check the core compatibility. if ($module->info['core'] != DRUPAL_CORE_COMPATIBILITY) { $compatible = FALSE; - $status .= $this->translationManager->translate('This version is not compatible with Drupal !core_version and should be replaced.', array( + $status .= $this->t('This version is not compatible with Drupal !core_version and should be replaced.', array( '!core_version' => DRUPAL_CORE_COMPATIBILITY, )); } @@ -258,7 +252,7 @@ protected function buildRow(array $modules, $module, $distribution) { if (version_compare(phpversion(), $module->info['php']) < 0) { $compatible = FALSE; $required = $module->info['php'] . (substr_count($module->info['php'], '.') < 2 ? '.*' : ''); - $status .= $this->translationManager->translate('This module requires PHP version @php_required and is incompatible with PHP version !php_version.', array( + $status .= $this->t('This module requires PHP version @php_required and is incompatible with PHP version !php_version.', array( '@php_required' => $required, '!php_version' => phpversion(), )); @@ -276,7 +270,7 @@ protected function buildRow(array $modules, $module, $distribution) { // If this module requires other modules, add them to the array. foreach ($module->requires as $dependency => $version) { if (!isset($modules[$dependency])) { - $row['#requires'][$dependency] = $this->translationManager->translate('@module (missing)', array('@module' => Unicode::ucfirst($dependency))); + $row['#requires'][$dependency] = $this->t('@module (missing)', array('@module' => Unicode::ucfirst($dependency))); $row['enable']['#disabled'] = TRUE; } // Only display visible modules. @@ -285,7 +279,7 @@ protected function buildRow(array $modules, $module, $distribution) { // Disable the module's checkbox if it is incompatible with the // dependency's version. if ($incompatible_version = drupal_check_incompatibility($version, str_replace(DRUPAL_CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']))) { - $row['#requires'][$dependency] = $this->translationManager->translate('@module (incompatible with version @version)', array( + $row['#requires'][$dependency] = $this->t('@module (incompatible with version @version)', array( '@module' => $name . $incompatible_version, '@version' => $modules[$dependency]->info['version'], )); @@ -294,16 +288,16 @@ protected function buildRow(array $modules, $module, $distribution) { // Disable the checkbox if the dependency is incompatible with this // version of Drupal core. elseif ($modules[$dependency]->info['core'] != DRUPAL_CORE_COMPATIBILITY) { - $row['#requires'][$dependency] = $this->translationManager->translate('@module (incompatible with this version of Drupal core)', array( + $row['#requires'][$dependency] = $this->t('@module (incompatible with this version of Drupal core)', array( '@module' => $name, )); $row['enable']['#disabled'] = TRUE; } elseif ($modules[$dependency]->status) { - $row['#requires'][$dependency] = $this->translationManager->translate('@module', array('@module' => $name)); + $row['#requires'][$dependency] = $this->t('@module', array('@module' => $name)); } else { - $row['#requires'][$dependency] = $this->translationManager->translate('@module (disabled)', array('@module' => $name)); + $row['#requires'][$dependency] = $this->t('@module (disabled)', array('@module' => $name)); } } } @@ -313,11 +307,11 @@ protected function buildRow(array $modules, $module, $distribution) { foreach ($module->required_by as $dependent => $version) { if (isset($modules[$dependent]) && empty($modules[$dependent]->info['hidden'])) { if ($modules[$dependent]->status == 1 && $module->status == 1) { - $row['#required_by'][$dependent] = $this->translationManager->translate('@module', array('@module' => $modules[$dependent]->info['name'])); + $row['#required_by'][$dependent] = $this->t('@module', array('@module' => $modules[$dependent]->info['name'])); $row['enable']['#disabled'] = TRUE; } else { - $row['#required_by'][$dependent] = $this->translationManager->translate('@module (disabled)', array('@module' => $modules[$dependent]->info['name'])); + $row['#required_by'][$dependent] = $this->t('@module (disabled)', array('@module' => $modules[$dependent]->info['name'])); } } } @@ -412,12 +406,6 @@ protected function buildModuleList(array $form_state) { /** * {@inheritdoc} */ - public function validateForm(array &$form, array &$form_state) { - } - - /** - * {@inheritdoc} - */ public function submitForm(array &$form, array &$form_state) { // Retrieve a list of modules to enable/disable and their dependencies. $modules = $this->buildModuleList($form_state); diff --git a/core/modules/system/lib/Drupal/system/Form/ModulesUninstallForm.php b/core/modules/system/lib/Drupal/system/Form/ModulesUninstallForm.php index a6b3dea..a8649db 100644 --- a/core/modules/system/lib/Drupal/system/Form/ModulesUninstallForm.php +++ b/core/modules/system/lib/Drupal/system/Form/ModulesUninstallForm.php @@ -7,19 +7,18 @@ namespace Drupal\system\Form; -use Drupal\Core\Form\FormInterface; -use Drupal\Core\StringTranslation\TranslationManager; -use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Controller\ControllerInterface; -use Drupal\Core\KeyValueStore\KeyValueExpirableFactory; -use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Form\FormBase; +use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface; +use Drupal\Core\StringTranslation\TranslationManager; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; /** * Provides a form for uninstalling modules. */ -class ModulesUninstallForm implements FormInterface, ControllerInterface { +class ModulesUninstallForm extends FormBase implements ControllerInterface { /** * The module handler service. @@ -36,13 +35,6 @@ class ModulesUninstallForm implements FormInterface, ControllerInterface { protected $keyValueExpirable; /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationManager - */ - protected $translationManager; - - /** * The request object. * * @var \Symfony\Component\HttpFoundation\Request @@ -71,9 +63,10 @@ public static function create(ContainerInterface $container) { * The translation manager. */ public function __construct(ModuleHandlerInterface $module_handler, KeyValueStoreExpirableInterface $key_value_expirable, TranslationManager $translation_manager) { + parent::__construct($translation_manager); + $this->moduleHandler = $module_handler; $this->keyValueExpirable = $key_value_expirable; - $this->translationManager = $translation_manager; } /** @@ -118,11 +111,11 @@ public function buildForm(array $form, array &$form_state, Request $request = NU $name = $module->info['name'] ?: $module->name; $form['modules'][$module->name]['#module_name'] = $name; $form['modules'][$module->name]['name']['#markup'] = $name; - $form['modules'][$module->name]['description']['#markup'] = $this->translationManager->translate($module->info['description']); + $form['modules'][$module->name]['description']['#markup'] = $this->t($module->info['description']); $form['uninstall'][$module->name] = array( '#type' => 'checkbox', - '#title' => $this->translationManager->translate('Uninstall @module module', array('@module' => $name)), + '#title' => $this->t('Uninstall @module module', array('@module' => $name)), '#title_display' => 'invisible', ); @@ -141,7 +134,7 @@ public function buildForm(array $form, array &$form_state, Request $request = NU $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', - '#value' => $this->translationManager->translate('Uninstall'), + '#value' => $this->t('Uninstall'), ); return $form; @@ -153,7 +146,7 @@ public function buildForm(array $form, array &$form_state, Request $request = NU public function validateForm(array &$form, array &$form_state) { // Form submitted, but no modules selected. if (!array_filter($form_state['values']['uninstall'])) { - drupal_set_message($this->translationManager->translate('No modules selected.'), 'error'); + drupal_set_message($this->t('No modules selected.'), 'error'); $form_state['redirect'] = 'admin/modules/uninstall'; } }