‪TYPO3CMS  9.5
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 
20 
25 {
31  protected ‪$possibleMimeTypes = ['audio/mpeg', 'audio/wav', 'audio/ogg'];
32 
42  public function ‪getPriority()
43  {
44  return 1;
45  }
46 
53  public function ‪canRender(‪FileInterface $file)
54  {
55  return in_array($file->‪getMimeType(), $this->possibleMimeTypes, true);
56  }
57 
68  public function ‪render(‪FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
69  {
70 
71  // If autoplay isn't set manually check if $file is a FileReference take autoplay from there
72  if (!isset($options['autoplay']) && $file instanceof ‪FileReference) {
73  $autoplay = $file->‪getProperty('autoplay');
74  if ($autoplay !== null) {
75  $options['autoplay'] = $autoplay;
76  }
77  }
78 
79  $additionalAttributes = [];
80  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
81  $additionalAttributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
82  }
83  if (isset($options['data']) && is_array($options['data'])) {
84  array_walk($options['data'], function (&$value, $key) {
85  $value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
86  });
87  $additionalAttributes[] = implode(' ', $options['data']);
88  }
89  if (!isset($options['controls']) || !empty($options['controls'])) {
90  $additionalAttributes[] = 'controls';
91  }
92  if (!empty($options['autoplay'])) {
93  $additionalAttributes[] = 'autoplay';
94  }
95  if (!empty($options['muted'])) {
96  $additionalAttributes[] = 'muted';
97  }
98  if (!empty($options['loop'])) {
99  $additionalAttributes[] = 'loop';
100  }
101  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'preload', 'controlsList'] as $key) {
102  if (!empty($options[$key])) {
103  $additionalAttributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
104  }
105  }
106 
107  return sprintf(
108  '<audio%s><source src="%s" type="%s"></audio>',
109  empty($additionalAttributes) ? '' : ' ' . implode(' ', $additionalAttributes),
110  htmlspecialchars($file->‪getPublicUrl($usedPathsRelativeToCurrentScript)),
111  $file->‪getMimeType()
112  );
113  }
114 }
‪TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer\getPriority
‪int getPriority()
Definition: AudioTagRenderer.php:41
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: AudioTagRenderer.php:52
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer
Definition: AudioTagRenderer.php:25
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:2
‪TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
Definition: AudioTagRenderer.php:67
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:23
‪TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer\$possibleMimeTypes
‪array $possibleMimeTypes
Definition: AudioTagRenderer.php:30
‪TYPO3\CMS\Core\Resource\FileInterface\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
‪TYPO3\CMS\Core\Resource\FileInterface\getMimeType
‪string getMimeType()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45