[BUGFIX] EXT:form - catch exceptions within page module and form engine
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Hooks / FormPagePreviewRenderer.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Hooks;
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
18 use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
19 use TYPO3\CMS\Core\Error\Exception;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Messaging\AbstractMessage;
22 use TYPO3\CMS\Core\Messaging\FlashMessage;
23 use TYPO3\CMS\Core\Messaging\FlashMessageService;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Extbase\Object\ObjectManager;
26 use TYPO3\CMS\Extbase\Service\FlexFormService;
27 use TYPO3\CMS\Form\Mvc\Configuration\Exception\NoSuchFileException;
28 use TYPO3\CMS\Form\Mvc\Configuration\Exception\ParseErrorException;
29 use TYPO3\CMS\Form\Mvc\Persistence\Exception\PersistenceManagerException;
30 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface;
31
32 /**
33 * Contains a preview rendering for the page module of CType="form_formframework"
34 */
35 class FormPagePreviewRenderer implements PageLayoutViewDrawItemHookInterface
36 {
37 /**
38 * Localisation prefix
39 */
40 const L10N_PREFIX = 'LLL:EXT:form/Resources/Private/Language/Database.xlf:';
41
42 /**
43 * Preprocesses the preview rendering of the content element "form_formframework".
44 *
45 * @param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling parent object
46 * @param bool $drawItem Whether to draw the item using the default functionalities
47 * @param string $headerContent Header content
48 * @param string $itemContent Item content
49 * @param array $row Record row of tt_content
50 */
51 public function preProcess(
52 \TYPO3\CMS\Backend\View\PageLayoutView &$parentObject,
53 &$drawItem,
54 &$headerContent,
55 &$itemContent,
56 array &$row
57 ) {
58 if ($row['CType'] === 'form_formframework') {
59 $contentType = $parentObject->CType_labels[$row['CType']];
60 $itemContent .= $parentObject->linkEditContent('<strong>' . htmlspecialchars($contentType) . '</strong>', $row) . '<br />';
61
62 $flexFormData = GeneralUtility::makeInstance(FlexFormService::class)
63 ->convertFlexFormContentToArray($row['pi_flexform']);
64
65 $persistenceIdentifier = $flexFormData['settings']['persistenceIdentifier'];
66 if (!empty($persistenceIdentifier)) {
67 try {
68 $formPersistenceManager = GeneralUtility::makeInstance(ObjectManager::class)->get(FormPersistenceManagerInterface::class);
69
70 try {
71 $formDefinition = $formPersistenceManager->load($persistenceIdentifier);
72 $formLabel = $formDefinition['label'];
73 } catch (ParseErrorException $e) {
74 $formLabel = sprintf(
75 $this->getLanguageService()->sL(self::L10N_PREFIX . 'tt_content.preview.invalidPersistenceIdentifier'),
76 $persistenceIdentifier
77 );
78 } catch (PersistenceManagerException $e) {
79 $formLabel = sprintf(
80 $this->getLanguageService()->sL(self::L10N_PREFIX . 'tt_content.preview.inaccessiblePersistenceIdentifier'),
81 $persistenceIdentifier
82 );
83 } catch (Exception $e) {
84 $formLabel = sprintf(
85 $this->getLanguageService()->sL(self::L10N_PREFIX . 'tt_content.preview.notExistingdPersistenceIdentifier'),
86 $persistenceIdentifier
87 );
88 }
89 } catch (NoSuchFileException $e) {
90 $this->addInvalidFrameworkConfigurationFlashMessage($e);
91 $formLabel = $messageText;
92 } catch (ParseErrorException $e) {
93 $this->addInvalidFrameworkConfigurationFlashMessage($e);
94 $formLabel = sprintf(
95 $this->getLanguageService()->sL(self::L10N_PREFIX . 'tt_content.preview.invalidFrameworkConfiguration'),
96 $persistenceIdentifier
97 );
98 }
99 } else {
100 $formLabel = $this->getLanguageService()->sL(self::L10N_PREFIX . 'tt_content.preview.noPersistenceIdentifier');
101 }
102
103 $itemContent .= $parentObject->linkEditContent(
104 $parentObject->renderText($formLabel),
105 $row
106 ) . '<br />';
107
108 $drawItem = false;
109 }
110 }
111
112 /**
113 * @param \Exception $e
114 */
115 protected function addInvalidFrameworkConfigurationFlashMessage(\Exception $e)
116 {
117 $messageText = sprintf(
118 $this->getLanguageService()->sL(self::L10N_PREFIX . 'tt_content.preview.invalidFrameworkConfiguration.text'),
119 $e->getMessage()
120 );
121
122 GeneralUtility::makeInstance(ObjectManager::class)
123 ->get(FlashMessageService::class)
124 ->getMessageQueueByIdentifier('core.template.flashMessages')
125 ->enqueue(
126 GeneralUtility::makeInstance(
127 FlashMessage::class,
128 $messageText,
129 $this->getLanguageService()->sL(self::L10N_PREFIX . 'tt_content.preview.invalidFrameworkConfiguration.title'),
130 AbstractMessage::ERROR,
131 true
132 )
133 );
134 }
135
136 /**
137 * @return LanguageService
138 */
139 protected function getLanguageService(): LanguageService
140 {
141 return $GLOBALS['LANG'];
142 }
143 }