mediaembed.html.twig 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. {# Shortcut variables #}
  2. {% set default = mediaembed.config.media %}
  3. {% set oembed = mediaembed.service %}
  4. {# Processing error #}
  5. {% if not mediaembed.success %}
  6. {% if default.responsive %}
  7. {% set class = "-responsive mediaembed-msod" %}
  8. {% else %}
  9. {% set class = " mediaembed-msod\" style=\"max-width: " ~ default.width ~ "px;" %}
  10. {% endif %}
  11. <div class="mediaembed{{ class }}">
  12. <p class="mediaembed-icon">&#9749;</p>
  13. <p class="mediaembed-error-title"><b>Unable to process oEmbed media:</b><a href="{{ mediaembed.raw.src }}" alt="{{ mediaembed.raw.alt }}" title="{{ mediaembed.raw.title }}">{{ mediaembed.raw.src|e }}</a></p>
  14. <p class="mediaembed-error-message"><b>Error:</b>{{ mediaembed.message }}</p>
  15. </div>
  16. {# Setup block #}
  17. {% else %}
  18. {% set response = oembed.attributes %}
  19. {# Normalize response #}
  20. {% if (response.width is empty) or not (response.width is defined) or (response.width == 0) %}
  21. {% set response = response|merge({'width' : default.width|default(1)}) %}
  22. {% endif %}
  23. {% if (response.height is empty) or not (response.height is defined) or (response.height == 0) %}
  24. {% set response = response|merge({'height' : default.height|default(1)}) %}
  25. {% endif %}
  26. {% set width = response.width %}
  27. {% set height = response.height %}
  28. {% set ratio = height / width %}
  29. {# Adjust OEmbed media dimensions or restrict them? #}
  30. {% if default.adjust %}
  31. {# Check if computed height is larger than default setting #}
  32. {% if (width * ratio) > default.height or (height / ratio) > default.width %}
  33. {# Rescale width and height #}
  34. {% set width = default.width|default(1) %}
  35. {% set height = (default.width * ratio)|round %}
  36. {% endif %}
  37. {% else %}
  38. {% set width = default.width|default(1) %}
  39. {% set height = default.height|default(1) %}
  40. {% endif %}
  41. {# Recompute aspect ratio #}
  42. {% set ratio = height / width %}
  43. {% set container_styles = "padding-bottom: " ~ "%.2f"|format(ratio * 100) ~ "%;" %}
  44. {# Embed responsive OEmbed media #}
  45. {% if default.responsive %}
  46. {% set responsive = "-responsive" %}
  47. {% set width = 1920 %}
  48. {% set height = (width * ratio)|round %}
  49. {% else %}
  50. {% set styles = " style=\"max-width: " ~ width ~ "px; max-height: " ~ height ~ "px;\"" %}
  51. {% endif %}
  52. {# Setup lazy loading media for those with preview enabled #}
  53. {% if default.preview %}
  54. {% set lazyload = " lazyload" %}
  55. {% set lazyload_script = " onclick=\"lazyload(this)\"" %}
  56. {% endif %}
  57. <div class="mediaembed{{ responsive }} mediaembed-media mediaembed-{{ oembed.type }} mediaembed-{{ oembed.name|lower }}{{ lazyload }}"{{ styles }}>
  58. <div class="mediaembed-container" style="{{ container_styles }}">
  59. {# Embed content according to media type #}
  60. {# -- Photo -- #}
  61. {% if oembed.type == "photo" %}
  62. {% set title = oembed.getOembed().provider_name ~ ': &#8220;' ~ oembed.title ~ '&#8221; by ' ~ oembed.author %}
  63. <a class="mediaembed-embed" href="{{ oembed.author('url') }}" title="{{ title }}">
  64. <img src="{{ oembed.url }}" alt="{{ oembed.title }}">
  65. </a>
  66. {# -- Video -- #}
  67. {% elseif oembed.type == "video" %}
  68. <a href="{{ oembed.getEmbedCode }}" alt="{{ mediaembed.raw.alt }}" title="{{ mediaembed.raw.title }}" class="mediaembed-media"{{ lazyload_script }}>
  69. {# JavaScript lazy-loading #}
  70. <!--
  71. <iframe class="mediaembed-embed" src="{{ oembed.getEmbedCode }}" width="{{ width }}" height="{{ height }}" frameborder="0" scrolling="no" webkitallowfullscreen mozallowfullscreen allowfullscreen>
  72. <p>Your browser does not support iframes.</p>
  73. </iframe>
  74. -->
  75. {# Show Thumbnail #}
  76. {% if default.preview and oembed.thumbnail %}
  77. <img src="{{ oembed.thumbnail }}" alt="{{ mediaembed.raw.alt }}" title="{{ mediaembed.raw.title }}" class="mediaembed-thumbnail" />
  78. {% else %}
  79. {# Show placeholder #}
  80. {% endif %}
  81. {# Toggle for loading iframe content #}
  82. <input type="checkbox" id="mediaembed-hidden-input-{{ mediaembed.uid }}" class="mediaembed-input" />
  83. {# Implement JavaScript-less lazyload technique #}
  84. <noscript>
  85. <iframe class="mediaembed-embed" src="{{ oembed.getEmbedCode }}" width="{{ width }}" height="{{ height }}" frameborder="0" scrolling="no" webkitallowfullscreen mozallowfullscreen allowfullscreen style="display: none;">
  86. <p>Your browser does not support iframes.</p>
  87. </iframe>
  88. </noscript>
  89. <label class="mediaembed-play" for="mediaembed-hidden-input-{{ mediaembed.uid }}">&#9654;</label>
  90. </a>
  91. {# -- Link -- #}
  92. {% elseif oembed.type == "link" %}
  93. {% set title = oembed.getOembed().provider_name ~ ': &#8220;' ~ oembed.mediaembed.raw.title|default(title) ~ '&#8221; by ' ~ oembed.author %}
  94. <a href="{{ mediaembed.raw.src }}" alt="{{ mediaembed.raw.alt }}" title="{{ title }}">{{ mediaembed.raw.src|e }}</a>
  95. {# -- Rich media -- #}
  96. {% elseif oembed.type == "rich" %}
  97. <div class="mediaembed-media">{{ oembed.getEmbedCode() }}</div>
  98. {# -- Nothing from above -- #}
  99. {% else %}
  100. {# Show at least a link for the user #}
  101. <a href="{{ mediaembed.raw.src }}" alt="{{ mediaembed.raw.alt }}" title="{{ mediaembed.raw.title }}">{{ mediaembed.raw.src|e }}</a>
  102. {% endif %}
  103. </div>
  104. </div>
  105. {% endif %}