Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\View\Shortcodes;
- use SilverStripe\Core\Convert;
- use SilverStripe\View\HTML;
- use SilverStripe\View\Parsers\ShortcodeHandler;
- use SilverStripe\View\Shortcodes\EmbedShortcodeProvider as SilverStripeEmbedShortcodeProvider;
- /**
- * Overrides the built-in embed shortcode provider so we can create adjust
- * the HTML to create responsive video embeds
- */
- class EmbedShortcodeProvider extends SilverStripeEmbedShortcodeProvider implements ShortcodeHandler
- {
- protected static function videoEmbed($arguments, $content): string
- {
- // Convert caption to <p>
- if (!empty($arguments['caption'])) {
- $xmlCaption = Convert::raw2xml($arguments['caption']);
- $content .= "\n<p class=\"caption\">{$xmlCaption}</p>";
- }
- $html = HTML::createTag('div', ['class' => 'responsive-video'], $content);
- if (!empty($arguments['width'])) {
- $arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;';
- }
- unset($arguments['width'], $arguments['height'], $arguments['url'], $arguments['caption']);
- return HTML::createTag('div', $arguments, $html);
- }
- public static function embedForTemplate($embed, $arguments): ?string
- {
- switch ($embed->getType()) {
- case 'video':
- case 'rich':
- // Attempt to inherit width (but leave height auto)
- if (empty($arguments['width']) && $embed->getWidth()) {
- $arguments['width'] = $embed->getWidth();
- }
- return static::videoEmbed($arguments, $embed->getCode());
- case 'link':
- return static::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle());
- case 'photo':
- return static::photoEmbed($arguments, $embed->getUrl());
- default:
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement