Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to switch content language on list and filter by language #119

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion Admin/Extension/Gedmo/TranslatableAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Gedmo\Translatable\TranslatableListener;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
use Sonata\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension;
use Sonata\TranslationBundle\Checker\TranslatableChecker;

Expand Down Expand Up @@ -40,13 +42,70 @@ public function alterObject(AdminInterface $admin, $object)
if ($this->getTranslatableChecker()->isTranslatable($object)) {
$translatableListener = $this->getTranslatableListener($admin);
$translatableListener->setTranslatableLocale($this->getTranslatableLocale($admin));
$translatableListener->setTranslationFallback('');
$translatableListener->setTranslationFallback(false);

$this->getContainer($admin)->get('doctrine')->getManager()->refresh($object);
$object->setLocale($this->getTranslatableLocale($admin));
}
}

/**
* {@inheritdoc}
*/
public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
{
$this->getTranslatableListener($admin)->setTranslatableLocale($this->getTranslatableLocale($admin));
$this->getTranslatableListener($admin)->setTranslationFallback(false);
}

/**
* Search on normal field and on translated field, use with a
* doctrine_orm_callback filter type.
*
* @param ProxyQuery $queryBuilder
* @param string $alias
* @param string $field
* @param string $value
*
* @return bool|null
*/
public static function translationFieldFilter(ProxyQuery $queryBuilder, $alias, $field, $value)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never used or at least no hint how to use it.

{
if (!$value['value']) {
return;
}

// verify if the join is not already done
$aliasAlreadyExists = false;
$joinDqlParts = $queryBuilder->getDQLParts()['join'];
foreach ($joinDqlParts as $joins) {
foreach ($joins as $join) {
if ($join->getAlias() === 't') {
$aliasAlreadyExists = true;
break 2;
}
}
}

if (!$aliasAlreadyExists) {
$queryBuilder->leftJoin($alias.'.translations', 't');
}

// search on translation OR on normal field
$queryBuilder->andWhere($queryBuilder->expr()->orX(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('t.field', $queryBuilder->expr()->literal($field)),
$queryBuilder->expr()->like('t.content', $queryBuilder->expr()->literal('%'.$value['value'].'%'))
),
$queryBuilder->expr()->like(
sprintf('%s.%s', $alias, $field),
$queryBuilder->expr()->literal('%'.$value['value'].'%')
)
));

return true;
}

/**
* @param AdminInterface $admin Deprecated, set TranslatableListener in the constructor instead.
*
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 2.1.0 (unreleased)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line. We add this line when releasing a new version.

### Added
- [gedmo translations] Added content language switcher to lists
- [gedmo translations] Added available translations filter

## [2.0.2](https://github.com/sonata-project/SonataTranslationBundle/compare/2.0.1...2.0.2) - 2016-06-15
### Fixed
- Remove wrong `doctrine-orm-admin-bundle` and `doctrine-phpcr-admin-bundle` composer requirements
Expand Down
3 changes: 3 additions & 0 deletions EventListener/LocaleSwitcherListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function onBlock(BlockEvent $event, $eventName)
if ($eventName == 'sonata.block.event.sonata.admin.show.top') {
$settings['locale_switcher_route'] = 'show';
}
if ($eventName == 'sonata.block.event.sonata.admin.list.table.top') {
$settings['locale_switcher_route'] = 'list';
}

$block = new Block();
$block->setSettings($settings);
Expand Down
1 change: 1 addition & 0 deletions Resources/config/listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<service id="sonata_translation.listener.locale_switcher" class="%sonata_translation.listener.locale_switcher.class%">
<tag name="kernel.event_listener" event="sonata.block.event.sonata.admin.edit.form.top" method="onBlock"/>
<tag name="kernel.event_listener" event="sonata.block.event.sonata.admin.show.top" method="onBlock"/>
<tag name="kernel.event_listener" event="sonata.block.event.sonata.admin.list.table.top" method="onBlock"/>
</service>
</services>
</container>
43 changes: 32 additions & 11 deletions Resources/public/css/sonata-translation.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,96 @@
* SonataTranslation: adds translations to your forms
*/
.sonata-bc .sonata-ba-form .locale_switcher,
.sonata-bc .sonata-ba-show .locale_switcher {
.sonata-bc .sonata-ba-show .locale_switcher,
.sonata-bc .box-primary .locale_switcher {
padding: 1px 0 0 0;
text-align: right;
margin-right: 15px;
margin-bottom: 5px;
float: right;
}
.sonata-bc .sonata-ba-form .locale_switcher a,
.sonata-bc .sonata-ba-show .locale_switcher a {
.sonata-bc .sonata-ba-show .locale_switcher a,
.sonata-bc .box-primary .locale_switcher a {
margin-right: 0;
padding: 2px;
opacity: 0.5;
}
.sonata-bc .sonata-ba-form .locale_switcher a.active,
.sonata-bc .sonata-ba-show .locale_switcher a.active {
.sonata-bc .sonata-ba-show .locale_switcher a.active,
.sonata-bc .box-primary .locale_switcher a.active {
opacity: 1;
}
.sonata-bc .sonata-ba-form .locale_switcher a:hover,
.sonata-bc .sonata-ba-show .locale_switcher a:hover {
.sonata-bc .sonata-ba-show .locale_switcher a:hover,
.sonata-bc .box-primary .locale_switcher a:hover {
opacity: 1;
}
.sonata-bc .sonata-ba-form label.wysiwyg.locale,
.sonata-bc .sonata-ba-show label.wysiwyg.locale,
.sonata-bc .box-primary label.wysiwyg.locale,
.sonata-bc .sonata-ba-form label.checkbox.locale,
.sonata-bc .sonata-ba-show label.checkbox.locale,
.sonata-bc .box-primary label.checkbox.locale,
.sonata-bc .sonata-ba-form input.locale,
.sonata-bc .sonata-ba-show input.locale,
.sonata-bc .box-primary input.locale,
.sonata-bc .sonata-ba-form textarea.locale,
.sonata-bc .sonata-ba-show textarea.locale,
.sonata-bc .box-primary textarea.locale,
.sonata-bc .sonata-ba-form select.locale,
.sonata-bc .sonata-ba-show select.locale,
.sonata-bc .box-primary select.locale,
.sonata-bc .sonata-ba-form li.locale a,
.sonata-bc .sonata-ba-show li.locale a {
.sonata-bc .sonata-ba-show li.locale a,
.sonata-bc .box-primary li.locale a {
background-repeat: no-repeat;
}
.sonata-bc .sonata-ba-form input[type="text"].locale,
.sonata-bc .sonata-ba-show input[type="text"].locale,
.sonata-bc .box-primary input[type="text"].locale,
.sonata-bc .sonata-ba-form select.locale,
.sonata-bc .sonata-ba-show select.locale {
.sonata-bc .sonata-ba-show select.locale,
.sonata-bc .box-primary select.locale {
padding-left: 35px !important;
background-position: 5px center;
}
.sonata-bc .sonata-ba-form textarea.locale,
.sonata-bc .sonata-ba-show textarea.locale {
.sonata-bc .sonata-ba-show textarea.locale,
.sonata-bc .box-primary textarea.locale {
padding-left: 35px;
background-position: 5px 5px;
}
.sonata-bc .sonata-ba-form label.wysiwyg.locale,
.sonata-bc .sonata-ba-show label.wysiwyg.locale,
.sonata-bc .box-primary label.wysiwyg.locale,
.sonata-bc .sonata-ba-form label.checkbox.locale,
.sonata-bc .sonata-ba-show label.checkbox.locale {
.sonata-bc .sonata-ba-show label.checkbox.locale,
.sonata-bc .box-primary label.checkbox.locale {
padding-right: 35px;
background-position: right bottom;
}
.sonata-bc .sonata-ba-form .form-horizontal .controls input[type="checkbox"],
.sonata-bc .sonata-ba-show .form-horizontal .controls input[type="checkbox"] {
.sonata-bc .sonata-ba-show .form-horizontal .controls input[type="checkbox"],
.sonata-bc .box-primary .form-horizontal .controls input[type="checkbox"] {
/*top: 5px;*/
position: relative;
width: 15px;
}
.sonata-bc .sonata-ba-form .form-horizontal .controls input[type="checkbox"].locale,
.sonata-bc .sonata-ba-show .form-horizontal .controls input[type="checkbox"].locale {
.sonata-bc .sonata-ba-show .form-horizontal .controls input[type="checkbox"].locale,
.sonata-bc .box-primary .form-horizontal .controls input[type="checkbox"].locale {
width: 65px;
background-position: 0 center;
}
.sonata-bc .sonata-ba-form .form-horizontal .controls input[type="checkbox"]:before,
.sonata-bc .sonata-ba-show .form-horizontal .controls input[type="checkbox"]:before {
.sonata-bc .sonata-ba-show .form-horizontal .controls input[type="checkbox"]:before,
.sonata-bc .box-primary .form-horizontal .controls input[type="checkbox"]:before {
margin-right: 35px;
}
.sonata-bc .sonata-ba-show .locale_switcher {
margin-right: 15px;
}
.sonata-bc.locale_nl label.wysiwyg.locale,
.sonata-bc.locale_nl label.checkbox.locale,
.sonata-bc.locale_nl input.locale,
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/less/sonata-translation.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
.sonata-bc {

.sonata-ba-form, .sonata-ba-show {
.sonata-ba-form, .sonata-ba-show, .box-primary {
.locale_switcher {
padding: 1px 0 0 0;
text-align: right;
Expand Down
11 changes: 9 additions & 2 deletions Resources/views/Block/block_locale_switcher.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
{% set admin = block_context.settings.admin %}
{% set locale_switcher_route = block_context.settings.locale_switcher_route %}
{% set locale_switcher_route_parameters = block_context.settings.locale_switcher_route_parameters %}
{% set currentLocale = object.locale %}
Copy link

@skyosev skyosev Jul 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is null in the list route, so trying to access its property throws an exception:

Impossible to access an attribute ("locale") on a null variable in SonataTranslationBundle:Block:block_locale_switcher.html.twig


{% if (admin.class is translatable) %}
{% for extension in admin.extensions %}
{% if (extension.translatableLocale is defined) %}
{% set currentLocale = extension.translatableLocale(admin) %}
{% endif %}
{% endfor %}

{% if (object is translatable) %}
<div class="locale_switcher">
{% spaceless %}
{% for locale in sonata_translation_locales %}
Expand All @@ -19,7 +26,7 @@
{'id': admin.id(object), 'tl': locale}|merge(locale_switcher_route_parameters)
) }}"
accesskey=""
class=" {% if (object.locale == locale) %} active {% endif %}"
class=" {% if (currentLocale == locale) %} active {% endif %}"
title="{{ 'admin.locale_switcher.tooltip' |trans([], 'SonataTranslationBundle') }}">
<img src="{{ asset('bundles/sonatatranslation/img/flags/' ~ locale ~ '.png')}}" />
</a>
Expand Down