NEW: Use templates to render embed shortcodes (closes #8762)

This commit is contained in:
Loz Calver 2019-08-19 16:35:40 +01:00
parent f676672f76
commit 759601741d
5 changed files with 75 additions and 22 deletions

View File

@ -5,6 +5,9 @@ namespace SilverStripe\View\Shortcodes;
use Embed\Http\DispatcherInterface;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Embed\Embeddable;
use SilverStripe\View\Embed\EmbedResource;
use SilverStripe\View\HTML;
@ -138,16 +141,13 @@ class EmbedShortcodeProvider implements ShortcodeHandler
$arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;';
}
// Convert caption to <p>
if (!empty($arguments['caption'])) {
$xmlCaption = Convert::raw2xml($arguments['caption']);
$content .= "\n<p class=\"caption\">{$xmlCaption}</p>";
}
unset($arguments['width']);
unset($arguments['height']);
unset($arguments['url']);
unset($arguments['caption']);
return HTML::createTag('div', $arguments, $content);
$data = [
'Arguments' => $arguments,
'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']),
'Content' => DBField::create_field('HTMLFragment', $content)
];
return ArrayData::create($data)->renderWith(self::class . '_video')->forTemplate();
}
/**
@ -160,13 +160,14 @@ class EmbedShortcodeProvider implements ShortcodeHandler
*/
protected static function linkEmbed($arguments, $href, $title)
{
$title = !empty($arguments['caption']) ? ($arguments['caption']) : $title;
unset($arguments['caption']);
unset($arguments['width']);
unset($arguments['height']);
unset($arguments['url']);
$arguments['href'] = $href;
return HTML::createTag('a', $arguments, Convert::raw2xml($title));
$data = [
'Arguments' => $arguments,
'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']),
'Href' => $href,
'Title' => !empty($arguments['caption']) ? ($arguments['caption']) : $title
];
return ArrayData::create($data)->renderWith(self::class . '_link')->forTemplate();
}
/**
@ -178,8 +179,37 @@ class EmbedShortcodeProvider implements ShortcodeHandler
*/
protected static function photoEmbed($arguments, $src)
{
$arguments['src'] = $src;
unset($arguments['url']);
return HTML::createTag('img', $arguments);
$data = [
'Arguments' => $arguments,
'Attributes' => static::buildAttributeListFromArguments($arguments, ['url']),
'Src' => $src
];
return ArrayData::create($data)->renderWith(self::class . '_photo')->forTemplate();
}
/**
* Build a list of HTML attributes from embed arguments - used to preserve backward compatibility
*
* @deprecated 4.5.0 Use {$Arguments.name} directly in shortcode templates to access argument values
* @param array $arguments List of embed arguments
* @param array $exclude List of attribute names to exclude from the resulting list
* @return ArrayList
*/
private static function buildAttributeListFromArguments(array $arguments, array $exclude = []): ArrayList
{
$attributes = ArrayList::create();
foreach ($arguments as $key => $value) {
if (in_array($key, $exclude)) {
continue;
}
$attributes->push(ArrayData::create([
'Name' => $key,
'Value' => Convert::raw2att($value)
]));
}
return $attributes;
}
}

View File

@ -0,0 +1,6 @@
<a
href="{$Href}"
<% loop $Attributes %> {$Name}="{$Value}"<% end_loop %>
>
{$Title}
</a>

View File

@ -0,0 +1,4 @@
<img
src="{$Src}"
<% loop $Attributes %> {$Name}="{$Value}"<% end_loop %>
/>

View File

@ -0,0 +1,8 @@
<div
<% loop $Attributes %> {$Name}="{$Value}"<% end_loop %>
>
{$Content}
<% if $Arguments.caption %>
<p class="caption">{$Arguments.caption}</p>
<% end_if %>
</div>

View File

@ -25,6 +25,11 @@ class EmbedShortcodeProviderTest extends SapphireTest
*/
protected static $test_soundcloud = 'http://soundcloud.com/napalmrecords/delain-suckerpunch';
public function assertEqualIgnoringWhitespace($a, $b, $message = '')
{
$this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message);
}
public function testYoutube()
{
/** @var string $result */
@ -51,7 +56,7 @@ class EmbedShortcodeProviderTest extends SapphireTest
'height' => 270,
]
);
$this->assertEquals(
$this->assertEqualIgnoringWhitespace(
<<<EOS
<div style="width: 480px;"><iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>
<p class="caption">A nice video</p></div>
@ -81,7 +86,7 @@ EOS
'author_url' => 'http://soundcloud.com/napalmrecords',
]
);
$this->assertEquals(
$this->assertEqualIgnoringWhitespace(
<<<EOS
<div style="width: 100px;"><iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F242518079&show_artwork=true"></iframe></div>
EOS