Skip to content

Commit

Permalink
Merge 2.x into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Dec 19, 2016
2 parents 78a4ca6 + 500a6c4 commit d31c6f5
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 17 deletions.
12 changes: 11 additions & 1 deletion Admin/Extension/Gedmo/TranslatableAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

Expand Down Expand Up @@ -40,13 +41,22 @@ 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);
}

/**
* @param AdminInterface $admin Deprecated, set TranslatableListener in the constructor instead
*
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
113 changes: 113 additions & 0 deletions Filter/TranslationFieldFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\TranslationBundle\Filter;

use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\DoctrineORMAdminBundle\Filter\Filter;

final class TranslationFieldFilter extends Filter
{
/**
* {@inheritdoc}
*/
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
return;
}

$data['value'] = trim($data['value']);

if (strlen($data['value']) == 0) {
return;
}

$joinAlias = 'tff';

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

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

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

$this->active = true;
}

/**
* {@inheritdoc}
*/
public function getDefaultOptions()
{
return array(
'field_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
? 'Symfony\Component\Form\Extension\Core\Type\TextType'
: 'text', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
'operator_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
? 'Symfony\Component\Form\Extension\Core\Type\HiddenType'
: 'hidden', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
'operator_options' => array(),
);
}

/**
* {@inheritdoc}
*/
public function getRenderSettings()
{
// NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
$type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
? 'Sonata\AdminBundle\Form\Type\Filter\DefaultType'
: 'sonata_type_filter_default';

return array($type, array(
'field_type' => $this->getFieldType(),
'field_options' => $this->getFieldOptions(),
'operator_type' => $this->getOption('operator_type'),
'operator_options' => $this->getOption('operator_options'),
'label' => $this->getLabel(),
));
}

/**
* {@inheritdoc}
*/
protected function association(ProxyQueryInterface $queryBuilder, $data)
{
$alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());

return array($this->getOption('alias', $alias), $this->getFieldName());
}
}
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>
3 changes: 3 additions & 0 deletions Resources/config/service.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
</parameters>
<services>
<service id="sonata_translation.checker.translatable" class="%sonata_translation.checker.translatable.class%"/>
<service id="sonata_translation.filter.type.translation_field" class="Sonata\TranslationBundle\Filter\TranslationFieldFilter">
<tag name="sonata.admin.filter.type" alias="doctrine_orm_translation_field"/>
</service>
</services>
</container>
34 changes: 34 additions & 0 deletions Resources/doc/reference/orm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,40 @@ you have to make a translation class to handle it.
protected $object;
}
4. Configure search filter
--------------------------

**This step is optional**, but you can use the ``doctrine_orm_translation_field``
filter to search on fields and on their translations.

4.1 Example for configure search filter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: php
<?php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\TranslationBundle\Filter\TranslationFieldFilter;
class FAQCategoryAdmin extends AbstractAdmin
{
/**
* @param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
// ...
->add('title', TranslationFieldFilter::class); // or 'doctrine_orm_translation_field'
}
B. Using KnpLabs Doctrine Behaviours
------------------------------------

Expand Down
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
16 changes: 12 additions & 4 deletions Resources/views/Block/block_locale_switcher.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
{% set locale_switcher_route = block_context.settings.locale_switcher_route %}
{% set locale_switcher_route_parameters = block_context.settings.locale_switcher_route_parameters %}

{% if (object is translatable) %}
{% if admin.class is translatable %}
{% set currentLocale = object.locale|default(null) %}

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

<div class="locale_switcher">
{% spaceless %}
{% for locale in sonata_translation_locales %}
{% if (locale_switcher_route is empty) %}
{% if (object.id) %}
{% if locale_switcher_route is empty %}
{% if object.id %}
{% set locale_switcher_route = 'edit' %}
{% else %}
{% set locale_switcher_route = 'create' %}
Expand All @@ -19,7 +27,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[-2:2]|lower ~ '.png')}}" />
</a>
Expand Down

0 comments on commit d31c6f5

Please sign in to comment.