Configure the new plugin
Let’s create a front-end plugin to list out employees in the front-end.
- You will need to configure the plugin in ext_localconf.php file at the root of the extension. (Path: employee/ext_localconf.php)
- Here you will define a controller and action in ext_localconf.php file, and before that, you will need to create that controller and action.
Let’s see about the controllers in TYPO3.
Controller
- TYPO3 does have 4 types of controllers.
- Most Extbase controllers are based on the TYPO3CMSExtbaseMvcControllerActionController.
- Most public and protected methods that end with “Action” (for example indexAction() or showAction()), are automatically registered as actions of the controller.
- There are multiple things for the extbase controller which you can refer from this link – https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ExtensionArchitecture/Extbase/Reference/Controller/ActionController.html#actioncontroller
- Here the Action controller will be created, which will be used for the front-end plugin to list the employees in the front-end.
- Create the controller file in the <extension_directory>/Classes/Controller folder, here the controller will be created in the employee/Classes/Controller folder.
- For this example of the “employee” extension, let’s create the Employee controller named “EmployController.php” and the path will be employee/Classes/Controller/EmployController.php
- You can name the controller as per your choice, but the postfix”Controller” is mandatory. Like here the name can be EmployeeController.php or EmployeesController.php or anything you want, but to add the postfix “Controller” after the Employee or Employees is mandatory.
- Create the controller with indexAction() as seen in the code below, to list out the employees. Here you can create any action like you can create listAction() instead of indexAction(), but adding the “Action” postfix is mandatory.
- Here the indexAction() has been defined only, not fetching any records for now.
<?php
declare(strict_types=1);
namespace CompanyEmployeeController;
use TYPO3CMSExtbaseMvcControllerActionController;
use PsrHttpMessageResponseInterface;
class EmployController extends ActionController
{
/**
* action list
*
* @return string|object|null|void
*/
public function indexAction(): ResponseInterface
{
$this->view->assign('title', 'List Of Employee!!');
return $this->htmlResponse();
}
}
- namespace: namespace for controller is vendor/ext_name/Controller.
- Controller name: Here the Action type controller has been used, so Define the controller name (EmployController here) with extending ActionController.
- Action: Here the indexAction() has been created to list out the employees, Define the actione with public accessibilities, and it will return the html response as per TYPO3 standards.
Plugin Configuration
1. Configure the plugin now in ext_localconf.php file.
2. Config the plugin with the controller name and the action name that are used with the plugin.
3. Below is the screenshot of ext_localconf.php file.
<?php declare(strict_types=1); use TYPO3CMSExtbaseUtilityExtensionUtility; use CompanyEmployeeControllerEmployController; // employee list plugin ExtensionUtility::configurePlugin( // extension key or extension name in UpperCamelCase (if the extension name is my products then the name here will be MyProducts) 'Employee', // A unique identifier for your plugin in UpperCamelCase that will be the name of your plugin 'Employlist', // all actions [EmployController::class => 'index'], // non-cacheable actions [EmployController::class => 'index'], );
4. TYPO3CMSExtbaseUtilityExtensionUtility::configurePlugin() generates the necessary TypoScript to display the plugin in the front-end with the following parameters
- Extension key ’employee’ or extension name Employee.
- A unique identifier for your plugin in UpperCamelCase: ‘Employlist’.
- An array of allowed combinations of controllers and actions is stored in an array.
- (Optional) an array of controller names and action names that should not be cached.
5. Here the allowed action of the EmployController is “index”, no other action is allowed here by this plugin.
6. Now let’s register the plugin.
Plugin Registration
1. Register the plugin by adding it to the tt_content file, and if the file is not available, you will need to create it. (it will make the plugin available in cType (Dropdown) ).
2. File path: <extension_directory>/Configuration/TCA/Overrides/tt_content.php, here in this example it will be employee/Configuration/TCA/Overrides/tt_content.php
<?php
defined TYPO3 or die();
(static function (): void{
TYPO3CMSExtbaseUtilityExtensionUtility::registerPlugin(
// extension name, matching the PHP namespaces (but without the vendor)
'Employee',
// arbitrary, but unique plugin name (not visible in the backend), must be the same as used in configurePlugin()
'Employlist',
// plugin title, as visible in the drop-down in the backend, use "LLL:" for localization
'List Of Employee',
// icon identifier
'EXT:employee/Resources/Public/Icons/Extension.png',
);
})();
3. Register the plugin with the following parameters
- Extension key ’employee’ or extension name “Employee”.
- A unique identifier for your plugin in UpperCamelCase: ‘Employlist’, must be the same as used in configurePlugin() or the plugin will not render.
- Plugin title in the backend: Can be a string or a localized string starting with LLL:.
- (Optional) the icon identifier or file path prepended with “EXT:”
4. Now the plugin has been registered.