Slide pages upwards to find default og:image, remove config
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user