‪TYPO3CMS  10.4
MetaTagGenerator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
27 
34 {
40  public function ‪generate(array $params)
41  {
42  ‪$metaTagManagerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
43 
44  if (!empty($params['page']['description'])) {
45  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('description');
46  $manager->addProperty('description', $params['page']['description']);
47  }
48 
49  if (!empty($params['page']['og_title'])) {
50  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('og:title');
51  $manager->addProperty('og:title', $params['page']['og_title']);
52  }
53 
54  if (!empty($params['page']['og_description'])) {
55  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('og:description');
56  $manager->addProperty('og:description', $params['page']['og_description']);
57  }
58 
59  if (!empty($params['page']['og_image'])) {
60  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
61  $fileCollector->addFilesFromRelation('pages', 'og_image', $params['page']);
62  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('og:image');
63 
64  $ogImages = $this->‪generateSocialImages($fileCollector->getFiles());
65  foreach ($ogImages as $ogImage) {
66  $subProperties = [];
67  $subProperties['url'] = $ogImage['url'];
68  $subProperties['width'] = $ogImage['width'];
69  $subProperties['height'] = $ogImage['height'];
70 
71  if (!empty($ogImage['alternative'])) {
72  $subProperties['alt'] = $ogImage['alternative'];
73  }
74 
75  $manager->addProperty(
76  'og:image',
77  $ogImage['url'],
78  $subProperties
79  );
80  }
81  }
82 
83  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:card');
84  $manager->addProperty('twitter:card', $params['page']['twitter_card'] ?: 'summary');
85 
86  if (!empty($params['page']['twitter_title'])) {
87  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:title');
88  $manager->addProperty('twitter:title', $params['page']['twitter_title']);
89  }
90 
91  if (!empty($params['page']['twitter_description'])) {
92  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:description');
93  $manager->addProperty('twitter:description', $params['page']['twitter_description']);
94  }
95 
96  if (!empty($params['page']['twitter_image'])) {
97  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
98  $fileCollector->addFilesFromRelation('pages', 'twitter_image', $params['page']);
99  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:image');
100 
101  $twitterImages = $this->‪generateSocialImages($fileCollector->getFiles());
102  foreach ($twitterImages as $twitterImage) {
103  $subProperties = [];
104 
105  if (!empty($twitterImage['alternative'])) {
106  $subProperties['alt'] = $twitterImage['alternative'];
107  }
108 
109  $manager->addProperty(
110  'twitter:image',
111  $twitterImage['url'],
112  $subProperties
113  );
114  }
115  }
116 
117  $noIndex = ((bool)$params['page']['no_index']) ? 'noindex' : 'index';
118  $noFollow = ((bool)$params['page']['no_follow']) ? 'nofollow' : 'follow';
119 
120  if ($noIndex === 'noindex' || $noFollow === 'nofollow') {
121  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('robots');
122  $manager->addProperty('robots', implode(',', [$noIndex, $noFollow]));
123  }
124  }
125 
130  protected function ‪generateSocialImages(array $fileReferences): array
131  {
132  $imageService = GeneralUtility::makeInstance(ImageService::class);
133 
134  $socialImages = [];
135 
137  foreach ($fileReferences as $file) {
138  $arguments = $file->getProperties();
139  $cropVariantCollection = ‪CropVariantCollection::create((string)$arguments['crop']);
140  $cropVariant = $arguments['cropVariant'] ?: 'social';
141  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
142  $crop = $cropArea->makeAbsoluteBasedOnFile($file);
143 
144  $processingConfiguration = [
145  'crop' => $crop,
146  'maxWidth' => 2000
147  ];
148 
149  $processedImage = $file->getOriginalFile()->process(
151  $processingConfiguration
152  );
153 
154  $imageUri = $imageService->getImageUri($processedImage, true);
155 
156  $socialImages[] = [
157  'url' => $imageUri,
158  'width' => floor($processedImage->getProperty('width')),
159  'height' => floor($processedImage->getProperty('height')),
160  'alternative' => $arguments['alternative'],
161  ];
162  }
163 
164  return $socialImages;
165  }
166 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:58
‪TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry
Definition: MetaTagManagerRegistry.php:28
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator
Definition: MetaTagGenerator.php:34
‪TYPO3\CMS\Seo\MetaTag
Definition: MetaTagGenerator.php:18
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:41
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:35
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\generateSocialImages
‪array generateSocialImages(array $fileReferences)
Definition: MetaTagGenerator.php:130
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:42
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\generate
‪generate(array $params)
Definition: MetaTagGenerator.php:40
‪$metaTagManagerRegistry
‪$metaTagManagerRegistry
Definition: ext_localconf.php:57