Skip to content

Commit f45a637

Browse files
authored
Merge pull request #201 from EmmanuelVella/twig-find-translation
Add cmf_find_translation twig function
2 parents 399e71a + 067aadc commit f45a637

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

Templating/Helper/CmfHelper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ public function find($path)
167167
return $this->getDm()->find(null, $path);
168168
}
169169

170+
/**
171+
* Finds a document by path and locale.
172+
*
173+
* @param string|object $pathOrDocument the identifier of the class (path or document object)
174+
* @param string $locale the language to try to load
175+
* @param bool $fallback set to true if the language fallback mechanism should be used
176+
*
177+
* @return null|object
178+
*/
179+
public function findTranslation($pathOrDocument, $locale, $fallback = true)
180+
{
181+
if (is_object($pathOrDocument)) {
182+
$path = $this->getDm()->getUnitOfWork()->getDocumentId($pathOrDocument);
183+
} else {
184+
$path = $pathOrDocument;
185+
}
186+
187+
return $this->getDm()->findTranslation(null, $path, $locale, $fallback);
188+
}
189+
170190
/**
171191
* Gets a document instance and validate if its eligible.
172192
*

Tests/Unit/Templating/Helper/CmfHelperTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ public function testFind()
137137
$this->assertEquals($document, $this->extension->find('/foo'));
138138
}
139139

140+
public function testFindTranslation()
141+
{
142+
$document = new \stdClass();
143+
144+
$this->manager->expects($this->any())
145+
->method('findTranslation')
146+
->with(null, '/foo', 'en')
147+
->will($this->onConsecutiveCalls(null, $document, 'en'))
148+
;
149+
150+
$this->assertNull($this->extension->findTranslation('/foo', 'en'));
151+
$this->assertEquals($document, $this->extension->findTranslation('/foo', 'en'));
152+
}
153+
140154
public function testFindMany()
141155
{
142156
$this->assertEquals(array(), $this->extension->findMany());

Tests/Unit/Twig/Extension/CmfExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function getFunctionsData()
6565
array('getPrev', array('current'), 'getPrev', array('current', null, null, false, null)),
6666
array('getNext', array('current'), 'getNext', array('current', null, null, false, null)),
6767
array('find', array('/cms/simple')),
68+
array('findTranslation', array('/cms/simple', 'en')),
6869
array('findMany', array(array('/cms/simple')), 'findMany', array(array('/cms/simple'), false, false, false, null)),
6970
array('getDescendants', array('parent', 2)),
7071
array('getNodeName', array('document1')),

Twig/Extension/CmfExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function getFunctions()
3636
new \Twig_SimpleFunction('cmf_prev', array($this, 'getPrev')),
3737
new \Twig_SimpleFunction('cmf_next', array($this, 'getNext')),
3838
new \Twig_SimpleFunction('cmf_find', array($this, 'find')),
39+
new \Twig_SimpleFunction('cmf_find_translation', array($this, 'findTranslation')),
3940
new \Twig_SimpleFunction('cmf_find_many', array($this, 'findMany')),
4041
new \Twig_SimpleFunction('cmf_descendants', array($this, 'getDescendants')),
4142
new \Twig_SimpleFunction('cmf_nodename', array($this, 'getNodeName')),
@@ -91,6 +92,11 @@ public function find($path)
9192
return $this->helper->find($path);
9293
}
9394

95+
public function findTranslation($path, $locale, $fallback = true)
96+
{
97+
return $this->helper->findTranslation($path, $locale, $fallback);
98+
}
99+
94100
public function findMany($paths = array(), $limit = false, $offset = false, $ignoreRole = false, $class = null)
95101
{
96102
return $this->helper->findMany($paths, $limit, $offset, $ignoreRole, $class);

0 commit comments

Comments
 (0)