From dfc6176f3410ad5d8601286b34fedc28813934df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Thu, 10 Oct 2019 16:49:43 +0200 Subject: [PATCH] Slide pages upwards to find default og:image, remove config --- .../MetaTag/OpenGraphMetaTagManager.php | 80 ++++++++++++------- .../TypoScript/constants.typoscript | 2 - .../Configuration/TypoScript/setup.typoscript | 1 - public/typo3conf/ext/ep_theme/ext_tables.php | 5 +- 4 files changed, 56 insertions(+), 32 deletions(-) diff --git a/public/typo3conf/ext/ep_theme/Classes/MetaTag/OpenGraphMetaTagManager.php b/public/typo3conf/ext/ep_theme/Classes/MetaTag/OpenGraphMetaTagManager.php index 3ae358d9..52bc792f 100644 --- a/public/typo3conf/ext/ep_theme/Classes/MetaTag/OpenGraphMetaTagManager.php +++ b/public/typo3conf/ext/ep_theme/Classes/MetaTag/OpenGraphMetaTagManager.php @@ -2,44 +2,68 @@ namespace EP\EpTheme\MetaTag; -use TYPO3\CMS\Core\Resource\ResourceFactory; +use TYPO3\CMS\Core\Resource\FileRepository; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; -use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; +use TYPO3\CMS\Core\Utility\RootlineUtility; use TYPO3\CMS\Extbase\Service\ImageService; class OpenGraphMetaTagManager extends \TYPO3\CMS\Seo\MetaTag\OpenGraphMetaTagManager { public function renderAllProperties(): string { - if (!array_key_exists('og:image', $this->properties)) { - /** @var ConfigurationManager $configurationManager */ - $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); - $settings = GeneralUtility::removeDotsFromTS( - $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT) + if (array_key_exists('og:image', $this->properties)) { + return parent::renderAllProperties(); + } + /** @var FileRepository $fileRepository */ + $fileRepository = GeneralUtility::makeInstance(FileRepository::class); + + $pageUid = $this->getPageWithImage(); + + if (null === $pageUid) { + return parent::renderAllProperties(); + } + + $images = $fileRepository->findByRelation('pages', 'og_image', $pageUid); + + /** @var ImageService $imageService */ + $imageService = GeneralUtility::makeInstance(ImageService::class); + + foreach ($images as $image) { + $processedImage = $imageService->applyProcessingInstructions( + $image, + [ + 'width' => '1200' + ] + ); + $this->addProperty( + 'og:image', + $imageService->getImageUri($processedImage, true), + [ + 'url' => $imageService->getImageUri($processedImage, true), + 'width' => $processedImage->getProperty('width'), + 'height' => $processedImage->getProperty('height'), + 'type' => $processedImage->getMimeType(), + ] ); - $imageSource = $settings['plugin']['tx_eptheme']['settings']['ogDefaultImage']; - $resourceFactory = ResourceFactory::getInstance(); - $image = $resourceFactory->getFileObjectFromCombinedIdentifier(sprintf('1:%s', $imageSource)); - - if (null !== $image) { - /** @var ImageService $imageService */ - $imageService = GeneralUtility::makeInstance(ImageService::class); - $processedImage = $imageService->applyProcessingInstructions($image, ['width' => '1200']); - - $this->addProperty( - 'og:image', - $imageService->getImageUri($processedImage, true), - [ - 'url' => $imageService->getImageUri($processedImage, true), - 'width' => $processedImage->getProperty('width'), - 'height' => $processedImage->getProperty('height'), - 'type' => $processedImage->getMimeType(), - ] - ); - } } return parent::renderAllProperties(); } + + /** + * @return null|int + */ + protected function getPageWithImage() + { + $rootLineUtility = new RootlineUtility($GLOBALS['TSFE']->id); + $rootLine = $rootLineUtility->get(); + + foreach ($rootLine as $page) { + if ($page['og_image'] > 0) { + return $page['uid']; + } + } + + return null; + } } \ No newline at end of file diff --git a/public/typo3conf/ext/ep_theme/Configuration/TypoScript/constants.typoscript b/public/typo3conf/ext/ep_theme/Configuration/TypoScript/constants.typoscript index c4463a82..114b9ba5 100644 --- a/public/typo3conf/ext/ep_theme/Configuration/TypoScript/constants.typoscript +++ b/public/typo3conf/ext/ep_theme/Configuration/TypoScript/constants.typoscript @@ -90,8 +90,6 @@ plugin.tx_eptheme { facebookViewContentProductUid = # cat=eptheme/100/150; type=string; label=Facebook ViewContent 'name internal' facebookViewContentNameInternal = - # cat=eptheme/100/160; type=string; label=OG default image - ogDefaultImage = user_upload/Open_Graph__Linkvorschau_/Home-Vorschaubild.jpg # cat=eptheme; type=string; label=Slider storage PID sliderStoragePid = diff --git a/public/typo3conf/ext/ep_theme/Configuration/TypoScript/setup.typoscript b/public/typo3conf/ext/ep_theme/Configuration/TypoScript/setup.typoscript index 7b3c7fe1..5d7df14c 100644 --- a/public/typo3conf/ext/ep_theme/Configuration/TypoScript/setup.typoscript +++ b/public/typo3conf/ext/ep_theme/Configuration/TypoScript/setup.typoscript @@ -94,7 +94,6 @@ plugin.tx_eptheme { productUid = {$plugin.tx_eptheme.settings.facebookViewContentProductUid} nameInternal = {$plugin.tx_eptheme.settings.facebookViewContentNameInternal} } - ogDefaultImage = {$plugin.tx_eptheme.settings.ogDefaultImage} domainsToTrack = bpn.ep-reisen.de,www.ski-boarderweek.de,www.ski-wochenenden.de,www.snowzone.net,www.unichamp.de } view { diff --git a/public/typo3conf/ext/ep_theme/ext_tables.php b/public/typo3conf/ext/ep_theme/ext_tables.php index cd2c7c64..c5761a01 100644 --- a/public/typo3conf/ext/ep_theme/ext_tables.php +++ b/public/typo3conf/ext/ep_theme/ext_tables.php @@ -3,6 +3,10 @@ defined('TYPO3_MODE') or die(); call_user_func(function () { + if ($GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'] !== '') { + $GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'] .= ','; + } + $GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'] .= 'og_image'; \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::allowTableOnStandardPages('tx_eptheme_domain_model_slide'); \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::allowTableOnStandardPages('tx_eptheme_domain_model_lineup'); @@ -17,5 +21,4 @@ call_user_func(function () { ['source' => 'EXT:ep_theme/Resources/Public/images/logo.svg'] ); } - });