‪TYPO3CMS  9.5
TcaRecordTitle.php
Go to the documentation of this file.
1 <?php
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 
22 
30 {
37  public function ‪addData(array $result)
38  {
39  if (!isset($result['processedTca']['ctrl']['label'])) {
40  throw new \UnexpectedValueException(
41  'TCA of table ' . $result['tableName'] . ' misses required [\'ctrl\'][\'label\'] definition.',
42  1443706103
43  );
44  }
45 
46  if ($result['isInlineChild'] && isset($result['processedTca']['ctrl']['formattedLabel_userFunc'])) {
47  // inline child with formatted user func is first
48  $parameters = [
49  'table' => $result['tableName'],
50  'row' => $result['databaseRow'],
51  'title' => '',
52  'isOnSymmetricSide' => $result['isOnSymmetricSide'],
53  'options' => $result['processedTca']['ctrl']['formattedLabel_userFunc_options'] ?? [],
54  'parent' => [
55  'uid' => $result['databaseRow']['uid'],
56  'config' => $result['inlineParentConfig']
57  ]
58  ];
59  // callUserFunction requires a third parameter, but we don't want to give $this as reference!
60  $null = null;
61  GeneralUtility::callUserFunction($result['processedTca']['ctrl']['formattedLabel_userFunc'], $parameters, $null);
62  $result['recordTitle'] = $parameters['title'];
63  } elseif ($result['isInlineChild'] && (isset($result['inlineParentConfig']['foreign_label'])
64  || isset($result['inlineParentConfig']['symmetric_label']))
65  ) {
66  // inline child with foreign label or symmetric inline child with symmetric_label
67  $fieldName = $result['isOnSymmetricSide']
68  ? $result['inlineParentConfig']['symmetric_label']
69  : $result['inlineParentConfig']['foreign_label'];
70  $result['recordTitle'] = $this->‪getRecordTitleForField($fieldName, $result);
71  } elseif (isset($result['processedTca']['ctrl']['label_userFunc'])) {
72  // userFunc takes precedence over everything else
73  $parameters = [
74  'table' => $result['tableName'],
75  'row' => $result['databaseRow'],
76  'title' => '',
77  'options' => $result['processedTca']['ctrl']['label_userFunc_options'] ?? [],
78  ];
79  $null = null;
80  GeneralUtility::callUserFunction($result['processedTca']['ctrl']['label_userFunc'], $parameters, $null);
81  $result['recordTitle'] = $parameters['title'];
82  } else {
83  // standard record
84  $result = $this->‪getRecordTitleByLabelProperties($result);
85  }
86 
87  return $result;
88  }
89 
96  protected function ‪getRecordTitleByLabelProperties(array $result)
97  {
98  $titles = [];
99  $titleByLabel = $this->‪getRecordTitleForField($result['processedTca']['ctrl']['label'], $result);
100  if (!empty($titleByLabel)) {
101  $titles[] = $titleByLabel;
102  }
103 
104  $labelAltForce = isset($result['processedTca']['ctrl']['label_alt_force'])
105  ? (bool)$result['processedTca']['ctrl']['label_alt_force']
106  : false;
107  if (!empty($result['processedTca']['ctrl']['label_alt']) && ($labelAltForce || empty($titleByLabel))) {
108  // Dive into label_alt evaluation if label_alt_force is set or if label did not came up with a title yet
109  $labelAltFields = GeneralUtility::trimExplode(',', $result['processedTca']['ctrl']['label_alt'], true);
110  foreach ($labelAltFields as $fieldName) {
111  $titleByLabelAlt = $this->‪getRecordTitleForField($fieldName, $result);
112  if (!empty($titleByLabelAlt)) {
113  $titles[] = $titleByLabelAlt;
114  }
115  if (!$labelAltForce && !empty($titleByLabelAlt)) {
116  // label_alt_force creates a comma separated list of multiple fields.
117  // If not set, one found field with content is enough
118  break;
119  }
120  }
121  }
122 
123  $result['recordTitle'] = implode(', ', $titles);
124  return $result;
125  }
126 
134  protected function ‪getRecordTitleForField($fieldName, $result)
135  {
136  if ($fieldName === 'uid') {
137  // uid return field content directly since it usually has not TCA definition
138  return $result['databaseRow']['uid'];
139  }
140 
141  if (!isset($result['processedTca']['columns'][$fieldName]['config']['type'])
142  || !is_string($result['processedTca']['columns'][$fieldName]['config']['type'])
143  ) {
144  return '';
145  }
146 
147  $recordTitle = '';
148  $rawValue = null;
149  if (array_key_exists($fieldName, $result['databaseRow'])) {
150  $rawValue = $result['databaseRow'][$fieldName];
151  }
152  $fieldConfig = $result['processedTca']['columns'][$fieldName]['config'];
153  switch ($fieldConfig['type']) {
154  case 'radio':
155  $recordTitle = $this->‪getRecordTitleForRadioType($rawValue, $fieldConfig);
156  break;
157  case 'inline':
158  $recordTitle = $this->‪getRecordTitleForInlineType(
159  $rawValue,
160  $result['processedTca']['columns'][$fieldName]['children']
161  );
162  break;
163  case 'select':
164  $recordTitle = $this->‪getRecordTitleForSelectType($rawValue, $fieldConfig);
165  break;
166  case 'group':
167  $recordTitle = $this->‪getRecordTitleForGroupType($rawValue);
168  break;
169  case 'check':
170  $recordTitle = $this->‪getRecordTitleForCheckboxType($rawValue, $fieldConfig);
171  break;
172  case 'input':
173  $recordTitle = $this->‪getRecordTitleForInputType($rawValue, $fieldConfig);
174  break;
175  case 'text':
176  $recordTitle = $this->‪getRecordTitleForTextType($rawValue);
177  break;
178  case 'flex':
179  // @todo: Check if and how a label could be generated from flex field data
180  default:
181 
182  }
183 
184  return $recordTitle;
185  }
186 
194  protected function ‪getRecordTitleForRadioType($value, $fieldConfig)
195  {
196  if (!isset($fieldConfig['items']) || !is_array($fieldConfig['items'])) {
197  return '';
198  }
199  foreach ($fieldConfig['items'] as $item) {
200  list($itemLabel, $itemValue) = $item;
201  if ((string)$value === (string)$itemValue) {
202  return $itemLabel;
203  }
204  }
205  return '';
206  }
207 
213  protected function ‪getRecordTitleForInlineType($value, array $children)
214  {
215  foreach ($children as $child) {
216  if ((int)$value === $child['vanillaUid']) {
217  return $child['recordTitle'];
218  }
219  }
220 
221  return '';
222  }
223 
231  protected function ‪getRecordTitleForSelectType($value, $fieldConfig)
232  {
233  if (!is_array($value)) {
234  return '';
235  }
236  $labelParts = [];
237  if (!empty($fieldConfig['items'])) {
238  $listOfValues = array_column($fieldConfig['items'], 1);
239  foreach ($value as $itemValue) {
240  $itemKey = array_search($itemValue, $listOfValues);
241  if ($itemKey !== false) {
242  $labelParts[] = $fieldConfig['items'][$itemKey][0];
243  }
244  }
245  }
246  $title = implode(', ', $labelParts);
247  if (empty($title) && !empty($value)) {
248  $title = implode(', ', $value);
249  }
250  return $title;
251  }
252 
259  protected function ‪getRecordTitleForGroupType($value)
260  {
261  $labelParts = [];
262  foreach ($value as $singleValue) {
263  if (isset($singleValue['uidOrPath'])) {
264  $labelParts[] = $singleValue['uidOrPath'];
265  } elseif (isset($singleValue['folder'])) {
266  $labelParts[] = $singleValue['folder'];
267  } else {
268  $labelParts[] = $singleValue['title'];
269  }
270  }
271  return implode(', ', $labelParts);
272  }
273 
281  protected function ‪getRecordTitleForCheckboxType($value, $fieldConfig)
282  {
283  $languageService = $this->‪getLanguageService();
284  if (empty($fieldConfig['items']) || !is_array($fieldConfig['items'])) {
285  $title = (bool)$value
286  ? $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:yes')
287  : $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:no');
288  } else {
289  $labelParts = [];
290  foreach ($fieldConfig['items'] as $key => $val) {
291  if ($value & pow(2, $key)) {
292  $labelParts[] = $val[0];
293  }
294  }
295  $title = implode(', ', $labelParts);
296  }
297  return $title;
298  }
299 
307  protected function ‪getRecordTitleForInputType($value, $fieldConfig)
308  {
309  if (!isset($value)) {
310  return '';
311  }
312  if (!isset($fieldConfig['eval'])) {
313  return $value;
314  }
315  $title = $value;
316  $dateTimeFormats = ‪QueryHelper::getDateTimeFormats();
317  if (GeneralUtility::inList($fieldConfig['eval'], 'date')) {
318  // Handle native date field
319  if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'date') {
320  $value = $value === $dateTimeFormats['date']['empty'] ? 0 : (int)strtotime($value);
321  } else {
322  $value = (int)$value;
323  }
324  if (!empty($value)) {
325  $ageSuffix = '';
326  // Generate age suffix as long as not explicitly suppressed
327  if (!isset($fieldConfig['disableAgeDisplay']) || (bool)$fieldConfig['disableAgeDisplay'] === false) {
328  $ageDelta = ‪$GLOBALS['EXEC_TIME'] - $value;
329  $calculatedAge = ‪BackendUtility::calcAge(
330  abs($ageDelta),
331  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears')
332  );
333  $ageSuffix = ' (' . ($ageDelta > 0 ? '-' : '') . $calculatedAge . ')';
334  }
335  $title = ‪BackendUtility::date($value) . $ageSuffix;
336  }
337  } elseif (GeneralUtility::inList($fieldConfig['eval'], 'time')) {
338  // Handle native time field
339  if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'time') {
340  $value = $value === $dateTimeFormats['time']['empty'] ? 0 : (int)strtotime('1970-01-01 ' . $value);
341  } else {
342  $value = (int)$value;
343  }
344  if (!empty($value)) {
345  $title = gmdate('H:i', (int)$value);
346  }
347  } elseif (GeneralUtility::inList($fieldConfig['eval'], 'timesec')) {
348  // Handle native time field
349  if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'time') {
350  $value = $value === $dateTimeFormats['time']['empty'] ? 0 : (int)strtotime('1970-01-01 ' . $value);
351  } else {
352  $value = (int)$value;
353  }
354  if (!empty($value)) {
355  $title = gmdate('H:i:s', (int)$value);
356  }
357  } elseif (GeneralUtility::inList($fieldConfig['eval'], 'datetime')) {
358  // Handle native datetime field
359  if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'datetime') {
360  $value = $value === $dateTimeFormats['datetime']['empty'] ? 0 : (int)strtotime($value);
361  } else {
362  $value = (int)$value;
363  }
364  if (!empty($value)) {
365  $title = ‪BackendUtility::datetime($value);
366  }
367  }
368  return $title;
369  }
370 
377  protected function ‪getRecordTitleForTextType($value)
378  {
379  return trim(strip_tags($value));
380  }
381 
385  protected function ‪getLanguageService()
386  {
387  return ‪$GLOBALS['LANG'];
388  }
389 }
‪TYPO3\CMS\Core\Database\Query\QueryHelper\getDateTimeFormats
‪static array getDateTimeFormats()
Definition: QueryHelper.php:175
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForGroupType
‪string getRecordTitleForGroupType($value)
Definition: TcaRecordTitle.php:259
‪TYPO3\CMS\Backend\Utility\BackendUtility\datetime
‪static string datetime($value)
Definition: BackendUtility.php:1190
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForSelectType
‪string getRecordTitleForSelectType($value, $fieldConfig)
Definition: TcaRecordTitle.php:231
‪TYPO3\CMS\Backend\Utility\BackendUtility\calcAge
‪static string calcAge($seconds, $labels='min|hrs|days|yrs|min|hour|day|year')
Definition: BackendUtility.php:1218
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForInlineType
‪string getRecordTitleForInlineType($value, array $children)
Definition: TcaRecordTitle.php:213
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForInputType
‪string getRecordTitleForInputType($value, $fieldConfig)
Definition: TcaRecordTitle.php:307
‪TYPO3\CMS\Core\Database\Query\QueryHelper
Definition: QueryHelper.php:30
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle
Definition: TcaRecordTitle.php:30
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForCheckboxType
‪string getRecordTitleForCheckboxType($value, $fieldConfig)
Definition: TcaRecordTitle.php:281
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:2
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\addData
‪array addData(array $result)
Definition: TcaRecordTitle.php:37
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForField
‪string getRecordTitleForField($fieldName, $result)
Definition: TcaRecordTitle.php:134
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getLanguageService
‪LanguageService getLanguageService()
Definition: TcaRecordTitle.php:385
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForTextType
‪string getRecordTitleForTextType($value)
Definition: TcaRecordTitle.php:377
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Utility\BackendUtility\date
‪static string date($tstamp)
Definition: BackendUtility.php:1179
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleByLabelProperties
‪array getRecordTitleByLabelProperties(array $result)
Definition: TcaRecordTitle.php:96
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaRecordTitle\getRecordTitleForRadioType
‪string getRecordTitleForRadioType($value, $fieldConfig)
Definition: TcaRecordTitle.php:194