Skip to content

Commit cb66916

Browse files
committed
Fixed i18n form tabs for SnowcapAdminBundle
1 parent 106833c commit cb66916

File tree

4 files changed

+92
-25
lines changed

4 files changed

+92
-25
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ file in this bundle:
1818

1919
[Read the Documentation for master](https://github.com/snowcap/SnowcapI18nBundle/blob/master/Resources/doc/index.md)
2020

21+
The Changelog is available here:
22+
23+
[Read the Changelog for master](https://github.com/snowcap/SnowcapI18nBundle/blob/master/Resources/doc/changelog.md)
24+
2125
Installation
2226
------------
2327

Resources/doc/changelog.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
SnowcapI18nBundle - Changelog
2+
=============================
3+
4+
## 2015-07-10
5+
6+
* When using SnowcapAdminBundle & embedding the i18n_form.html.twig file, you must now also include the i18n_form.js
7+
in your ``base.html.twig`` file in your AdminBundle:
8+
9+
```jinja
10+
{# src/Acme/AdminBundle/Resources/views/base.html.twig #}
11+
12+
{% extends 'SnowcapAdminBundle::admin_base.html.twig' %}
13+
14+
{# ... #}
15+
16+
{% block javascripts %}
17+
{{ parent() }}
18+
{% javascripts
19+
'bundles/snowcapi18n/js/admin/i18n_form.js'
20+
output='cache/assetic/js/acmeadmin.js'
21+
%}
22+
<script type="text/javascript" src="{{ asset_url }}"></script>
23+
{% endjavascripts %}
24+
{% endblock javascripts %}
25+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
jQuery(document).ready(function () {
2+
// As the I18n form prototype will always have the same prefix it would be a problem
3+
// for collections having translations (eg. Questions have multiple Answers, and these answers are translated).
4+
// The current system does not work properly if we try to add 2+ Answers at the same time.
5+
// This fix replaces any prefix having the "fake prefix" to a new unique prefix on-the-fly.
6+
function replaceI18nTabTags() {
7+
$('[data-admin=translatable-content]').each(function (index, el) {
8+
var fakePrefix = 'I18N_TAB_PREFIX';
9+
var $el = $(el);
10+
11+
if ($el.attr('data-prefix') === fakePrefix) {
12+
// Prefix must be replaced
13+
var prefix = Math.floor(Math.random() * 10000000 + 1);
14+
$el.attr('data-prefix', prefix);
15+
16+
$('[data-toggle=tab]', $el).each(function (index, tab) {
17+
var $tab = $(tab);
18+
$tab.attr('href', $tab.attr('href').replace(fakePrefix, prefix));
19+
});
20+
21+
$('.tab-pane', $el).each(function (index, pane) {
22+
var $pane = $(pane);
23+
$pane.attr('id', $pane.attr('id').replace(fakePrefix, prefix));
24+
});
25+
}
26+
});
27+
}
28+
29+
// Calling it when the page is displayed (for already displayed tabs)
30+
replaceI18nTabTags();
31+
32+
// Calling it every time the form is changed
33+
$(document).on('change', 'form', function() {
34+
replaceI18nTabTags();
35+
});
36+
});
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
{% set prefix = random() %}
2-
<ul class="nav nav-tabs translation-tabs">
3-
{% for locale in _locales %}
4-
{% block tab_item %}
5-
<li class="{% if loop.first %}active{% endif %}"><a href="#{{ prefix }}_{{ locale }}" data-toggle="tab">{{ locale|language|capitalize }}</a></li>
6-
{% endblock %}
7-
{% endfor %}
8-
</ul>
9-
10-
<div class="tab-content translation-content">
11-
{% for locale in _locales %}
12-
<div class="tab-pane{% if loop.first %} active{% endif %}" id="{{ prefix }}_{{ locale }}" data-locale="{{ locale|language|capitalize }}">
13-
{% set translation = form.translations[locale] %}
14-
15-
{% block i18n_form_fields %}
16-
{% for field in translation %}
17-
{{ form_row(field) }}
18-
{% endfor %}
1+
{% set prefix = 'I18N_TAB_PREFIX' %}
2+
<div data-admin="translatable-content" data-prefix="{{ prefix }}">
3+
<ul class="nav nav-tabs translation-tabs">
4+
{% for locale in _locales %}
5+
{% block tab_item %}
6+
<li class="{% if loop.first %}active{% endif %}"><a href="#{{ prefix }}_{{ locale }}" data-toggle="tab">{{ locale|language|capitalize }}</a></li>
197
{% endblock %}
20-
</div>
21-
{% endfor %}
22-
{% if form.translations.vars.errors is not empty %}
23-
<div class="error">
24-
{{ form_errors(form.translations) }}
25-
</div>
26-
{% endif %}
8+
{% endfor %}
9+
</ul>
10+
11+
<div class="tab-content translation-content">
12+
{% for locale in _locales %}
13+
<div class="tab-pane{% if loop.first %} active{% endif %}" id="{{ prefix }}_{{ locale }}" data-locale="{{ locale|language|capitalize }}">
14+
{% set translation = form.translations[locale] %}
15+
16+
{% block i18n_form_fields %}
17+
{% for field in translation %}
18+
{{ form_row(field) }}
19+
{% endfor %}
20+
{% endblock %}
21+
</div>
22+
{% endfor %}
23+
{% if form.translations.vars.errors is not empty %}
24+
<div class="error">
25+
{{ form_errors(form.translations) }}
26+
</div>
27+
{% endif %}
28+
</div>
2729
</div>

0 commit comments

Comments
 (0)