Contao\Controller::addEnclosuresToTemplate PHP Method

addEnclosuresToTemplate() public static method

Add enclosures to a template
public static addEnclosuresToTemplate ( object $objTemplate, array $arrItem, string $strKey = 'enclosure' )
$objTemplate object The template object to add the enclosures to
$arrItem array The element or module as array
$strKey string The name of the enclosures field in $arrItem
    public static function addEnclosuresToTemplate($objTemplate, $arrItem, $strKey = 'enclosure')
    {
        $arrEnclosures = \StringUtil::deserialize($arrItem[$strKey]);
        if (!is_array($arrEnclosures) || empty($arrEnclosures)) {
            return;
        }
        $objFiles = \FilesModel::findMultipleByUuids($arrEnclosures);
        if ($objFiles === null) {
            return;
        }
        $file = \Input::get('file', true);
        // Send the file to the browser and do not send a 404 header (see #5178)
        if ($file != '') {
            while ($objFiles->next()) {
                if ($file == $objFiles->path) {
                    static::sendFileToBrowser($file);
                }
            }
            $objFiles->reset();
        }
        /** @var PageModel $objPage */
        global $objPage;
        $arrEnclosures = array();
        $allowedDownload = \StringUtil::trimsplit(',', strtolower(\Config::get('allowedDownload')));
        // Add download links
        while ($objFiles->next()) {
            if ($objFiles->type == 'file') {
                if (!in_array($objFiles->extension, $allowedDownload) || !is_file(TL_ROOT . '/' . $objFiles->path)) {
                    continue;
                }
                $objFile = new \File($objFiles->path);
                $strHref = \Environment::get('request');
                // Remove an existing file parameter (see #5683)
                if (preg_match('/(&(amp;)?|\\?)file=/', $strHref)) {
                    $strHref = preg_replace('/(&(amp;)?|\\?)file=[^&]+/', '', $strHref);
                }
                $strHref .= (strpos($strHref, '?') !== false ? '&' : '?') . 'file=' . \System::urlEncode($objFiles->path);
                $arrMeta = \Frontend::getMetaData($objFiles->meta, $objPage->language);
                if (empty($arrMeta) && $objPage->rootFallbackLanguage !== null) {
                    $arrMeta = \Frontend::getMetaData($objFiles->meta, $objPage->rootFallbackLanguage);
                }
                // Use the file name as title if none is given
                if ($arrMeta['title'] == '') {
                    $arrMeta['title'] = \StringUtil::specialchars($objFile->basename);
                }
                $arrEnclosures[] = array('id' => $objFiles->id, 'uuid' => $objFiles->uuid, 'name' => $objFile->basename, 'title' => \StringUtil::specialchars(sprintf($GLOBALS['TL_LANG']['MSC']['download'], $objFile->basename)), 'link' => $arrMeta['title'], 'caption' => $arrMeta['caption'], 'href' => $strHref, 'filesize' => static::getReadableSize($objFile->filesize), 'icon' => \Image::getPath($objFile->icon), 'mime' => $objFile->mime, 'meta' => $arrMeta, 'extension' => $objFile->extension, 'path' => $objFile->dirname, 'enclosure' => $objFiles->path);
            }
        }
        $objTemplate->enclosure = $arrEnclosures;
    }

Usage Example

 /**
  * Add an enclosure to a template.
  *
  * @param AddEnclosureToTemplateEvent $event The event.
  *
  * @return void
  */
 public function handleAddEnclosureToTemplate(AddEnclosureToTemplateEvent $event)
 {
     Controller::addEnclosuresToTemplate($event->getTemplate(), $event->getEnclosureData(), $event->getKey());
 }