Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\View\Shortcodes;
  4.  
  5. use SilverStripe\Core\Convert;
  6. use SilverStripe\View\HTML;
  7. use SilverStripe\View\Parsers\ShortcodeHandler;
  8. use SilverStripe\View\Shortcodes\EmbedShortcodeProvider as SilverStripeEmbedShortcodeProvider;
  9.  
  10. /**
  11. * Overrides the built-in embed shortcode provider so we can create adjust
  12. * the HTML to create responsive video embeds
  13. */
  14. class EmbedShortcodeProvider extends SilverStripeEmbedShortcodeProvider implements ShortcodeHandler
  15. {
  16. protected static function videoEmbed($arguments, $content): string
  17. {
  18. // Convert caption to <p>
  19. if (!empty($arguments['caption'])) {
  20. $xmlCaption = Convert::raw2xml($arguments['caption']);
  21. $content .= "\n<p class=\"caption\">{$xmlCaption}</p>";
  22. }
  23.  
  24. $html = HTML::createTag('div', ['class' => 'responsive-video'], $content);
  25.  
  26. if (!empty($arguments['width'])) {
  27. $arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;';
  28. }
  29. unset($arguments['width'], $arguments['height'], $arguments['url'], $arguments['caption']);
  30. return HTML::createTag('div', $arguments, $html);
  31. }
  32.  
  33. public static function embedForTemplate($embed, $arguments): ?string
  34. {
  35. switch ($embed->getType()) {
  36. case 'video':
  37. case 'rich':
  38. // Attempt to inherit width (but leave height auto)
  39. if (empty($arguments['width']) && $embed->getWidth()) {
  40. $arguments['width'] = $embed->getWidth();
  41. }
  42. return static::videoEmbed($arguments, $embed->getCode());
  43. case 'link':
  44. return static::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle());
  45. case 'photo':
  46. return static::photoEmbed($arguments, $embed->getUrl());
  47. default:
  48. return null;
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement