Skip to content

Commit

Permalink
ENH Improve typing to support refactored template layer
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Oct 9, 2024
1 parent df82059 commit 35782de
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/AssetControlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ protected function findAssets(DataObject $record)
}

// Omit variant and merge with set
$next = $record->dbObject($field)->getValue();
$next = $record->dbObject($field)?->getValue();
unset($next['Variant']);
if ($next) {
$files[] = $next;
Expand Down
6 changes: 2 additions & 4 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1322,16 +1322,14 @@ public function forTemplate(): string

/**
* Return a html5 tag of the appropriate for this file (normally img or a)
*
* @return string
*/
public function getTag()
public function getTag(): string
{
$template = $this->File->getFrontendTemplate();
if (empty($template)) {
return '';
}
return (string)$this->renderWith($template);
return $this->renderWith($template);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use SilverStripe\View\HTML;
use SilverStripe\View\Parsers\ShortcodeHandler;
use SilverStripe\View\Parsers\ShortcodeParser;
use SilverStripe\View\SSTemplateEngine;
use SilverStripe\View\SSViewer;
use SilverStripe\View\SSViewer_FromString;
use SilverStripe\View\ViewLayerData;

/**
* Provides shortcodes for File dataobject
Expand Down Expand Up @@ -237,9 +238,9 @@ protected static function find_error_record($errorCode)
*/
public static function getCacheKey($params, $content = null)
{
$key = SSViewer::config()->get('global_key');
$viewer = new SSViewer_FromString($key);
$globalKey = md5($viewer->process(ArrayData::create([])) ?? '');
$key = SSTemplateEngine::config()->get('global_key');
$engine = new SSTemplateEngine();
$globalKey = md5($engine->renderString($key, ViewLayerData::create([])));
$argsKey = md5(serialize($params)) . '#' . md5(serialize($content));

return "{$globalKey}#{$argsKey}";
Expand Down
7 changes: 4 additions & 3 deletions tests/php/ImageManipulationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\View\SSViewer;
use PHPUnit\Framework\Attributes\DataProvider;
use SilverStripe\View\SSTemplateEngine;
use SilverStripe\View\ViewLayerData;

/**
* ImageTest is abstract and should be overridden with manipulator-specific subtests
Expand Down Expand Up @@ -447,10 +448,10 @@ public function testRender(string $template, string $expected)
{
/** @var Image $origin */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$engine = new SSTemplateEngine();
$this->assertEquals(
$expected,
trim($image->renderWith(SSViewer::fromString($template)) ?? '')
trim($engine->renderString($template, ViewLayerData::create($image)))
);
}

Expand Down

0 comments on commit 35782de

Please sign in to comment.