Commit c14bbc49 authored by webchick's avatar webchick

Issue #2018411 by ParisLiakos, tim.plunkett, msonnabaum: Figure out a nice DX...

Issue #2018411 by ParisLiakos, tim.plunkett, msonnabaum: Figure out a nice DX when working with injected translation.
parent 7a0745a2
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Entity;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Utility\String;
......@@ -46,6 +47,13 @@ class EntityListController implements EntityListControllerInterface, EntityContr
*/
protected $entityInfo;
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* {@inheritdoc}
*/
......@@ -205,4 +213,40 @@ public function render() {
return $build;
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->getTranslationManager()->translate($string, $args, $options);
}
/**
* Gets the translation manager.
*
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function getTranslationManager() {
if (!$this->translationManager) {
$this->translationManager = \Drupal::translation();
}
return $this->translationManager;
}
/**
* Sets the translation manager for this form.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager.
*
* @return self
* The entity form.
*/
public function setTranslationManager(TranslationInterface $translation_manager) {
$this->translationManager = $translation_manager;
return $this;
}
}
......@@ -48,20 +48,7 @@ public function validateForm(array &$form, array &$form_state) {
/**
* Translates a string to the current language or to a given language.
*
* @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.
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->getTranslationManager()->translate($string, $args, $options);
......
......@@ -10,7 +10,7 @@
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Component\Utility\String;
use Drupal\Component\Discovery\YamlDiscovery;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -44,7 +44,7 @@ class StaticLocalActionDeriver implements ContainerDerivativeInterface {
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
......@@ -66,10 +66,10 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
* The base plugin ID.
* @param\Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\StringTranslation\TranslationManager translation_manager
* @param \Drupal\Core\StringTranslation\TranslationInterface translation_manager
* The translation manager.
*/
public function __construct($base_plugin_id, ModuleHandlerInterface $module_handler, TranslationManager $translation_manager) {
public function __construct($base_plugin_id, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager) {
$this->basePluginId = $base_plugin_id;
$this->moduleHandler = $module_handler;
$this->translationManager = $translation_manager;
......@@ -103,7 +103,7 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
$info += array('provider' => $module);
// Make sure 'appears_on' is an array.
$info['appears_on'] = (array) $info['appears_on'];
$info['title'] = $this->translationManager->translate($info['title']);
$info['title'] = $this->t($info['title']);
$this->derivatives[$name] = $info + $base_plugin_definition;
}
}
......@@ -112,4 +112,13 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
return $this->derivatives;
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translationManager->translate($string, $args, $options);
}
}
......@@ -9,7 +9,7 @@
use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -43,7 +43,7 @@ class AggregatorCategoryBlock implements ContainerDerivativeInterface {
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
......@@ -54,10 +54,10 @@ class AggregatorCategoryBlock implements ContainerDerivativeInterface {
* The base plugin ID.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translation_manager
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager.
*/
public function __construct($base_plugin_id, Connection $connection, TranslatorInterface $translation_manager) {
public function __construct($base_plugin_id, Connection $connection, TranslationInterface $translation_manager) {
$this->basePluginId = $base_plugin_id;
$this->connection = $connection;
$this->translationManager = $translation_manager;
......@@ -83,7 +83,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
}
$result = $this->connection->query('SELECT cid, title FROM {aggregator_category} ORDER BY title WHERE cid = :cid', array(':cid' => $derivative_id))->fetchObject();
$this->derivatives[$derivative_id] = $base_plugin_definition;
$this->derivatives[$derivative_id]['admin_label'] = $this->translationManager->translate('@title category latest items', array('@title' => $result->title));
$this->derivatives[$derivative_id]['admin_label'] = $this->t('@title category latest items', array('@title' => $result->title));
return $this->derivatives[$derivative_id];
}
......@@ -95,9 +95,18 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
$result = $this->connection->query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
foreach ($result as $category) {
$this->derivatives[$category->cid] = $base_plugin_definition;
$this->derivatives[$category->cid]['admin_label'] = $this->translationManager->translate('@title category latest items', array('@title' => $category->title));
$this->derivatives[$category->cid]['admin_label'] = $this->t('@title category latest items', array('@title' => $category->title));
}
return $this->derivatives;
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translationManager->translate($string, $args, $options);
}
}
......@@ -10,7 +10,6 @@
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Component\Archiver\ArchiveTar;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\system\FileDownloadController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -37,7 +36,7 @@ class ConfigController implements ControllerInterface {
/**
* The file download controller.
*
* @var \Drupal\Core\Controller\ControllerInterface
* @var \Drupal\system\FileDownloadController
*/
protected $fileDownloadController;
......@@ -45,10 +44,12 @@ class ConfigController implements ControllerInterface {
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$file_download = new FileDownloadController();
$file_download->setContainer($container);
return new static(
$container->get('config.storage'),
$container->get('config.storage.staging'),
FileDownloadController::create($container)
$file_download
);
}
......@@ -59,10 +60,10 @@ public static function create(ContainerInterface $container) {
* The target storage.
* @param \Drupal\Core\Config\StorageInterface $source_storage
* The source storage
* @param \Drupal\Core\Controller\ControllerInterface $file_download_controller
* @param \Drupal\system\FileDownloadController $file_download_controller
* The file download controller.
*/
public function __construct(StorageInterface $target_storage, StorageInterface $source_storage, ControllerInterface $file_download_controller) {
public function __construct(StorageInterface $target_storage, StorageInterface $source_storage, FileDownloadController $file_download_controller) {
$this->targetStorage = $target_storage;
$this->sourceStorage = $source_storage;
$this->fileDownloadController = $file_download_controller;
......
......@@ -8,12 +8,9 @@
namespace Drupal\image\Controller;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
use Drupal\image\ImageStyleInterface;
use Drupal\system\FileDownloadController;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -28,13 +25,6 @@
*/
class ImageStyleDownloadController extends FileDownloadController implements ControllerInterface {
/**
* The config factory.
*
* @var \Drupal\Core\config\ConfigFactory
*/
protected $configFactory;
/**
* The lock backend.
*
......@@ -42,13 +32,6 @@ class ImageStyleDownloadController extends FileDownloadController implements Con
*/
protected $lock;
/**
* The translator service.
*
* @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface
*/
protected $translator;
/**
* The image factory.
*
......@@ -59,22 +42,13 @@ class ImageStyleDownloadController extends FileDownloadController implements Con
/**
* Constructs a ImageStyleDownloadController object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory.
* @param \Drupal\Core\Lock\LockBackendInterface $lock
* The lock backend.
* @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator
* The translator service.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory.
*/
public function __construct(ModuleHandlerInterface $module_handler, ConfigFactory $config_factory, LockBackendInterface $lock, TranslatorInterface $translator, ImageFactory $image_factory) {
parent::__construct($module_handler);
$this->configFactory = $config_factory;
public function __construct(LockBackendInterface $lock, ImageFactory $image_factory) {
$this->lock = $lock;
$this->translator = $translator;
$this->imageFactory = $image_factory;
}
......@@ -83,10 +57,7 @@ public function __construct(ModuleHandlerInterface $module_handler, ConfigFactor
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
$container->get('config.factory'),
$container->get('lock'),
$container->get('string_translation'),
$container->get('image.factory')
);
}
......@@ -105,6 +76,8 @@ public static function create(ContainerInterface $container) {
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* Thrown when the user does not have access to the file.
* @throws \Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException
* Thrown when the file is still being generated.
*
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\Response
* The transferred file as response or some error response.
......@@ -120,7 +93,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
// bypass the latter check, but this will increase the site's vulnerability
// to denial-of-service attacks.
$valid = !empty($image_style) && file_stream_wrapper_valid_scheme($scheme);
if (!$this->configFactory->get('image.settings')->get('allow_insecure_derivatives')) {
if (!$this->config('image.settings')->get('allow_insecure_derivatives')) {
$valid &= $request->query->get(IMAGE_DERIVATIVE_TOKEN) === $image_style->getPathToken($image_uri);
}
if (!$valid) {
......@@ -137,7 +110,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
return parent::download($request, $scheme);
}
else {
$headers = $this->moduleHandler->invokeAll('file_download', array($image_uri));
$headers = $this->moduleHandler()->invokeAll('file_download', array($image_uri));
if (in_array(-1, $headers) || empty($headers)) {
throw new AccessDeniedHttpException();
}
......@@ -147,7 +120,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
// Don't try to generate file if source is missing.
if (!file_exists($image_uri)) {
watchdog('image', 'Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', array('%source_image_path' => $image_uri, '%derivative_path' => $derivative_uri));
return new Response($this->translator->translate('Error generating image, missing source file.'), 404);
return new Response($this->t('Error generating image, missing source file.'), 404);
}
// Don't start generating the image if the derivative already exists or if
......@@ -158,7 +131,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
if (!$lock_acquired) {
// Tell client to retry again in 3 seconds. Currently no browsers are
// known to support Retry-After.
throw new ServiceUnavailableHttpException(3, $this->translator->translate('Image generation in progress. Try again shortly.'));
throw new ServiceUnavailableHttpException(3, $this->t('Image generation in progress. Try again shortly.'));
}
}
......@@ -181,7 +154,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
}
else {
watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri));
return new Response($this->translator->translate('Error generating image.'), 500);
return new Response($this->t('Error generating image.'), 500);
}
}
......
......@@ -28,13 +28,6 @@ class ImageStyleListController extends ConfigEntityListController implements Ent
*/
protected $urlGenerator;
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface
*/
protected $translator;
/**
* Constructs a new ImageStyleListController object.
*
......@@ -48,13 +41,10 @@ class ImageStyleListController extends ConfigEntityListController implements Ent
* The module handler to invoke hooks on.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The URL generator.
* @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator
* The translation manager.
*/
public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $image_style_storage, ModuleHandlerInterface $module_handler, UrlGeneratorInterface $url_generator, TranslatorInterface $translator) {
public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $image_style_storage, ModuleHandlerInterface $module_handler, UrlGeneratorInterface $url_generator) {
parent::__construct($entity_type, $entity_info, $image_style_storage, $module_handler);
$this->urlGenerator = $url_generator;
$this->translator = $translator;
}
/**
......@@ -75,7 +65,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->translator->translate('Style name');
$header['label'] = $this->t('Style name');
return $header + parent::buildHeader();
}
......@@ -92,7 +82,7 @@ public function buildRow(EntityInterface $entity) {
*/
public function render() {
$build = parent::render();
$build['#empty'] = $this->translator->translate('There are currently no styles. <a href="!url">Add a new one</a>.', array(
$build['#empty'] = $this->t('There are currently no styles. <a href="!url">Add a new one</a>.', array(
'!url' => $this->urlGenerator->generateFromPath('admin/config/media/image-styles/add'),
));
return $build;
......
......@@ -7,9 +7,7 @@
namespace Drupal\system;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
......@@ -18,33 +16,7 @@
/**
* System file controller.
*/
class FileDownloadController implements ControllerInterface {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a FileDownloadController object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler')
);
}
class FileDownloadController extends ControllerBase {
/**
* Handles private file transfers.
......@@ -78,7 +50,7 @@ public function download(Request $request, $scheme = 'private') {
if (file_stream_wrapper_valid_scheme($scheme) && file_exists($uri)) {
// Let other modules provide headers and controls access to the file.
$headers = $this->moduleHandler->invokeAll('file_download', array($uri));
$headers = $this->moduleHandler()->invokeAll('file_download', array($uri));
foreach ($headers as $result) {
if ($result == -1) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment