Restore legacy $ThemeDir support

This commit is contained in:
Damian Mooyman 2017-11-22 14:16:46 +13:00
parent 07a0f75426
commit 6e7fb4747e
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
3 changed files with 48 additions and 0 deletions

View File

@ -3,9 +3,12 @@
namespace SilverStripe\View;
use Exception;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
@ -586,6 +589,35 @@ class ViewableData implements IteratorAggregate
return $this;
}
/**
* Return the directory if the current active theme (relative to the site root).
*
* This method is useful for things such as accessing theme images from your template without hardcoding the theme
* page - e.g. <img src="$ThemeDir/images/something.gif">.
*
* This method should only be used when a theme is currently active. However, it will fall over to the current
* project directory.
*
* @return string URL to the current theme
* @deprecated 4.0.0..5.0.0 Use $resourcePath or $resourceURL template helpers instead
*/
public function ThemeDir()
{
Deprecation::notice('5.0', 'Use $resourcePath or $resourceURL template helpers instead');
$themes = SSViewer::get_themes();
foreach ($themes as $theme) {
// Skip theme sets
if (strpos($theme, '$') === 0) {
continue;
}
// Map theme path to url
$themePath = ThemeResourceLoader::inst()->getPath($theme);
return ModuleResourceLoader::resourceURL($themePath);
}
return project();
}
/**
* Get part of the current classes ancestry to be used as a CSS class.
*

View File

@ -5,6 +5,7 @@ namespace SilverStripe\View\Tests;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\View\ArrayData;
use SilverStripe\View\SSViewer;
use SilverStripe\View\ViewableData;
/**
@ -204,4 +205,19 @@ class ViewableDataTest extends SapphireTest
$this->assertSame($failover, $container->getFailover(), 'getFailover() returned a different object');
$this->assertFalse($container->hasMethod('testMethod'), 'testMethod() incorrectly reported as existing');
}
public function testThemeDir()
{
$themes = [
"silverstripe/framework:/tests/php/View/ViewableDataTest/testtheme",
SSViewer::DEFAULT_THEME
];
SSViewer::set_themes($themes);
$data = new ViewableData();
$this->assertContains(
'tests/php/View/ViewableDataTest/testtheme',
$data->ThemeDir()
);
}
}