From 2b24e72116c6377aa3c39c113e95da8419262eb0 Mon Sep 17 00:00:00 2001 From: webchick <drupal@webchick.net> Date: Mon, 25 Aug 2014 21:48:05 -0700 Subject: [PATCH] Issue #2183929 by cilefen, tim.plunkett: Allow \Drupal:: call in BlockBase to be bypassed. --- core/lib/Drupal/Core/Block/BlockBase.php | 33 +++++++++++++++++-- .../Drupal/Tests/Core/Block/BlockBaseTest.php | 3 +- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php index 37d5b93d1c11..ed2a63abb306 100644 --- a/core/lib/Drupal/Core/Block/BlockBase.php +++ b/core/lib/Drupal/Core/Block/BlockBase.php @@ -21,6 +21,7 @@ use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Session\AccountInterface; +use Drupal\Component\Transliteration\TransliterationInterface; /** * Defines a base block implementation that most blocks plugins will extend. @@ -49,6 +50,13 @@ abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginIn */ protected $conditionPluginManager; + /** + * The transliteration service. + * + * @var \Drupal\Component\Transliteration\TransliterationInterface + */ + protected $transliteration; + /** * {@inheritdoc} */ @@ -407,8 +415,7 @@ public function getMachineNameSuggestion() { // @todo This is basically the same as what is done in // \Drupal\system\MachineNameController::transliterate(), so it might make // sense to provide a common service for the two. - $transliteration_service = \Drupal::transliteration(); - $transliterated = $transliteration_service->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_'); + $transliterated = $this->transliteration()->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_'); $replace_pattern = '[^a-z0-9_.]+'; @@ -421,6 +428,28 @@ public function getMachineNameSuggestion() { return $transliterated; } + /** + * Wraps the transliteration service. + * + * @return \Drupal\Component\Transliteration\TransliterationInterface + */ + protected function transliteration() { + if (!$this->transliteration) { + $this->transliteration = \Drupal::transliteration(); + } + return $this->transliteration; + } + + /** + * Sets the transliteration service. + * + * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration + * The transliteration service. + */ + public function setTransliteration(TransliterationInterface $transliteration) { + $this->transliteration = $transliteration; + } + /** * Returns the cache contexts required for this block. * diff --git a/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php b/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php index 13320edf9428..77656bd8aadc 100644 --- a/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php +++ b/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php @@ -34,7 +34,6 @@ public function testGetMachineNameSuggestion() { ->will($this->returnValue(array())); $container = new ContainerBuilder(); $container->set('plugin.manager.condition', $condition_plugin_manager); - $container->set('transliteration', $transliteraton); \Drupal::setContainer($container); $config = array(); @@ -43,6 +42,7 @@ public function testGetMachineNameSuggestion() { 'provider' => 'block_test', ); $block_base = new TestBlockInstantiation($config, 'test_block_instantiation', $definition); + $block_base->setTransliteration($transliteraton); $this->assertEquals('adminlabel', $block_base->getMachineNameSuggestion()); // Test with more unicodes. @@ -51,6 +51,7 @@ public function testGetMachineNameSuggestion() { 'provider' => 'block_test', ); $block_base = new TestBlockInstantiation($config, 'test_block_instantiation', $definition); + $block_base->setTransliteration($transliteraton); $this->assertEquals('uberawesome', $block_base->getMachineNameSuggestion()); } -- GitLab