3. Add first Plugin

Let’s get dirty and start with some functionality.

Most extensions provide a Plugin, this is a functionality provided as content element for TYPO3 CMS Frontend. Also Modules are available, that is functionality in TYPO3 CMS Backend.

Also many more parts like Signals and Slots or Hooks are available. You can also provide HashAlgorithms some Logger or CacheBackends, etc. TYPO3 CMS can be extended in many areas.

Still Plugins are a very typical thing to bring in features to your website.

3.1. The Plugin

As mentioned, we will start with a simple example plugin to display “Hello World”.

3.2. Register Plugin in Backend

Task

Register a plugin in TYPO3 backend.

We first need to register the Plugin in the backend. This way it will become available as a new option for the content element Insert Plugin. This is done with the following code in file Configuration/TCA/Overrides/tt_content.php:

(function () {
    \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
        'Workshop.ExampleExtension',
        'pluginkey',
        'Example Plugin'
    );
})();

3.3. Configure Plugin for Frontend

Task

Configure a plugin for TYPO3 frontend.

To actually call some PHP Code when the content element is rendered, we need to configure the plugin in ext_localconf.php:

(function () {
   \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
      'Workshop.ExampleExtension',
      'pluginkey',
      [
            'Example' => 'example'
      ]
   );
})();

3.4. Write necessary Code

Task

Remove all PHP Errors, step by step.

If we insert the plugin as content element and open the site, we should see an error message. This message is not helpful, so we will switch to development context within the installation / maintenance tool. Also we will add the following TypoScript setup for our local development instance:

config.contentObjectExceptionHandler = 0

Afterwards we should see the following error message:

Could not analyse class: “Workshop\ExampleExtension\Controller\ExampleController” maybe not loaded or no autoloader? Class Workshop\ExampleExtension\Controller\ExampleController does not exist

This tells us that everything so far has worked as expected. TYPO3 tries to call our ExampleController, which just does not exist yet.

So let’s create the controller with the following code in Classes/Controller/ExampleController.php:

<?php

namespace Workshop\ExampleExtension\Controller;

/*
 * Copyright (C) 2018 Daniel Siepmann <coding@daniel-siepmann.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class ExampleController extends ActionController
{
}

The error message should change to:

An action “exampleAction” does not exist in controller “Workshop\ExampleExtension\Controller\ExampleController”.

Yeah, we fixed the error to get the next one. Even if our class exists, the configured default action does not exist yet, so let’s create it.

class ExampleController extends ActionController
{
    public function exampleAction()
    {
        return 'Hello world!';
    }
}

We now should see “Hello world!” in our frontend.

We just created our first plugin.