This repository has been archived by the owner on May 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
override elcodi.twig_extension.language
- Loading branch information
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
src/Elcodi/Admin/LanguageBundle/Twig/LanguageExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |