Issue #2060859 by tim.plunkett, Gábor Hojtsy: Make Block plugin's 'category' a translatable string.
parent
882c6f0f20
commit
7bdc1e61fa
|
@ -1,7 +1,7 @@
|
||||||
services:
|
services:
|
||||||
plugin.manager.block:
|
plugin.manager.block:
|
||||||
class: Drupal\block\Plugin\Type\BlockManager
|
class: Drupal\block\Plugin\Type\BlockManager
|
||||||
arguments: ['@container.namespaces', '@cache.block', '@language_manager', '@module_handler']
|
arguments: ['@container.namespaces', '@cache.block', '@language_manager', '@module_handler', '@string_translation']
|
||||||
cache.block:
|
cache.block:
|
||||||
class: Drupal\Core\Cache\CacheBackendInterface
|
class: Drupal\Core\Cache\CacheBackendInterface
|
||||||
tags:
|
tags:
|
||||||
|
|
|
@ -11,6 +11,7 @@ use Drupal\Core\Cache\CacheBackendInterface;
|
||||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||||
use Drupal\Core\Language\LanguageManager;
|
use Drupal\Core\Language\LanguageManager;
|
||||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||||
|
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages discovery and instantiation of block plugins.
|
* Manages discovery and instantiation of block plugins.
|
||||||
|
@ -21,6 +22,20 @@ use Drupal\Core\Plugin\DefaultPluginManager;
|
||||||
*/
|
*/
|
||||||
class BlockManager extends DefaultPluginManager {
|
class BlockManager extends DefaultPluginManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An array of all available modules and their data.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $moduleData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The translation manager.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\StringTranslation\TranslationInterface
|
||||||
|
*/
|
||||||
|
protected $translationManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new \Drupal\block\Plugin\Type\BlockManager object.
|
* Constructs a new \Drupal\block\Plugin\Type\BlockManager object.
|
||||||
*
|
*
|
||||||
|
@ -33,12 +48,16 @@ class BlockManager extends DefaultPluginManager {
|
||||||
* The language manager.
|
* The language manager.
|
||||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||||
* The module handler to invoke the alter hook with.
|
* The module handler to invoke the alter hook with.
|
||||||
|
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
|
||||||
|
* The translation manager.
|
||||||
*/
|
*/
|
||||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
|
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager) {
|
||||||
$annotation_namespaces = array('Drupal\block\Annotation' => $namespaces['Drupal\block']);
|
$annotation_namespaces = array('Drupal\block\Annotation' => $namespaces['Drupal\block']);
|
||||||
parent::__construct('Plugin/Block', $namespaces, $annotation_namespaces, 'Drupal\block\Annotation\Block');
|
parent::__construct('Plugin/Block', $namespaces, $annotation_namespaces, 'Drupal\block\Annotation\Block');
|
||||||
|
|
||||||
$this->alterInfo($module_handler, 'block');
|
$this->alterInfo($module_handler, 'block');
|
||||||
$this->setCacheBackend($cache_backend, $language_manager, 'block_plugins');
|
$this->setCacheBackend($cache_backend, $language_manager, 'block_plugins');
|
||||||
|
$this->translationManager = $translation_manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,10 +67,45 @@ class BlockManager extends DefaultPluginManager {
|
||||||
parent::processDefinition($definition, $plugin_id);
|
parent::processDefinition($definition, $plugin_id);
|
||||||
|
|
||||||
// Ensure that every block has a category.
|
// Ensure that every block has a category.
|
||||||
$definition += array(
|
if (!isset($definition['category'])) {
|
||||||
'module' => $definition['provider'],
|
$definition['category'] = $this->getModuleName($definition['provider']);
|
||||||
'category' => $definition['provider'],
|
}
|
||||||
);
|
// @todo Remove any usage of 'module' from block code.
|
||||||
|
if (!isset($definition['module'])) {
|
||||||
|
$definition['module'] = $definition['provider'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the name of the module.
|
||||||
|
*
|
||||||
|
* @param string $module
|
||||||
|
* The machine name of a module.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* The human-readable module name if it exists, otherwise the
|
||||||
|
* machine-readable module name.
|
||||||
|
*/
|
||||||
|
protected function getModuleName($module) {
|
||||||
|
// Gather module data.
|
||||||
|
if (!isset($this->moduleData)) {
|
||||||
|
$this->moduleData = system_get_info('module');
|
||||||
|
}
|
||||||
|
// If the module exists, return its human-readable name.
|
||||||
|
if (isset($this->moduleData[$module])) {
|
||||||
|
return $this->t($this->moduleData[$module]['name']);
|
||||||
|
}
|
||||||
|
// Otherwise, return the machine name.
|
||||||
|
return $module;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ use Drupal\Core\Annotation\Translation;
|
||||||
* @Block(
|
* @Block(
|
||||||
* id = "system_menu_block",
|
* id = "system_menu_block",
|
||||||
* admin_label = @Translation("Menu"),
|
* admin_label = @Translation("Menu"),
|
||||||
* category = "menu",
|
* category = @Translation("Menu"),
|
||||||
* derivative = "Drupal\system\Plugin\Derivative\SystemMenuBlock"
|
* derivative = "Drupal\system\Plugin\Derivative\SystemMenuBlock"
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -2370,7 +2370,9 @@ function system_get_info($type, $name = NULL) {
|
||||||
if ($type == 'module') {
|
if ($type == 'module') {
|
||||||
$data = system_rebuild_module_data();
|
$data = system_rebuild_module_data();
|
||||||
foreach (Drupal::moduleHandler()->getModuleList() as $module => $filename) {
|
foreach (Drupal::moduleHandler()->getModuleList() as $module => $filename) {
|
||||||
$info[$module] = $data[$module]->info;
|
if (isset($data[$module])) {
|
||||||
|
$info[$module] = $data[$module]->info;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Reference in New Issue