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