Certainly! Here's an example of a TYPO3 console command that automatically creates a plugin:
<?php
namespace Vendor\Extension\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extensionmanager\Utility\InstallUtility;
class CreatePluginCommand extends Command
{
protected function configure()
{
$this->setName('extension:createplugin')
->setDescription('Creates a new plugin for an extension')
->addArgument(
'extensionKey',
InputArgument::REQUIRED,
'The extension key where the plugin should be created'
)
->addArgument(
'pluginName',
InputArgument::REQUIRED,
'The name of the plugin'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$extensionKey = $input->getArgument('extensionKey');
$pluginName = $input->getArgument('pluginName');
// Check if the extension exists
if (!is_dir(GeneralUtility::getFileAbsFileName('EXT:' . $extensionKey))) {
$output->writeln('<error>Extension "' . $extensionKey . '" does not exist.</error>');
return 1;
}
// Generate the plugin files
$this->generatePluginFiles($extensionKey, $pluginName);
// Update the extension configuration
$this->updateExtensionConfiguration($extensionKey, $pluginName);
$output->writeln('<info>Plugin "' . $pluginName . '" created successfully for extension "' . $extensionKey . '".</info>');
return 0;
}
protected function generatePluginFiles($extensionKey, $pluginName)
{
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$installUtility = $objectManager->get(InstallUtility::class);
$pluginPath = 'EXT:' . $extensionKey . '/Resources/Private/Plugins/' . $pluginName . '/';
// Create the plugin directory
GeneralUtility::mkdir_deep(GeneralUtility::getFileAbsFileName($pluginPath));
// Create the plugin template file
$templateContent = '<f:layout name="Default" /><f:section name="main"><h1>' . $pluginName . ' Plugin</h1></f:section>';
GeneralUtility::writeFile(GeneralUtility::getFileAbsFileName($pluginPath . 'Templates/Plugin.html'), $templateContent);
// Create the plugin controller
$controllerContent = '<?php' . PHP_EOL . 'namespace Vendor\\' . $extensionKey . '\\Controller;' . PHP_EOL . PHP_EOL . 'use TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController;' . PHP_EOL . PHP_EOL . 'class ' . $pluginName . 'Controller extends ActionController' . PHP_EOL . '{' . PHP_EOL . '}';
GeneralUtility::writeFile(GeneralUtility::getFileAbsFileName($pluginPath . 'Controller/' . $pluginName . 'Controller.php'), $controllerContent);
// Create the plugin locallang file
$localLangContent = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?><T3locallang><meta type="array"><langDisable>1</langDisable></meta><data type="array"><default type="array"><label index="' . strtolower($pluginName) . '.title">' . $pluginName . '</label></default></data></T3locallang>';
GeneralUtility::writeFile(GeneralUtility::getFileAbsFileName($pluginPath . 'Resources/Private/Language/locallang.xlf'), $localLangContent);
}
protected function updateExtensionConfiguration($extensionKey, $pluginName)
{
$extensionConfiguration = $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extensionKey] ?? [];
$pluginKey = strtolower($pluginName);
$extensionConfiguration['plugins'][$pluginKey] = [
'controller' => 'Vendor\\' . $extensionKey . '\\Controller\\' . $pluginName . 'Controller',
'actions' => [
'index' => 'index',
],
'name' => strtolower($extensionKey) . '_' . strtolower($pluginName),
'icon' => 'EXT:' . $extensionKey . '/Resources/Public/Icons/Extension.svg',
'labels' => [
'title' => 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/locallang.xlf:' . strtolower($pluginName) . '.title',
],
];
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extensionKey] = $extensionConfiguration;
}
}
To use this command, you need to register it in your extension's Configuration/Commands.php file:
<?php
return [
'extension:createplugin' => [
'class' => \Vendor\Extension\Command\CreatePluginCommand::class,
],
];
Make sure to replace Vendor and Extension with your actual vendor and extension names.
Once the command is registered, you can run it from the TYPO3 command line using the following syntax:
./vendor/bin/typo3 extension:createplugin extensionKey pluginName
Replace extensionKey with the key of your extension and pluginName with the desired name of your plugin.
The command will perform the following actions:
- Check if the specified extension exists.
- Generate the necessary plugin files, including the template, controller, and locallang files.
- Update the extension configuration to register the plugin.
After running the command, the plugin will be created and ready to use in your TYPO3 extension.
Note: Make sure to adapt the generated code to fit your specific requirements and follow the TYPO3 coding standards and best practices.