diff --git a/src/View/ViewableData.php b/src/View/ViewableData.php index aca9ffa77..79d3a239f 100644 --- a/src/View/ViewableData.php +++ b/src/View/ViewableData.php @@ -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. . + * + * 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. * diff --git a/tests/php/View/ViewableDataTest.php b/tests/php/View/ViewableDataTest.php index 421b8b320..0f8582070 100644 --- a/tests/php/View/ViewableDataTest.php +++ b/tests/php/View/ViewableDataTest.php @@ -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() + ); + } } diff --git a/tests/php/View/ViewableDataTest/testtheme/empty.txt b/tests/php/View/ViewableDataTest/testtheme/empty.txt new file mode 100644 index 000000000..e69de29bb