|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +namespace SIMONKOEHLER\ContentBlocksBootstrap\Preview; |
| 4 | + |
| 5 | +use TYPO3\CMS\Backend\Routing\UriBuilder; |
| 6 | +use TYPO3\CMS\Backend\Utility\BackendUtility; |
| 7 | +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; |
| 8 | +use \TYPO3\CMS\Backend\Preview\PreviewRendererInterface; |
| 9 | +use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem; |
| 10 | +use TYPO3\CMS\Core\Domain\Record; |
| 11 | +use TYPO3\CMS\Core\Domain\RecordFactory; |
| 12 | +use TYPO3\CMS\Core\Localization\LanguageService; |
| 13 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 14 | +use TYPO3\CMS\Core\Utility\DebugUtility; |
| 15 | +use TYPO3\CMS\Core\Type\Bitmask\Permission; |
| 16 | +use TYPO3\CMS\Core\Domain\Repository\PageRepository; |
| 17 | +use TYPO3\CMS\Core\Resource\FileRepository; |
| 18 | +use TYPO3\CMS\Extbase\Service\ImageService; |
| 19 | + |
| 20 | +class CardsOfSubpages implements PreviewRendererInterface{ |
| 21 | + |
| 22 | + public function renderPageModulePreviewHeader(GridColumnItem $item): string |
| 23 | + { |
| 24 | + $record = $item->getRecord(); |
| 25 | + // Render a custom header for the content element |
| 26 | + return '<strong>' . htmlspecialchars($item->getRecord()['header']) . '</strong>'; |
| 27 | + } |
| 28 | + |
| 29 | + public function renderPageModulePreviewContent(GridColumnItem $item): string |
| 30 | + { |
| 31 | + $pageRepository = GeneralUtility::makeInstance(PageRepository::class); |
| 32 | + $fileRepository = GeneralUtility::makeInstance(FileRepository::class); |
| 33 | + $imageService = GeneralUtility::makeInstance(ImageService::class); |
| 34 | + |
| 35 | + $languageService = $this->getLanguageService(); |
| 36 | + $table = $item->getTable(); |
| 37 | + $record = $item->getRecord(); |
| 38 | + $recordObj = GeneralUtility::makeInstance(RecordFactory::class)->createResolvedRecordFromDatabaseRow($table, $record); |
| 39 | + $recordType = $recordObj->getRecordType(); |
| 40 | + $output = ''; |
| 41 | + $output .= $this->linkEditContent($this->generateListForMenuContentTypes($record, $recordType), $record); |
| 42 | + $output .= '<div class="row">'; |
| 43 | + |
| 44 | + $subpages = $pageRepository->getMenu(explode(',', $record['pages'])[0]); |
| 45 | + |
| 46 | + foreach ($subpages as $page) { |
| 47 | + $media = $fileRepository->findByRelation('pages', 'media', $page['uid']); |
| 48 | + $output .= '<div class="col"><div class="card">'; |
| 49 | + //DebugUtility::debug($media, 'recordObj'); |
| 50 | + if($media){ |
| 51 | + foreach ($media as $fileReference) { |
| 52 | + $publicUrl = $fileReference->getPublicUrl(); |
| 53 | + try { |
| 54 | + // Create a processed image with a width of 300px |
| 55 | + $processedImage = $imageService->applyProcessingInstructions( |
| 56 | + $fileReference, |
| 57 | + ['width' => '200'] // 300 pixels, cropped |
| 58 | + ); |
| 59 | + $thumbnailUrl = $imageService->getImageUri($processedImage); |
| 60 | + |
| 61 | + $output .= '<img class="card-img-top" src="' . htmlspecialchars($thumbnailUrl) . '" alt="">'; |
| 62 | + } catch (\Exception $e) { |
| 63 | + // Handle broken file reference or processing error |
| 64 | + echo 'Error loading thumbnail: ' . $e->getMessage(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + $output .= '<div class="card-body">'; |
| 69 | + $output .= '<h6 class="card-title fw-bold">' . $page['title'] . '</h6>'; |
| 70 | + $output .= '<div class="card-text">' . $page['abstract'] . '</div>'; |
| 71 | + $output .= '</div>'; |
| 72 | + $output .= '</div></div>'; |
| 73 | + } |
| 74 | + |
| 75 | + $output .= '</div>'; |
| 76 | + |
| 77 | + //DebugUtility::debug($subpages, 'recordObj'); |
| 78 | + return $output; |
| 79 | + } |
| 80 | + |
| 81 | + public function renderPageModulePreviewFooter(GridColumnItem $item): string |
| 82 | + { |
| 83 | + // Optional footer content |
| 84 | + return ''; |
| 85 | + } |
| 86 | + |
| 87 | + public function wrapPageModulePreview(string $previewHeader, string $previewContent, GridColumnItem $item): string |
| 88 | + { |
| 89 | + // Combine header, content, and footer into a full preview box |
| 90 | + return sprintf( |
| 91 | + '<div class="element-preview"> |
| 92 | + <div class="element-preview-header mb-2">%s</div> |
| 93 | + <div class="element-preview-content">%s</div> |
| 94 | + </div>', |
| 95 | + $previewHeader, |
| 96 | + $previewContent |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Generates a list of selected pages or categories for the menu content types |
| 102 | + * |
| 103 | + * @param array $record row from pages |
| 104 | + */ |
| 105 | + protected function generateListForMenuContentTypes(array $record, string $contentType): string |
| 106 | + { |
| 107 | + $table = 'pages'; |
| 108 | + $field = 'pages'; |
| 109 | + |
| 110 | + if (trim($record[$field] ?? '') === '') { |
| 111 | + return ''; |
| 112 | + } |
| 113 | + $content = ''; |
| 114 | + $uidList = explode(',', $record[$field]); |
| 115 | + foreach ($uidList as $uid) { |
| 116 | + $uid = (int)$uid; |
| 117 | + $pageRecord = BackendUtility::getRecord($table, $uid, 'title'); |
| 118 | + if ($pageRecord) { |
| 119 | + $content .= '<li class="list-group-item">' . htmlspecialchars($pageRecord['title']) . ' <span class="text-body-secondary">[' . $uid . ']</span></li>'; |
| 120 | + } |
| 121 | + } |
| 122 | + return $content ? '<ul class="list-group mb-3">' . $content . '</ul>' : ''; |
| 123 | + } |
| 124 | + |
| 125 | + protected function linkEditContent(string $linkText, array $row, string $table = 'tt_content'): string |
| 126 | + { |
| 127 | + if (empty($linkText)) { |
| 128 | + return $linkText; |
| 129 | + } |
| 130 | + |
| 131 | + $backendUser = $this->getBackendUser(); |
| 132 | + if ($backendUser->check('tables_modify', $table) |
| 133 | + && $backendUser->recordEditAccessInternals($table, $row) |
| 134 | + && (new Permission($backendUser->calcPerms(BackendUtility::getRecord('pages', $row['pid']) ?? [])))->editContentPermissionIsGranted() |
| 135 | + ) { |
| 136 | + $urlParameters = [ |
| 137 | + 'edit' => [ |
| 138 | + $table => [ |
| 139 | + $row['uid'] => 'edit', |
| 140 | + ], |
| 141 | + ], |
| 142 | + 'returnUrl' => $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri() . '#element-' . $table . '-' . $row['uid'], |
| 143 | + ]; |
| 144 | + $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
| 145 | + $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters); |
| 146 | + return '<a href="' . htmlspecialchars($url) . '" title="' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:edit')) . '">' . $linkText . '</a>'; |
| 147 | + } |
| 148 | + return $linkText; |
| 149 | + } |
| 150 | + |
| 151 | + protected function getBackendUser(): BackendUserAuthentication |
| 152 | + { |
| 153 | + return $GLOBALS['BE_USER']; |
| 154 | + } |
| 155 | + |
| 156 | + protected function getLanguageService(): LanguageService |
| 157 | + { |
| 158 | + return $GLOBALS['LANG']; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments