Skip to content

Commit 8037c51

Browse files
committed
add Multilingual form widget
1 parent c414e58 commit 8037c51

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

src/widgets/Multilingual.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @author Dmytro Karpovych
4+
* @copyright 2016 NRE
5+
*/
6+
namespace nullref\core\widgets;
7+
8+
use nullref\core\interfaces\ILanguageManager;
9+
use Yii;
10+
use yii\base\Widget;
11+
12+
class Multilingual extends Widget
13+
{
14+
public $tab;
15+
public $model;
16+
public $languages = null;
17+
18+
/**
19+
* @var MultilingualForm
20+
*/
21+
public $form;
22+
23+
public function init()
24+
{
25+
parent::init();
26+
if (!count($this->languages)) {
27+
/** @var ILanguageManager $languageManager */
28+
$languageManager = Yii::$app->get('languageManager');
29+
$this->languages = $languageManager->getLanguages();
30+
}
31+
}
32+
33+
34+
public function run()
35+
{
36+
$this->view->registerJs(<<<JS
37+
jQuery('form .nav.nav-tabs').find('a').each(function (index, item) {
38+
var link = jQuery(item);
39+
var errors = jQuery(link.attr('href')).find('.has-error');
40+
if (errors.length) {
41+
link.css('color', '#A94442');
42+
link.html('<i class="fa fa-exclamation-triangle"></i>' + link.text());
43+
}
44+
});
45+
JS
46+
);
47+
$form = new MultilingualForm();
48+
ob_get_clean();
49+
return $this->render('multilingual', [
50+
'form' => $form,
51+
'tab' => $this->tab,
52+
'model' => $this->model,
53+
'languages' => $this->languages,
54+
]);
55+
}
56+
}

src/widgets/MultilingualForm.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @author Dmytro Karpovych
4+
* @copyright 2016 NRE
5+
*/
6+
namespace nullref\core\widgets;
7+
8+
use yii\widgets\ActiveForm;
9+
10+
class MultilingualForm extends ActiveForm
11+
{
12+
public $language;
13+
14+
public function field($model, $attribute, $options = [])
15+
{
16+
if ($this->language) {
17+
return parent::field($model, $attribute . '_' . $this->language, $options);
18+
}
19+
return parent::field($model, $attribute, $options);
20+
}
21+
}

src/widgets/views/multilingual.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* @var $this \yii\web\View
4+
* @var $form \nullref\core\widgets\MultilingualForm
5+
* @var $model \yii\base\Model
6+
* @var $tab Closure
7+
*/
8+
use app\helpers\Languages;
9+
10+
?>
11+
<style>
12+
.multilingual-tab-content {
13+
padding: 15px;
14+
margin-bottom: 5px;
15+
border: 1px solid #ddd;
16+
border-top: none;
17+
border-radius: 5px;
18+
border-top-left-radius: 0;
19+
border-top-right-radius: 0;
20+
}
21+
</style>
22+
<div class="multilingual">
23+
<ul class="nav nav-tabs">
24+
<?php $first = true;
25+
foreach (Languages::getSlugMap() as $key => $lang): ?>
26+
<li class="<?= ($first) ? 'active' : '' ?>">
27+
<a data-toggle="tab" href="#<?= $lang ?>">
28+
<?= Yii::t('translation', Languages::getMap()[$key]) ?>
29+
</a>
30+
</li>
31+
<?php $first = false; endforeach ?>
32+
</ul>
33+
<div class="tab-content multilingual-tab-content" style="padding: 10px 5px">
34+
<?php $first = true;
35+
foreach (Languages::getSlugMap() as $key => $lang): ?>
36+
<div id="<?= $lang ?>" class="tab-pane <?= ($first) ? 'active' : '' ?>">
37+
<?php $form->language = $lang ?>
38+
<?= call_user_func($tab, $form, $model) ?>
39+
</div>
40+
<?php $first = false; endforeach ?>
41+
</div>
42+
</div>

0 commit comments

Comments
 (0)