Skip to content

Instantly share code, notes, and snippets.

@cedricziel
Forked from spoonerWeb/FacebookVideoHelper.php
Created October 7, 2016 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cedricziel/a2465a6dc3fb6959a60931b5f8bca4ec to your computer and use it in GitHub Desktop.
Save cedricziel/a2465a6dc3fb6959a60931b5f8bca4ec to your computer and use it in GitHub Desktop.
Facebook video integration in TYPO3 (needs some love for FE rendering)
<?php
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['onlineMediaHelpers']['facebook'] = \Vendor\ExtensionKey\Helpers\FacebookVideoHelper::class;
$rendererRegistry = \TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::getInstance();
$rendererRegistry->registerRendererClass(
\Vendor\ExtensionKey\Rendering\FacebookVideoRenderer::class
);
// Register an custom mime-type for your videos
$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType']['facebook'] = 'video/facebook';
// Register your custom file extension as allowed media file
$GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'] .= ',facebook';
<?php
namespace Vendor\ExtensionKey\Helpers;
/**
* 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\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class FacebookVideoHelper
*
* @author Thomas Löffler <loeffler@spooner-web.de>
*/
class FacebookVideoHelper extends AbstractOEmbedHelper {
/**
* @param string $url
* @param \TYPO3\CMS\Core\Resource\Folder $targetFolder
* @return File
*/
public function transformUrlToFile($url, \TYPO3\CMS\Core\Resource\Folder $targetFolder)
{
$videoId = null;
// Try to get the YouTube code from given url.
// These formats are supported with and without http(s)://
// - facebook.com/<site>/videos/<code> # Share URL
if (preg_match('/facebook\.com\/.*\/videos\/*([0-9]+)/i', $url, $matches)) {
$videoId = $matches[1];
}
if (empty($videoId)) {
return null;
}
return $this->transformMediaIdToFile($videoId, $targetFolder, $this->extension);
}
/**
* Transform mediaId to File
*
* @param string $mediaId
* @param Folder $targetFolder
* @param string $fileExtension
* @return File
*/
protected function transformMediaIdToFile($mediaId, Folder $targetFolder, $fileExtension)
{
$file = $this->findExistingFileByOnlineMediaId($mediaId, $targetFolder, $fileExtension);
// no existing file create new
if ($file === null) {
$oEmbed = $this->getOEmbedData($mediaId);
if (!empty($oEmbed) && isset($oEmbed['name'])) {
$fileName = $oEmbed['name'] . '.' . $fileExtension;
} else {
$fileName = $mediaId . '.' . $fileExtension;
}
$file = $this->createNewFile($targetFolder, $fileName, $mediaId);
}
return $file;
}
/**
* Get meta data for OnlineMedia item
* Using the meta data from oEmbed
*
* @param File $file
* @return array with metadata
*/
public function getMetaData(File $file)
{
$metadata = [];
$oEmbed = $this->getOEmbedData($this->getOnlineMediaId($file));
if ($oEmbed) {
$metadata['width'] = (int)$oEmbed['width'];
$metadata['height'] = (int)$oEmbed['height'];
if (empty($file->getProperty('title')) && isset($oEmbed['name'])) {
$metadata['title'] = strip_tags($oEmbed['name']);
}
if (empty($file->getProperty('description')) && isset($oEmbed['description'])) {
$metadata['description'] = strip_tags($oEmbed['description']);
}
$metadata['author'] = $oEmbed['from']['name'];
}
return $metadata;
}
/**
* @param \TYPO3\CMS\Core\Resource\File $file
* @param bool $relativeToCurrentScript
* @return string
*/
public function getPublicUrl(\TYPO3\CMS\Core\Resource\File $file, $relativeToCurrentScript = false)
{
$videoId = $this->getOnlineMediaId($file);
$videoLink = sprintf('https://www.facebook.com/video.php?v=%s', $videoId);
return 'https://www.facebook.com/v2.5/plugins/video.php?href=' . urlencode($videoLink);
}
/**
* @param \TYPO3\CMS\Core\Resource\File $file
* @return string
*/
public function getPreviewImage(\TYPO3\CMS\Core\Resource\File $file)
{
$videoId = $this->getOnlineMediaId($file);
$temporaryFileName = $this->getTempFolderPath() . 'facebook_' . md5($videoId) . '.jpg';
if (!file_exists($temporaryFileName)) {
$videoInformation = $this->getOEmbedData($videoId);
if (!empty($videoInformation['format'])) {
$biggestFormat = array_pop($videoInformation['format']);
$previewImage = GeneralUtility::getUrl($biggestFormat['picture']);
if ($previewImage !== false) {
file_put_contents($temporaryFileName, $previewImage);
GeneralUtility::fixPermissions($temporaryFileName);
}
}
}
return $temporaryFileName;
}
/**
* @param string $mediaId
* @param string $format
* @return string
*/
public function getOEmbedUrl($mediaId, $format = 'json')
{
return 'https://graph.facebook.com/' . $mediaId . '/';
}
}
<?php
namespace Vendor\ExtensionKey\Rendering;
/**
* 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\Resource\File;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
/**
* Class FacebookVideoRenderer
*
* @author Thomas Löffler <loeffler@spooner-web.de>
*/
class FacebookVideoRenderer implements FileRendererInterface {
/**
* @var OnlineMediaHelperInterface
*/
protected $onlineMediaHelper;
/**
* @return integer
*/
public function getPriority()
{
return 1;
}
/**
* @param \TYPO3\CMS\Core\Resource\FileInterface $file
* @return boolean
*/
public function canRender(\TYPO3\CMS\Core\Resource\FileInterface $file)
{
return ($file->getMimeType() === 'video/facebook' || $file->getExtension() === 'facebook') && $this->getOnlineMediaHelper($file) !== false;
}
/**
* Get online media helper
*
* @param FileInterface $file
* @return bool|OnlineMediaHelperInterface
*/
protected function getOnlineMediaHelper(FileInterface $file)
{
if ($this->onlineMediaHelper === null) {
$orgFile = $file;
if ($orgFile instanceof FileReference) {
$orgFile = $orgFile->getOriginalFile();
}
if ($orgFile instanceof File) {
$this->onlineMediaHelper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($orgFile);
} else {
$this->onlineMediaHelper = false;
}
}
return $this->onlineMediaHelper;
}
/**
* @param \TYPO3\CMS\Core\Resource\FileInterface $file
* @param int|string $width
* @param int|string $height
* @param array $options
* @param bool $usedPathsRelativeToCurrentScript
* @return string
*/
public function render(\TYPO3\CMS\Core\Resource\FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
{
// Due to the rendering via fluid_styled_content, text/media element and click-enlarge the rendering happens not here
return '';
}
}
lib.fluidContent.settings.media.videopopup {
typolink {
parameter {
cObject = CASE
cObject {
key.data = file:current:extension
facebook = TEXT
facebook.data = file:current:contents
facebook.wrap = https://www.facebook.com/v2.5/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D|
}
}
ATagParams = data-toggle="lightbox" data-title="{file:current:title}" data-footer="{file:current:description}"
ATagParams.insertData = 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment