Skip to content

Commit

Permalink
Merge pull request #9999 from robertpustulka/translation-field
Browse files Browse the repository at this point in the history
New translationField method.
  • Loading branch information
lorenzo committed Jan 13, 2017
2 parents b2c658c + 975a972 commit fcf7c07
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/ORM/Behavior/TranslateBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ class TranslateBehavior extends Behavior implements PropertyMarshalInterface
*/
protected $_defaultConfig = [
'implementedFinders' => ['translations' => 'findTranslations'],
'implementedMethods' => ['locale' => 'locale'],
'implementedMethods' => [
'locale' => 'locale',
'translationField' => 'translationField'
],
'fields' => [],
'translationTable' => 'I18n',
'defaultLocale' => '',
Expand Down Expand Up @@ -413,6 +416,28 @@ public function locale($locale = null)
return $this->_locale = (string)$locale;
}

/**
* Returns a fully aliased field name for translated fields.
*
* If the requested field is configured as a translation field, the `content`
* field with an alias of a corresponding association is returned. Table-aliased
* field name is returned for all other fields.
*
* @param string $field Field name to be aliased.
* @return string
*/
public function translationField($field)
{
$table = $this->_table;
$associationName = $table->getAlias() . '_' . $field . '_translation';

if ($table->associations()->has($associationName)) {
return $associationName . '.content';
}

return $table->aliasField($field);
}

/**
* Custom finder method used to retrieve all translations for the found records.
* Fetched translations can be filtered by locale by passing the `locales` key
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,36 @@ public function testFindSingleLocaleWithConditions()
$this->assertEquals($expected, $row->toArray());
}

/**
* Tests translationField method for translated fields.
*
* @return void
*/
public function testTranslationFieldForTranslatedFields()
{
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);

$expected = 'Articles_title_translation.content';
$field = $table->translationField('title');
$this->assertSame($expected, $field);
}

/**
* Tests translationField method for other fields.
*
* @return void
*/
public function testTranslationFieldForOtherFields()
{
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);

$expected = 'Articles.foo';
$field = $table->translationField('foo');
$this->assertSame($expected, $field);
}

/**
* Tests that translating fields work when other formatters are used
*
Expand Down

0 comments on commit fcf7c07

Please sign in to comment.