Skip to content

Commit 4f577c0

Browse files
Elements should switch to prefixed template path if available
1 parent b299e2a commit 4f577c0

File tree

14 files changed

+121
-17
lines changed

14 files changed

+121
-17
lines changed

src/View/View.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -964,17 +964,7 @@ protected function _getLayoutFileName($name = null)
964964
}
965965
list($plugin, $name) = $this->pluginSplit($name);
966966

967-
$layoutPaths = ['Layout' . DS . $subDir];
968-
if (!empty($this->request->params['prefix'])) {
969-
$prefixPath = array_map(
970-
'Cake\Utility\Inflector::camelize',
971-
explode('/', $this->request->params['prefix'])
972-
);
973-
array_unshift(
974-
$layoutPaths,
975-
implode('/', $prefixPath) . DS . $layoutPaths[0]
976-
);
977-
}
967+
$layoutPaths = $this->_getSubPaths('Layout' . DS . $subDir);
978968

979969
foreach ($this->_paths($plugin) as $path) {
980970
foreach ($layoutPaths as $layoutPath) {
@@ -1000,14 +990,45 @@ protected function _getElementFileName($name)
1000990
list($plugin, $name) = $this->pluginSplit($name);
1001991

1002992
$paths = $this->_paths($plugin);
993+
$elementPaths = $this->_getSubPaths('Element');
994+
1003995
foreach ($paths as $path) {
1004-
if (file_exists($path . 'Element' . DS . $name . $this->_ext)) {
1005-
return $path . 'Element' . DS . $name . $this->_ext;
996+
foreach ($elementPaths as $elementPath) {
997+
if (file_exists($path . $elementPath . DS . $name . $this->_ext)) {
998+
return $path . $elementPath . DS . $name . $this->_ext;
999+
}
10061000
}
10071001
}
10081002
return false;
10091003
}
10101004

1005+
/**
1006+
* Find all sub templates path, based on $basePath
1007+
* If a prefix is defined in the current request, this method will prepend
1008+
* the prefixed template path to the $basePath.
1009+
* This is essentially used to find prefixed template paths for elements
1010+
* and layouts.
1011+
*
1012+
* @param string $basePath Base path on which to get the prefixed one.
1013+
* @return array Array with all the templates paths.
1014+
*/
1015+
protected function _getSubPaths($basePath)
1016+
{
1017+
$paths = [$basePath];
1018+
if (!empty($this->request->params['prefix'])) {
1019+
$prefixPath = array_map(
1020+
'Cake\Utility\Inflector::camelize',
1021+
explode('/', $this->request->params['prefix'])
1022+
);
1023+
array_unshift(
1024+
$paths,
1025+
implode('/', $prefixPath) . DS . $basePath
1026+
);
1027+
}
1028+
1029+
return $paths;
1030+
}
1031+
10111032
/**
10121033
* Return all possible paths to find view files in order
10131034
*

tests/TestCase/View/Helper/FlashHelperTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,18 @@ public function testFlashWithTheme()
169169
$expected = 'flash element from TestTheme';
170170
$this->assertContains($expected, $result);
171171
}
172+
173+
/**
174+
* test that when View prefix is set, flash element from that prefix
175+
* is used if available.
176+
*
177+
* @return void
178+
*/
179+
public function testFlashWithPrefix()
180+
{
181+
$this->View->request->params['prefix'] = 'Admin';
182+
$result = $this->Flash->render('flash');
183+
$expected = 'flash element from Admin prefix folder';
184+
$this->assertContains($expected, $result);
185+
}
172186
}

tests/TestCase/View/ViewTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,29 @@ public function testElement()
816816
$this->assertEquals('this is the test set using View::$plugin plugin element', $result);
817817
}
818818

819+
/**
820+
* Test element method with a prefix
821+
*
822+
* @return void
823+
*/
824+
public function testPrefixElement()
825+
{
826+
$this->View->request->params['prefix'] = 'Admin';
827+
$result = $this->View->element('prefix_element');
828+
$this->assertEquals('this is a prefixed test element', $result);
829+
830+
$result = $this->View->element('TestPlugin.plugin_element');
831+
$this->assertEquals('this is the plugin prefixed element using params[plugin]', $result);
832+
833+
$this->View->plugin = 'TestPlugin';
834+
$result = $this->View->element('test_plugin_element');
835+
$this->assertEquals('this is the test set using View::$plugin plugin prefixed element', $result);
836+
837+
$this->View->request->params['prefix'] = 'FooPrefix/BarPrefix';
838+
$result = $this->View->element('prefix_element');
839+
$this->assertEquals('this is a nested prefixed test element', $result);
840+
}
841+
819842
/**
820843
* Test elementInexistent method
821844
*
@@ -1710,6 +1733,26 @@ public function testExtendElement()
17101733
Parent Element.
17111734
Element content.
17121735
1736+
TEXT;
1737+
$this->assertEquals($expected, $content);
1738+
}
1739+
1740+
/**
1741+
* Test extend() in an element and a view.
1742+
*
1743+
* @return void
1744+
*/
1745+
public function testExtendPrefixElement()
1746+
{
1747+
$this->View->request->params['prefix'] = 'Admin';
1748+
$this->View->layout = false;
1749+
$content = $this->View->render('extend_element');
1750+
$expected = <<<TEXT
1751+
Parent View.
1752+
View content.
1753+
Parent Element.
1754+
Prefix Element content.
1755+
17131756
TEXT;
17141757
$this->assertEquals($expected, $content);
17151758
}
@@ -1745,6 +1788,24 @@ public function testExtendWithElementBeforeExtend()
17451788
Parent View.
17461789
this is the test elementThe view
17471790
1791+
TEXT;
1792+
$this->assertEquals($expected, $result);
1793+
}
1794+
1795+
/**
1796+
* Test extend() preceeded by an element()
1797+
*
1798+
* @return void
1799+
*/
1800+
public function testExtendWithPrefixElementBeforeExtend()
1801+
{
1802+
$this->View->request->params['prefix'] = 'Admin';
1803+
$this->View->layout = false;
1804+
$result = $this->View->render('extend_with_element');
1805+
$expected = <<<TEXT
1806+
Parent View.
1807+
this is the test prefix elementThe view
1808+
17481809
TEXT;
17491810
$this->assertEquals($expected, $result);
17501811
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is the plugin prefixed element using params[plugin]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is the test set using View::$plugin plugin prefixed element
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?= 'this is the plugin element'; ?>
1+
this is the plugin element
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?= 'this is the plugin element using params[plugin]'; ?>
1+
this is the plugin element using params[plugin]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?= 'this is the test set using View::$plugin plugin element'; ?>
1+
this is the test set using View::$plugin plugin element
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flash element from Admin prefix folder
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php $this->extend('parent_element'); ?>
2+
Prefix Element content.

0 commit comments

Comments
 (0)