Search code examples
typo3typo3-extensions

Typo3: How to get the View exception error


I am Developing a custom extension for Typo3. now I am getting an error if the user did not include my extension from template's include section.

I want to catch this error to show a message from controller. How can I do this?

my controller action.

 public function listAction()
 {
    $audits = $this->auditRepository->findAll();
    $this->view->assign('arrDetails', $audits);
 }

Solution

  • This could be one solution, but not the cleanest.

    We first need to get the values from the field include_static_file that it is located on the sys_template table. So:

    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_template')->createQueryBuilder();
    $result = $queryBuilder
           ->select('include_static_file')
           ->from('sys_template')
           ->execute()
           ->fetch(0);
    

    The we need to get the string and evaluate if your extension key is present. So:

     $extKey = 'your_extension_key';
    
     if (strpos($result['include_static_file'], $extKey) !== false) {
        $audits = $this->auditRepository->findAll();
        $this->view->assign('arrDetails', $audits);
     }
     else {
        $this->addFlashMessage(
                'You forgot to add the static template',
                $messageTitle = 'Template is missing',
                $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING,
                $storeInSession = TRUE
            );
     }
    

    Your HTML

    <f:if condition="{arrDetails}">
        <f:then>
            do something with your content
        </:then>
        <f:else>
               <f:flashMessages />
        </f:else>
    </f:if>
    

    Of course you can write a static function for this or you can use the LocalizationUtility in order to get the text in multiple languages. It is up to you.

    Result:

    enter image description here