Commit 63d604b5 authored by Tomas Norre Mikkelsen's avatar Tomas Norre Mikkelsen Committed by Thomas Löffler
Browse files

[FEATURE] Add og:image to extension singleview for better sharing

parent 77a93211
......@@ -18,8 +18,11 @@ use T3o\Ter\Api\ApiUser;
use T3o\Ter\Api\ExtensionKey;
use T3o\Ter\Api\ExtensionVersion;
use T3o\TerFe2\Provider\FileProvider;
use T3o\TerFe2\Utility\ExtensionUtility;
use T3o\TerFe2\Validation\Validator\ComposerNameValidator;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Routing\SiteMatcher;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -27,6 +30,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
*/
class ExtensionController extends \T3o\TerFe2\Controller\AbstractController
{
const ROOT_PAGE_ID = 1;
/**
* @var \T3o\TerFe2\Domain\Repository\ExtensionRepository
*/
......@@ -210,6 +215,8 @@ class ExtensionController extends \T3o\TerFe2\Controller\AbstractController
$metaTagManager->addProperty('og:title', $extension->getLastVersion()->getTitle() . ' (' . $extension->getExtKey() . ')');
$metaTagManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry::class)->getManagerForProperty('og:description');
$metaTagManager->addProperty('og:description', $extension->getLastVersion()->getDescription());
$metaTagManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry::class)->getManagerForProperty('og:image');
$metaTagManager->addProperty('og:image', $this->getBaseUrl() . ExtensionUtility::getExtensionIcon($extension->getExtKey(), $extension->getLastVersion()->getVersionString()));
$GLOBALS['TSFE']->page['title'] = 'TYPO3 Extension \'' . $extension->getLastVersion()->getTitle() . '\' (' . $extension->getExtKey() . ')';
}
......@@ -627,4 +634,17 @@ class ExtensionController extends \T3o\TerFe2\Controller\AbstractController
$GLOBALS['TSFE']->register['SYS_LASTCHANGED'] = $dateTime;
}
}
/**
* @return \Psr\Http\Message\UriInterface|string
*/
private function getBaseUrl()
{
$site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId(self::ROOT_PAGE_ID);
$url = '';
if ($site instanceof Site) {
$url = $site->getRouter()->generateUri(self::ROOT_PAGE_ID);
}
return $url;
}
}
......@@ -16,8 +16,8 @@ namespace T3o\TerFe2\Solr\IndexQueue;
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use T3o\TerFe2\Utility\ExtensionUtility;
use T3o\TerFe2\Utility\VersionUtility;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
......@@ -160,7 +160,7 @@ class TerIndexer extends \ApacheSolrForTypo3\Solr\IndexQueue\Indexer
$document->setField('typo3supportelts_stringM', $supportELTSVersions);
$document->setField('typo3supportold_stringM', $supportOlderVersions);
$extensionIcon = $this->getExtensionIcon($itemRecord['ext_key'], $extension->getLastVersion()->getVersionString());
$extensionIcon = ExtensionUtility::getExtensionIcon($itemRecord['ext_key'], $extension->getLastVersion()->getVersionString());
$document->setField('extensionIcon_stringS', $extensionIcon);
// add extension likes count
......@@ -224,56 +224,4 @@ class TerIndexer extends \ApacheSolrForTypo3\Solr\IndexQueue\Indexer
return $versions;
}
/**
* get the relative path for the extension icon
* could be png or gif
*
* @param $extensionKey
* @param $extensionVersion
* @return string
*/
protected function getExtensionIcon($extensionKey, $extensionVersion)
{
$fileTypesToCheck = [
'svg',
'png',
'gif'
];
$iconPath = 'fileadmin/ter/';
$icon = '';
foreach ($fileTypesToCheck as $fileType) {
$iconFile = $this->generateIconFileName(
$extensionKey,
$extensionVersion,
$fileType
);
if (file_exists(Environment::getPublicPath() . '/' . $iconPath . $iconFile)) {
$icon = \T3o\TerFe2\Utility\FileUtility::getRelativeUrlFromAbsolutePath($iconPath . $iconFile);
break;
}
}
return $icon;
}
/**
* generate the path to an extension icon for a specific version
*
* @param $extension
* @param $version
* @param $fileType
* @return string
*/
protected function generateIconFileName($extension, $version, $fileType)
{
if (empty($extension) || empty($version) || empty($fileType)) {
return '';
}
$extension = strtolower($extension);
$fileType = strtolower(trim($fileType, '. '));
return $extension[0] . '/' . $extension[1] . '/' . $extension . '_' . $version . '.' . $fileType;
}
}
<?php
declare(strict_types = 1);
namespace T3o\TerFe2\Utility;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Core\Environment;
class ExtensionUtility
{
public static function getExtensionIcon(string $extensionKey, string $extensionVersion): string
{
$fileTypesToCheck = [
'svg',
'png',
'gif'
];
$iconPath = 'fileadmin/ter/';
$icon = '';
foreach ($fileTypesToCheck as $fileType) {
$iconFile = self::generateIconFileName(
$extensionKey,
$extensionVersion,
$fileType
);
if (file_exists(Environment::getPublicPath() . '/' . $iconPath . $iconFile)) {
$icon = \T3o\TerFe2\Utility\FileUtility::getRelativeUrlFromAbsolutePath($iconPath . $iconFile);
break;
}
}
return $icon;
}
/**
* generate the path to an extension icon for a specific version
*
* @return string
*/
private static function generateIconFileName(string $extension, string $version, string $fileType)
{
if (empty($extension) || empty($version) || empty($fileType)) {
return '';
}
$extension = strtolower($extension);
$fileType = strtolower(trim($fileType, '. '));
return $extension[0] . '/' . $extension[1] . '/' . $extension . '_' . $version . '.' . $fileType;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment