sgalinski/sg-youtube

一种将YouTube视频、播放列表或频道轻松嵌入TYPO3页面的解决方案。

安装: 11

依赖项: 0

建议者: 0

安全: 0

类型:typo3-cms-extension

7.2.0 2024-06-06 13:00 UTC

README

许可证: GNU GPL,版本2

仓库: https://gitlab.sgalinski.de/typo3/sg_youtube

请在此处报告错误:https://gitlab.sgalinski.de/typo3/sg_youtube

安装/集成

首先在扩展管理器中安装扩展并激活。

TypoScript集成

  • 在主题中的Configuration/TypoScript/setup.typoscript和constants.typoscript中包含TypoScript。
  • 添加您的YouTube API密钥
plugin.tx_sgyoutube {
	settings {
		# cat=plugin.tx_sgyoutube/file; type=string; label=YouTube API Key
		apiKey = <your-api-key>
	}
}

注册每天的免费配额超过10,000个

不是每个API调用1个配额。每个API都有自己的费用,如下面的链接中所示。

目前,在版本3.2.1中,我们使用以下API

  • "search/list"用于频道视频
  • "playlistItems/list"用于特定播放列表中的视频
  • "videos/list"用于获取每个视频的详细信息以及所需的本地化

目前,为了渲染带有视频详细信息和翻译的频道最新视频,最大配额成本为"102"。

配额计算器

YouTube API服务 - 审计和配额扩展表单

缓存行为

由于配额费用,我们为每天的调用实现了缓存。API的响应将被保存并使用24小时。通常网站缓存会这样做,但可能会连续多次清除缓存,或者插件在未缓存页面上。使用TYPO3注册表作为缓存。清理是动态处理的。

如果URL中添加了?disableYoutubeCache=1参数,此缓存也将被忽略。

如果配额限制仍然达到,可能的解决方案

您可以为特定站点使用不同的API密钥。您可以在"TypoScript集成"主题中实现TypoScript页面uid检查,然后仅更改密钥。

修改JavaScript/CSS

我们附带源文件和已压缩的资产扩展。默认情况下,我们在布局中加载已压缩的资产,以便扩展即插即用。如果您想更改此行为,可以执行以下操作

  • 在TypoScript local/project_theme/Configuration/TypoScript/Extensions/SgYouTube/Constants.typoscript中覆盖布局路径
plugin.tx_sgyoutube {
	settings {
		apiKey = MY_KEY
	}

	view {
		layoutRootPath = EXT:project_theme/Resources/Private/Layouts/SgYouTube/
	}
}

  • 创建一个新的布局文件,省略您想更改的资产(例如,不加载CSS)
<div class="tx-sg-youtube">
	<f:asset.script identifier="sgVideoJs" src="EXT:sg_youtube/Resources/Public/JavaScript/Dist/main.bundled.min.js" />
	<f:render section="main"/>
</div>

  • 在您的相应资产管道中导入CSS或JavaScript源文件,并将其外部添加。

使用SGC编译CSS/JS资产

  • 通过composer安装sgalinski/sgc-core库
  • 在.sgc-config.json中添加sg-youtube扩展路径
  • 从Resources/Private/Templates/Youtube/Index.html中删除加载编译的CSS/JS资产的代码
  • 在相应的main.scss和main.js中导入scss和js模块文件
  • 在main.js中初始化javascript模块: new SgVideoLightbox(); SgVideo.initDefault();

仅使用SGC编译SASS

示例

npm install -g sass npx sass ./Resources/Public/Sass/Bootstrap5/main.scss ./Resources/Public/StyleSheets/Bootstrap5/main.min.css --style compressed --no-source-map

使用Bootstrap 5模板

如果您想使用Bootstrap 5模板,您必须首先在主题中安装Bootstrap 5以使用其样式和JavaScript。之后,只需将plugin.tx_project_theme.config.bootstrapVersion TypoScript设置变量设置为5。

使用事件自定义TYPO3中的YouTube API结果

本文档解释了如何在TYPO3扩展中使用自定义事件来操纵YouTube API调用的结果。通过使用这些事件,您可以修改API参数、筛选结果以及进一步自定义YouTube返回的JSON数据。

可用事件

以下事件在YouTube视频渲染过程中被触发

  1. BeforeYoutubeCallEvent
  2. AfterYoutubeCallEvent
  3. AfterFilterVideosEvent
  4. AfterMapCustomThumbnailsEvent

事件监听器

1. BeforeYoutubeCallEvent

描述:在发起YouTube API调用之前触发此事件,允许您修改API参数。

示例用例:更改API密钥或操作其他参数
<?php

namespace Vendor\Extension\EventListener;

use SGalinski\SgYoutube\Event\BeforeVimeoCallEvent;

class BeforeYoutubeCallEventListener
{
    public function __invoke(BeforeVimeoCallEvent $event): void
    {
        // Change the API key
        $event->setApiKey('your-new-api-key');
        // Extend the max results limit by 10 videos
        $event->setMaxResultsWithFilters($event->getMaxResultsWithFilters() + 10);
    }
}

2. AfterYoutubeCallEvent

描述:向JSON数组添加自定义数据

示例用例:更改API密钥或操作其他参数
<?php
namespace SGalinski\SgYoutube\EventListeners;

use SGalinski\SgYoutube\Event\AfterVimeoCallEvent;

class AfterYoutubeCallEventListener
{
	public function __invoke(AfterVimeoCallEvent $event): void
	{
		$jsonArray = $event->getJsonArray();
		// Add custom data
		$jsonArray['customData'] = 'This is some custom data';
		$event->setJsonArray($jsonArray);
	}
}

3. AfterFilterVideosEvent

描述:在视频被筛选后触发此事件,允许您进一步操作筛选结果。

示例用例:移除我们最初在第一个事件中添加的10个视频
<?php
namespace SGalinski\SgYoutube\EventListeners;

use SGalinski\SgYoutube\Event\AfterFilterVideosEvent;

class AfterFilterVideosEventListener
{
	public function __invoke(AfterFilterVideosEvent $event): void
	{
		// Modify the jsonArray if needed
		$jsonArray = $event->getJsonArray();
		// Add some custom processing here
		// For example let's remove the extra 10 videos that we added in the other event
		if (count($jsonArray['items']) > 10) {
			array_splice($jsonArray['items'], 0, 10);
		}
		$event->setJsonArray($jsonArray);
	}
}

4. AfterMapCustomThumbnailsEvent

描述:在自定义缩略图映射后触发此事件,允许您在渲染前最后一次修改JSON数组。

示例用例:为列表中的所有视频使用自定义缩略图
<?php
namespace SGalinski\SgYoutube\EventListeners;

use SGalinski\SgYoutube\Event\AfterMapCustomThumbnailsEvent;

class AfterMapCustomThumbnailsEventListener
{
    public function __invoke(AfterMapCustomThumbnailsEvent $event): void
    {
        $jsonArray = $event->getJsonArray();
        // Add a custom thumbnail URL
        foreach ($jsonArray['items'] as &$item) {
            $item['customThumbnail'] = 'https://example.com/custom-thumbnail.jpg';
        }
        $event->setJsonArray($jsonArray);
    }
}

要启用这些事件,只需在您的Services.php中注册它们,如下所示(如果您喜欢,可以使用yaml)

$services->set(BeforeYoutubeCallEventListener::class)
    ->tag('event.listener', ['event' => BeforeYoutubeCallEvent::class]);
$services->set(AfterYoutubeCallEventListener::class)
    ->tag('event.listener', ['event' => AfterYoutubeCallEvent::class]);
$services->set(AfterFilterVideosEventListener::class)
    ->tag('event.listener', ['event' => AfterFilterVideosEvent::class]);
$services->set(AfterMapCustomThumbnailsEventListener::class)
    ->tag('event.listener', ['event' => AfterMapCustomThumbnailsEvent::class]);