‪TYPO3CMS  ‪main
UuidElement.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 Symfony\Component\Uid\Uuid;
22 use TYPO3\CMS\Core\Imaging\IconSize;
27 
32 {
38  protected ‪$defaultFieldInformation = [
39  'tcaDescription' => [
40  'renderType' => 'tcaDescription',
41  ],
42  ];
43 
44  public function ‪__construct(
45  private readonly ‪IconFactory $iconFactory,
46  ) {}
47 
48  public function ‪render(): array
49  {
50  $resultArray = $this->‪initializeResultArray();
51  $parameterArray = $this->data['parameterArray'];
52  $itemValue = htmlspecialchars((string)$parameterArray['itemFormElValue'], ENT_QUOTES);
53  $config = $parameterArray['fieldConf']['config'];
54  $itemName = $parameterArray['itemFormElName'];
55  $fieldId = ‪StringUtility::getUniqueId('formengine-uuid-');
56 
57  if (!isset($config['required'])) {
58  $config['required'] = true;
59  }
60 
61  if ($config['required'] && !Uuid::isValid($itemValue)) {
62  // Note: This can only happen in case the TcaUuid data provider is not executed or a custom
63  // data provider has changed the value afterwards. Since this can only happen in user code,
64  // we throw an exception to inform the administrator about this misconfiguration.
65  throw new \RuntimeException(
66  'Field "' . $this->data['fieldName'] . '" in table "' . $this->data['tableName'] . '" of type "uuid" defines the field to be required but does not contain a valid uuid. Make sure to properly generate a valid uuid value.',
67  1678895476
68  );
69  }
70 
71  $width = $this->‪formMaxWidth(
72  ‪MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth)
73  );
74 
75  $attributes = [
76  'id' => $fieldId,
77  'name' => $itemName,
78  'type' => 'text',
79  'readonly' => 'readonly',
80  'class' => 'form-control disabled',
81  'data-formengine-input-name' => $itemName,
82  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
83  ];
84 
85  $uuidElement = '
86  <input value="' . $itemValue . '"
87  ' . GeneralUtility::implodeAttributes($attributes, true) . '
88  />';
89 
90  $fieldInformationResult = $this->‪renderFieldInformation();
91  $fieldInformationHtml = $fieldInformationResult['html'];
92  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
93 
94  if (($config['enableCopyToClipboard'] ?? true) !== false) {
95  $uuidElement = '
96  <div class="input-group">
97  ' . $uuidElement . '
98  <typo3-copy-to-clipboard
99  class="btn btn-default"
100  title="' . htmlspecialchars(sprintf($this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_copytoclipboard.xlf:copyToClipboard.title'), 'UUID')) . '"
101  text="' . $itemValue . '"
102  >
103  ' . $this->iconFactory->getIcon('actions-clipboard', IconSize::SMALL) . '
104  </typo3-copy-to-clipboard>
105  </div>';
106 
107  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/copy-to-clipboard.js');
108  }
109 
110  $fieldControlResult = $this->‪renderFieldControl();
111  $fieldControlHtml = $fieldControlResult['html'];
112  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
113 
114  $html = [];
115  $html[] = $this->‪renderLabel($fieldId);
116  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
117  $html[] = $fieldInformationHtml;
118  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
119  $html[] = '<div class="form-wizards-wrap">';
120  $html[] = '<div class="form-wizards-element">';
121  $html[] = $uuidElement;
122  $html[] = '</div>';
123 
124  if (!empty($fieldControlHtml)) {
125  $html[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
126  $html[] = '<div class="btn-group">';
127  $html[] = $fieldControlHtml;
128  $html[] = '</div>';
129  $html[] = '</div>';
130  }
131 
132  $html[] = '</div>';
133  $html[] = '</div>';
134  $html[] = '</div>';
135 
136  $resultArray['html'] = implode(LF, $html);
137 
138  return $resultArray;
139  }
140 }
‪TYPO3\CMS\Backend\Form\Element\UuidElement\__construct
‪__construct(private readonly IconFactory $iconFactory,)
Definition: UuidElement.php:43
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\Element\UuidElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: UuidElement.php:37
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getLanguageService
‪getLanguageService()
Definition: AbstractFormElement.php:456
‪TYPO3\CMS\Backend\Form\Element\UuidElement
Definition: UuidElement.php:32
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:332
‪TYPO3\CMS\Backend\Form\Element\UuidElement\render
‪render()
Definition: UuidElement.php:47
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderLabel
‪renderLabel(string $for)
Definition: AbstractFormElement.php:119
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77