Issue #2060859 by tim.plunkett, Gábor Hojtsy: Make Block plugin's 'category' a translatable string.

8.0.x
webchick 2013-09-10 09:38:01 -07:00
parent 882c6f0f20
commit 7bdc1e61fa
4 changed files with 64 additions and 8 deletions

View File

@ -1,7 +1,7 @@
services:
plugin.manager.block:
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:
class: Drupal\Core\Cache\CacheBackendInterface
tags:

View File

@ -11,6 +11,7 @@ use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
* Manages discovery and instantiation of block plugins.
@ -21,6 +22,20 @@ use Drupal\Core\Plugin\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.
*
@ -33,12 +48,16 @@ class BlockManager extends DefaultPluginManager {
* The language manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* 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']);
parent::__construct('Plugin/Block', $namespaces, $annotation_namespaces, 'Drupal\block\Annotation\Block');
$this->alterInfo($module_handler, 'block');
$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);
// Ensure that every block has a category.
$definition += array(
'module' => $definition['provider'],
'category' => $definition['provider'],
);
if (!isset($definition['category'])) {
$definition['category'] = $this->getModuleName($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);
}
}

View File

@ -17,7 +17,7 @@ use Drupal\Core\Annotation\Translation;
* @Block(
* id = "system_menu_block",
* admin_label = @Translation("Menu"),
* category = "menu",
* category = @Translation("Menu"),
* derivative = "Drupal\system\Plugin\Derivative\SystemMenuBlock"
* )
*/

View File

@ -2370,7 +2370,9 @@ function system_get_info($type, $name = NULL) {
if ($type == 'module') {
$data = system_rebuild_module_data();
foreach (Drupal::moduleHandler()->getModuleList() as $module => $filename) {
$info[$module] = $data[$module]->info;
if (isset($data[$module])) {
$info[$module] = $data[$module]->info;
}
}
}
else {