‪TYPO3CMS  11.5
PreviewUriBuilder.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 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Core\Page\PageRenderer;
25 
32 {
33  public const ‪OPTION_SWITCH_FOCUS = 'switchFocus';
34  public const ‪OPTION_WINDOW_NAME = 'windowName';
35  public const ‪OPTION_WINDOW_FEATURES = 'windowFeatures';
36  public const ‪OPTION_WINDOW_SCOPE = 'windowScope';
37 
38  public const ‪OPTION_WINDOW_SCOPE_LOCAL = 'local';
39  public const ‪OPTION_WINDOW_SCOPE_GLOBAL = 'global';
40 
44  protected ‪$pageId;
45 
49  protected ‪$alternativeUri;
50 
54  protected ‪$rootLine;
55 
59  protected ‪$section;
60 
65 
70  protected ‪$backPath;
71 
75  protected ‪$moduleLoading = true;
76 
82  public static function ‪create(int ‪$pageId, ?string ‪$alternativeUri = null): self
83  {
84  return GeneralUtility::makeInstance(static::class, ‪$pageId, ‪$alternativeUri);
85  }
86 
91  public function ‪__construct(int ‪$pageId, ?string ‪$alternativeUri = null)
92  {
93  $this->pageId = ‪$pageId;
94  $this->alternativeUri = ‪$alternativeUri;
95  }
96 
101  public function ‪withModuleLoading(bool ‪$moduleLoading): self
102  {
103  if ($this->moduleLoading === ‪$moduleLoading) {
104  return $this;
105  }
106  $target = clone $this;
107  $target->moduleLoading = ‪$moduleLoading;
108  return $target;
109  }
110 
115  public function ‪withRootLine(array ‪$rootLine): self
116  {
117  if ($this->rootLine === ‪$rootLine) {
118  return $this;
119  }
120  $target = clone $this;
121  $target->rootLine = ‪$rootLine;
122  return $this;
123  }
124 
129  public function ‪withSection(string ‪$section): self
130  {
131  if ($this->section === ‪$section) {
132  return $this;
133  }
134  $target = clone $this;
135  $target->section = ‪$section;
136  return $target;
137  }
138 
144  {
145  if ($this->additionalQueryParameters === ‪$additionalQueryParameters) {
146  return $this;
147  }
148  $target = clone $this;
149  $target->additionalQueryParameters = ‪$additionalQueryParameters;
150  return $target;
151  }
152 
159  public function ‪buildUri(?array $options = null): ?‪Uri
160  {
161  try {
162  $options = $this->‪enrichOptions($options);
163  $switchFocus = $options[‪self::OPTION_SWITCH_FOCUS] ?? true;
164  $uriString = BackendUtility::getPreviewUrl(
165  $this->pageId,
166  $this->backPath ?? '',
167  $this->rootLine,
168  $this->section ?? '',
169  $this->alternativeUri ?? '',
170  $this->additionalQueryParameters ?? '',
171  $switchFocus
172  );
173  return GeneralUtility::makeInstance(Uri::class, $uriString);
174  } catch (‪UnableToLinkToPageException $exception) {
175  return null;
176  }
177  }
178 
186  public function ‪buildDispatcherDataAttributes(?array $options = null): ?array
187  {
188  if (null === ($attributes = $this->‪buildAttributes($options))) {
189  return null;
190  }
191  $this->‪loadActionDispatcher();
192  return $this->‪prefixAttributeNames('dispatch-', $attributes);
193  }
194 
202  public function ‪buildDispatcherAttributes(?array $options = null): ?array
203  {
204  if (null === ($attributes = $this->‪buildAttributes($options))) {
205  return null;
206  }
207  $this->‪loadActionDispatcher();
208  return $this->‪prefixAttributeNames('data-dispatch-', $attributes);
209  }
210 
217  public function ‪serializeDispatcherAttributes(?array $options = null): ?string
218  {
219  if (null === ($attributes = $this->‪buildDispatcherAttributes($options))) {
220  return null;
221  }
222  return ' ' . GeneralUtility::implodeAttributes($attributes, true);
223  }
224 
232  public function ‪buildImmediateActionElement(?array $options = null): ?string
233  {
234  if (null === ($attributes = $this->‪buildAttributes($options))) {
235  return null;
236  }
238  return sprintf(
239  // `<typo3-immediate-action action="TYPO3.WindowManager.localOpen" args="[...]">`
240  '<typo3-immediate-action %s></typo3-immediate-action>',
241  GeneralUtility::implodeAttributes($attributes, true)
242  );
243  }
244 
245  protected function ‪buildAttributes(?array $options = null): ?array
246  {
247  $options = $this->‪enrichOptions($options);
248  if (null === ($uri = $this->‪buildUri($options))) {
249  return null;
250  }
251  ‪$args = [
252  // target URI
253  (string)$uri,
254  // whether to switch focus to that window
255  $options[self::OPTION_SWITCH_FOCUS],
256  // name of the window instance for JavaScript references
257  $options[self::OPTION_WINDOW_NAME],
258  ];
259  if (isset($options[self::OPTION_WINDOW_FEATURES])) {
260  // optional window features (e.g. 'width=500,height=300')
262  }
263  return [
264  'action' => $options[‪self::OPTION_WINDOW_SCOPE] === self::OPTION_WINDOW_SCOPE_GLOBAL
265  ? 'TYPO3.WindowManager.globalOpen'
266  : 'TYPO3.WindowManager.localOpen',
267  'args' => json_encode(‪$args),
268  ];
269  }
270 
280  protected function ‪enrichOptions(?array $options = null): array
281  {
282  return array_merge(
283  [
284  self::OPTION_SWITCH_FOCUS => null,
285  // 'newTYPO3frontendWindow' was used in BackendUtility::viewOnClick
286  self::OPTION_WINDOW_NAME => 'newTYPO3frontendWindow',
287  self::OPTION_WINDOW_SCOPE => self::OPTION_WINDOW_SCOPE_LOCAL,
288  ],
289  $options ?? []
290  );
291  }
292 
293  protected function ‪loadActionDispatcher(): void
294  {
295  if (!$this->moduleLoading) {
296  return;
297  }
298  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
299  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ActionDispatcher');
300  }
301 
302  protected function ‪loadImmediateActionElement(): void
303  {
304  if (!$this->moduleLoading) {
305  return;
306  }
307  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
308  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Element/ImmediateActionElement');
309  }
310 
311  protected function ‪prefixAttributeNames(string $prefix, array $attributes): array
312  {
313  $attributeNames = array_map(
314  static function (string $name) use ($prefix): string {
315  return $prefix . $name;
316  },
317  array_keys($attributes)
318  );
319  return array_combine(
320  $attributeNames,
321  array_values($attributes)
322  );
323  }
324 }
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\$rootLine
‪array null $rootLine
Definition: PreviewUriBuilder.php:51
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\enrichOptions
‪array enrichOptions(?array $options=null)
Definition: PreviewUriBuilder.php:273
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\create
‪static static create(int $pageId, ?string $alternativeUri=null)
Definition: PreviewUriBuilder.php:75
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\__construct
‪__construct(int $pageId, ?string $alternativeUri=null)
Definition: PreviewUriBuilder.php:84
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\$alternativeUri
‪string null $alternativeUri
Definition: PreviewUriBuilder.php:47
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\$section
‪string null $section
Definition: PreviewUriBuilder.php:55
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\OPTION_WINDOW_FEATURES
‪const OPTION_WINDOW_FEATURES
Definition: PreviewUriBuilder.php:35
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\withModuleLoading
‪static withModuleLoading(bool $moduleLoading)
Definition: PreviewUriBuilder.php:94
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\buildDispatcherDataAttributes
‪array null buildDispatcherDataAttributes(?array $options=null)
Definition: PreviewUriBuilder.php:179
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\loadActionDispatcher
‪loadActionDispatcher()
Definition: PreviewUriBuilder.php:286
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\serializeDispatcherAttributes
‪string null serializeDispatcherAttributes(?array $options=null)
Definition: PreviewUriBuilder.php:210
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\OPTION_WINDOW_SCOPE_LOCAL
‪const OPTION_WINDOW_SCOPE_LOCAL
Definition: PreviewUriBuilder.php:38
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\prefixAttributeNames
‪prefixAttributeNames(string $prefix, array $attributes)
Definition: PreviewUriBuilder.php:304
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\OPTION_WINDOW_NAME
‪const OPTION_WINDOW_NAME
Definition: PreviewUriBuilder.php:34
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\$pageId
‪int $pageId
Definition: PreviewUriBuilder.php:43
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\buildAttributes
‪buildAttributes(?array $options=null)
Definition: PreviewUriBuilder.php:238
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\$additionalQueryParameters
‪string null $additionalQueryParameters
Definition: PreviewUriBuilder.php:59
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\OPTION_WINDOW_SCOPE_GLOBAL
‪const OPTION_WINDOW_SCOPE_GLOBAL
Definition: PreviewUriBuilder.php:39
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\buildUri
‪Uri null buildUri(?array $options=null)
Definition: PreviewUriBuilder.php:152
‪$args
‪$args
Definition: validateRstFiles.php:215
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\loadImmediateActionElement
‪loadImmediateActionElement()
Definition: PreviewUriBuilder.php:295
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder
Definition: PreviewUriBuilder.php:32
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\buildImmediateActionElement
‪string null buildImmediateActionElement(?array $options=null)
Definition: PreviewUriBuilder.php:225
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\$moduleLoading
‪bool $moduleLoading
Definition: PreviewUriBuilder.php:68
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\withAdditionalQueryParameters
‪static withAdditionalQueryParameters(string $additionalQueryParameters)
Definition: PreviewUriBuilder.php:136
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\OPTION_WINDOW_SCOPE
‪const OPTION_WINDOW_SCOPE
Definition: PreviewUriBuilder.php:36
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\OPTION_SWITCH_FOCUS
‪const OPTION_SWITCH_FOCUS
Definition: PreviewUriBuilder.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\$backPath
‪string null $backPath
Definition: PreviewUriBuilder.php:64
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\withRootLine
‪static withRootLine(array $rootLine)
Definition: PreviewUriBuilder.php:108
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\withSection
‪static withSection(string $section)
Definition: PreviewUriBuilder.php:122
‪TYPO3\CMS\Backend\Routing
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\buildDispatcherAttributes
‪array null buildDispatcherAttributes(?array $options=null)
Definition: PreviewUriBuilder.php:195