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