Lyshatt
Lyshatt

Reputation: 170

How can I overwrite a controller function of a typo3 extension?

I have a Typo3 9 application that uses several extensions. One of them is the "main" extension and includes various resource files, models, controllers and so on. Because this extension is developed seperately and I might have to update it later, I don't want to touch its code. For that reason, I created a separare extension, which already works well to overwrite templates and stylings. However I can't really figure out how to cleanly overwrite controller functions.

Example: The main extension has a controller called "FrontendController.php". This controller has a function called "blogDetailAction", which is called when accessing the detail view of a blog entry. I want to change/overwrite this function by unsing my extension. How would I do that?

Thank you!

Upvotes: 2

Views: 1314

Answers (1)

Florian Rival
Florian Rival

Reputation: 726

If you want to change the code of a class which doesn't provide options like hooks or events, you have to use the XClass (Extending Class) mechanism.

To do that, in your own extension, file EXT:YourExtension/ext_localconf.php, add this kind of code :

$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][VendorName\ExtensionName\Controller\FrontendController::class] = [
   'className' => YourVendorName\YourExtensionName\Xclass\NewFrontendController::class
];

Then in your NewFrontendController.php :

class NewFrontendController extends \VendorName\ExtensionName\Controller\FrontendController
{
   // Your code here
}

Every class created with \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() can be extended with XClass mechanism.

But you have to know that using XClass is risky because : neither the core, nor extensions authors can guarantee that XCLASSes will not break if the underlying code changes.

More information here : https://docs.typo3.org/m/typo3/reference-coreapi/9.5/en-us/ApiOverview/Xclasses/Index.html

Upvotes: 2

Related Questions