Inject TypoScript SettingsΒΆ

Sometimes you need settings like TypoScript in a class which is not your controller. Inside a controller, the Extbase framework already injects the settings for you, so you are able to access them under $this->settings.

In all other classes it’s easy to let Extbase inject the settings for you. Just include the following code, and make sure you instantiate the class via \TYPO3\CMS\Extbase\Object\ObjectManager instead of \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance().

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace DS\ExampleExtension\Service;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

/**
 * Example Service to demonstrate inject of settings from tx_news.
 *
 * @author Daniel Siepmann <coding@daniel-siepmann.de>
 */
class SomeService
{
   /**
     * Inject news settings via ConfigurationManager.
     *
     * @param ConfigurationManagerInterface $configurationManager
     *
     * @return SomeService
     */
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
    {
        $this->newsSettings = $configurationManager->getConfiguration(
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
            'News',
            'pi1'
        );

        return $this;
    }
}

This will use Extbase inject mechanism of ObjectManager to inject the ConfigurationManager. Instead of persisting this instance as a property, we just use it to fetch the settings and persist them instead. As most developers are used to $this->settings from Controllers, I’m using the same convention in my classes.

This way the settings are available as a property in the class without any further work.

Just make sure you are using the \TYPO3\CMS\Extbase\Object\ObjectManager as pointed out above. It’s really important, as only this class will process the injects. \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() doesn’t support this feature.