Skip to content
This repository has been archived by the owner on May 30, 2019. It is now read-only.

Commit

Permalink
override elcodi.twig_extension.language
Browse files Browse the repository at this point in the history
  • Loading branch information
xphere committed Jun 5, 2015
1 parent 0284cbc commit 09f9340
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function getConfigFilesLocation()
*/
public function getConfigFiles(array $config)
{
return [];
return [
'twig',
];
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Elcodi/Admin/LanguageBundle/Resources/config/twig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:

#
# Twig extensions
#
# Overrides elcodi.twig_extension.language
#
elcodi.admin.twig_extension.language:
class: Elcodi\Admin\LanguageBundle\Twig\LanguageExtension
decorates: elcodi.twig_extension.language
arguments:
- @elcodi.manager.language
- @=elcodi_config('store.default_language')
tags:
- { name: twig.extension }
113 changes: 113 additions & 0 deletions src/Elcodi/Admin/LanguageBundle/Twig/LanguageExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/*
* This file is part of the Elcodi package.
*
* Copyright (c) 2014-2015 Elcodi.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
* @author Aldo Chiecchia <zimage@tiscali.it>
* @author Elcodi Team <tech@elcodi.com>
*/

namespace Elcodi\Admin\LanguageBundle\Twig;

use Twig_Extension;

use Elcodi\Component\Language\Entity\Interfaces\LanguageInterface;
use Elcodi\Component\Language\Services\LanguageManager;

/**
* Class LanguageExtension
*/
class LanguageExtension extends Twig_Extension
{
/**
* @var LanguageManager
*
* Language manager
*/
protected $languageManager;

/**
* @var string
*
* Master locale ISO
*/
protected $masterLocaleIso;

/**
* Construct method
*
* @param LanguageManager $languageManager Language manager
* @param string $masterLocaleIso Master locale ISO
*/
public function __construct(
LanguageManager $languageManager,
$masterLocaleIso
) {
$this->languageManager = $languageManager;
$this->masterLocaleIso = $masterLocaleIso;
}

/**
* Returns a list of global variables to add to the existing list.
*
* @return array An array of global variables
*/
public function getGlobals()
{
return [
'elcodi_languages' => $this->getLanguages(),
];
}

/**
* Return all available languages
*
* @return array Available languages
*/
public function getLanguages()
{
$languages = $this
->languageManager
->getLanguages()
->toArray();

return $this->promoteMasterLanguage($languages);
}

/**
* return extension name
*
* @return string extension name
*/
public function getName()
{
return 'language_extension';
}

/**
* Move master language to the first position
*
* @param LanguageInterface[] $languages
*
* @return LanguageInterface[]
*/
protected function promoteMasterLanguage(array $languages)
{
$index = array_search($this->masterLocaleIso, $languages);

if (false !== $index) {
unset($languages[$index]);
array_unshift($languages, $this->masterLocaleIso);
}

return $languages;
}
}

0 comments on commit 09f9340

Please sign in to comment.