Skip to content

Commit

Permalink
added config for type translations
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Jul 25, 2016
1 parent 2275957 commit b223385
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 29 deletions.
58 changes: 55 additions & 3 deletions Admin/ArticleJsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sulu\Bundle\AdminBundle\Admin\JsConfigInterface;
use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
* Provides js-configuration.
Expand All @@ -22,29 +23,51 @@ class ArticleJsConfig implements JsConfigInterface
{
use TypeTrait;

/**
* @var TokenStorageInterface
*/
private $tokenStorage;

/**
* @var StructureManagerInterface
*/
private $structureManager;

/**
* @var array
*/
private $typeConfiguration;

/**
* @param TokenStorageInterface $tokenStorage
* @param StructureManagerInterface $structureManager
* @param $typeConfiguration
*/
public function __construct(StructureManagerInterface $structureManager)
{
public function __construct(
TokenStorageInterface $tokenStorage,
StructureManagerInterface $structureManager,
$typeConfiguration
) {
$this->tokenStorage = $tokenStorage;
$this->structureManager = $structureManager;
$this->typeConfiguration = $typeConfiguration;
}

/**
* {@inheritdoc}
*/
public function getParameters()
{
$locale = $this->getLocale();

$types = [];
foreach ($this->structureManager->getStructures('article') as $structure) {
$type = $this->getType($structure->getStructure());
if (!array_key_exists($type, $types)) {
$types[$type] = $structure->getKey();
$types[$type] = [
'default' => $structure->getKey(),
'title' => $this->getTitle($type, $locale),
];
}
}

Expand All @@ -58,4 +81,33 @@ public function getName()
{
return 'sulu_article.types';
}

/**
* Returns title for given type.
*
* @param string $type
* @param string $locale
*
* @return string
*/
private function getTitle($type, $locale)
{
if (!array_key_exists($type, $this->typeConfiguration)
|| !array_key_exists($locale, $this->typeConfiguration[$type]['translations'])
) {
return $type;
}

return $this->typeConfiguration[$type]['translations'][$locale];
}

/**
* Returns locale.
*
* @return string
*/
private function getLocale()
{
return $this->tokenStorage->getToken()->getUser()->getLocale();
}
}
15 changes: 14 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('sulu_article');
$treeBuilder->root('sulu_article')
->children()
->arrayNode('types')
->useAttributeAsKey('name')
->prototype('array')
->children()
->arrayNode('translations')
->useAttributeAsKey('locale')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end();

return $treeBuilder;
}
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('sulu_articles.types', $config['types']);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
<tag name="sulu.context" context="admin"/>
</service>
<service id="sulu_article.js_config" class="Sulu\Bundle\ArticleBundle\Admin\ArticleJsConfig">
<argument type="service" id="security.token_storage"/>
<argument type="service" id="sulu.content.structure_manager"/>
<argument>%sulu_articles.types%</argument>

<tag name="sulu.js_config"/>
<tag name="sulu.context" context="admin"/>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b223385

Please sign in to comment.