Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Improve typing to support refactored template layer #647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbObject() can return null

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);
}
Comment on lines -1325 to 1333
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just adding a bit of type safety since this method gets used in forTemplate() above.


/**
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 = SSTemplateEngine::create();
$globalKey = md5($engine->renderString($key, ViewLayerData::create([])));
Comment on lines +241 to +243
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly use SSTemplateEngine instead of getting whatever's registered against TemplateEngine because this config is specific to SSTemplateEngine and is explicitly ss template syntax.

$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
Loading