TYPO3 CMS  TYPO3_7-6
AudioTagRenderer.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
30  protected $possibleMimeTypes = ['audio/mpeg', 'audio/wav', 'audio/ogg'];
31 
41  public function getPriority()
42  {
43  return 1;
44  }
45 
52  public function canRender(FileInterface $file)
53  {
54  return in_array($file->getMimeType(), $this->possibleMimeTypes, true);
55  }
56 
67  public function render(FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
68  {
69 
70  // If autoplay isn't set manually check if $file is a FileReference take autoplay from there
71  if (!isset($options['autoplay']) && $file instanceof FileReference) {
72  $autoplay = $file->getProperty('autoplay');
73  if ($autoplay !== null) {
74  $options['autoplay'] = $autoplay;
75  }
76  }
77 
78  $additionalAttributes = [];
79  if (!isset($options['controls']) || !empty($options['controls'])) {
80  $additionalAttributes[] = 'controls';
81  }
82  if (!empty($options['autoplay'])) {
83  $additionalAttributes[] = 'autoplay';
84  }
85  if (!empty($options['muted'])) {
86  $additionalAttributes[] = 'muted';
87  }
88  if (!empty($options['loop'])) {
89  $additionalAttributes[] = 'loop';
90  }
91  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'preload', 'controlsList'] as $key) {
92  if (!empty($options[$key])) {
93  $additionalAttributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
94  }
95  }
96 
97  return sprintf(
98  '<audio%s><source src="%s" type="%s"></audio>',
99  empty($additionalAttributes) ? '' : ' ' . implode(' ', $additionalAttributes),
100  htmlspecialchars($file->getPublicUrl($usedPathsRelativeToCurrentScript)),
101  $file->getMimeType()
102  );
103  }
104 }
render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
getPublicUrl($relativeToCurrentScript=false)