Commit 2b24e721 authored by webchick's avatar webchick

Issue #2183929 by cilefen, tim.plunkett: Allow \Drupal:: call in BlockBase to be bypassed.

parent 8e3819bd
......@@ -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.
*
......
......@@ -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());
}
......
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