TYPO3 CMS  TYPO3_7-6
FormUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
29  public static function create(Configuration $configuration)
30  {
32  $formUtility = self::getObjectManager()->get(self::class);
33  $formUtility->setConfiguration($configuration);
34  return $formUtility;
35  }
36 
40  protected $configuration;
41 
45  public function setConfiguration(Configuration $configuration)
46  {
47  $this->configuration = $configuration;
48  }
49 
130  public function renderItem($configuration, $type = null, $defaultMessage = '')
131  {
132  if ($this->configuration->getContentElementRendering()) {
133  $renderedMessage = null;
134  if ($type !== null) {
135  if (is_array($configuration)) {
136  /* Direct cObject rendering */
137  $value = $configuration;
138  } else {
139  /* got only a string, no rendering required */
140  $renderedMessage = $type;
141  }
142  } else {
143  if ($configuration !== null) {
144  /* Render the "short syntax"
145  * The wizard write things like label.value
146  * The previous version of EXT:form interpreted this
147  * as a TEXT content object, so we do the same
148  * */
149  $type = 'TEXT';
150  if (is_array($configuration)) {
151  $value = $configuration;
152  } else {
153  $value['value'] = $configuration;
154  }
155  } else {
156  /* return the default message
157  * If $type === NULL and $configuration === NULL
158  * return the default message (if set).
159  * */
160  $renderedMessage = $defaultMessage;
161  }
162  }
163  if ($renderedMessage === null) {
164  $renderedMessage = $GLOBALS['TSFE']->cObj->cObjGetSingle(
165  $type,
166  $value
167  );
168  }
169  } else {
170  if ($type !== null) {
171  if ($configuration !== null) {
172  /* the wizard write things like label.value = some text
173  * so we need the handle that, even content object rendering
174  * is not allowed.
175  * */
176  if (isset($configuration['value'])) {
177  $renderedMessage = $configuration['value'];
178  } else {
179  $renderedMessage = $defaultMessage;
180  }
181  } else {
182  // string, no rendering required
183  $renderedMessage = $type;
184  }
185  } else {
186  $renderedMessage = $defaultMessage;
187  if (
188  is_array($configuration)
189  && isset($configuration['value'])
190  ) {
191  $renderedMessage = $configuration['value'];
192  }
193  }
194  }
195  return $renderedMessage;
196  }
197 
205  public function renderArrayItems(array &$arrayToRender = [])
206  {
207  foreach ($arrayToRender as $attributeName => &$attributeValue) {
208  $attributeNameWithoutDot = rtrim($attributeName, '.');
209  if (
210  isset($arrayToRender[$attributeNameWithoutDot])
211  && isset($arrayToRender[$attributeNameWithoutDot . '.'])
212  ) {
213  $attributeValue = $this->renderItem(
214  $arrayToRender[$attributeNameWithoutDot . '.'],
215  $arrayToRender[$attributeNameWithoutDot]
216  );
217  unset($arrayToRender[$attributeNameWithoutDot . '.']);
218  } elseif (
219  !isset($arrayToRender[$attributeNameWithoutDot])
220  && isset($arrayToRender[$attributeNameWithoutDot . '.'])
221  ) {
222  $this->renderArrayItems($attributeValue);
223  }
224  }
225  }
226 
238  public function sanitizeNameAttribute($name)
239  {
240  if (!empty($name)) {
241  // Change spaces into hyphens
242  $name = preg_replace('/\\s/', '-', $name);
243  // Remove non-word characters
244  $name = preg_replace('/[^a-zA-Z0-9_\\-]+/', '', $name);
245  }
246  return $name;
247  }
248 
261  public function sanitizeIdAttribute($id)
262  {
263  if (!empty($id)) {
264  // Change spaces into hyphens
265  $attribute = preg_replace('/\\s/', '-', $id);
266  // Change first non-letter to field-
267  if (preg_match('/^([^a-zA-Z]{1})/', $attribute)) {
268  $id = 'field-' . $attribute;
269  }
270  // Remove non-word characters
271  $id = preg_replace('/([^a-zA-Z0-9_:\\-\\.]*)/', '', $id);
272  }
273  return $id;
274  }
275 
279  public static function getObjectManager()
280  {
281  return GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
282  }
283 }
renderArrayItems(array &$arrayToRender=[])
setConfiguration(Configuration $configuration)
Definition: FormUtility.php:45
renderItem($configuration, $type=null, $defaultMessage='')
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']